2013-10-28 18 views
0

在舊的Lasso 8代碼中,我經常在條件中使用field('foo', -recordindex=(loop_count - 1)),以便在記錄之間更改值時在表中顯示標題行。在Lasso 9中,field方法的-recordindex參數是否存在?備擇方案?

在Lasso 9中,-recordindex參數似乎被忽略。它也沒有記錄。

我的第一個解決方法是爲每個記錄迭代設置一個變量,並將當前記錄的字段與該值進行比較。

<?= var('lastSortFieldValue') = null ?> 
[records] 
    [if(field('sortField') != $lastSortFieldValue)] 
     <!-- display header --> 
    [/if] 
    <!-- display row --> 
    <?= $lastSortFieldValue = field('sortField') => 
[/records] 

這裏的最佳實踐的任何建議?提前感謝您的時間和幫助。

-Justin

回答

1

你設置變量的解決方法可能是最好的事情。 (雖然我會把它變成一個局部變量而不是線程變量,因爲它可能不需要線程範圍。)

如果你真的想要一個方法來做什麼field(...,-recordIndex)以下是一種方法:

define fieldFromRow(field::string, row::integer) => { 
    local(curMap = inline_scopeGet) 
    !#curMap || #curMap->size == 0? return null; 

    local(stashRow) = #curMap->find(::currentrow) 
    handle => { 
     #curMap->insert(::currentrow = #stashRow) 
    } 

    #curMap->insert(::currentrow = #curMap->find(::currentset)->get(INLINE_RESULTROWS_POS)->get(#row)) 

    return column(#field) 
} 

此方法獲取當前的內聯信息。然後它存儲當前行並設置一個處理程序,以便在完成所有操作時恢復當前行。然後它將當前行設置爲方法調用中指定的行並返回指定字段的值。有點破解,但它適用於我的基本測試。

+0

非常有益的工作,各地的地方637
結果!感謝您的自定義方法。我把它放在我的實例的LassoStartup中,並迅速將所有出現的舊方式轉換爲臨時的新方式。你如何找到所有這些祕密類型和方法,如inline_scopeGet和INLINE_RESULTROWS_POS? :-) –

+0

我同意Jolle,而優雅的解決方案不必要地增加開銷。賈斯汀,你的第一個方法可能是最有效的。 –

+0

我也同意Jolle和Ke - 使用這種方法來使事情有效,但正如上面所說的,使用變量的解決方案是最好的方法。 (順便說一句,我的解決方案可以通過不使用列來抓取數據而加快速度,但這可能會在邊緣情況下導致意外的結果。) – bfad

1

然而優雅的Brads自定義方法是我建議不要使用它。它比設置和比較局部變量要慢。

local(
    timer, 
    loops = 1000, 
    present_value, 
    prev_value, 
    out = string, 
    result = string 
) 

inline(-database = 'mysql', -sql = "SELECT * FROM help_category") => { 

    #timer = micros 

    loop(#loops) => { 
     rows => { 
      #present_value = field('name') 
      #prev_value != #present_value ? #out -> append('<hr />Here be a new field<br />') 
      #out -> append(#present_value + '<br />') 
      #prev_value = #present_value 
     } 
    } 
    #result -> append('Result using local ' + ((micros - #timer)/# loops) + '<br />') 

    #timer = micros 

    loop(#loops) => { 
     rows => { 
      #present_value = field('name') 
      loop_count > 1 and #prev_value != fieldFromRow(#present_value, loop_count -1) ? #out -> append('<hr />Here be a new field<br />') 
      #out -> append(#present_value + '<br />') 
     } 
    } 
    #result -> append('Result using fieldFromRow ' + ((micros - #timer)/# loops) + '<br />') 

} 
#result 

- >結果使用使用fieldFromRow 1612

+0

感謝您的其他信息。在我實現Brad的自定義方法時,我添加了一個記錄使用情況的步驟,以便稍後返回並清理它。現在,這是一個簡單的正則表達式查找和替換,使很多東西開始工作。再次感謝您對此的詳細介紹。 –

相關問題