在本課程中,您將使用 DataGrid 小組件來建立表格。DataGrid 小組件具有互動和視覺化呈現的進階功能,使其比 GridLayout 小組件更適合於顯示記錄陣列。
在第 4 課中,您已將記錄變數拖曳至編輯器,以建立 GridLayout 小組件。 在本課程中,您會將記錄陣列拖曳至編輯器,依預設,這會建立 DataGrid 小組件。
historyResults



selectionMode = DataGridLib.SINGLE_SELECTION,指定的值會確保使用者只能選取格線的一列,而不能選取多列。
selectionListeners ::= cellClicked,
您剛剛更新了接聽器內容,此內容會以陣列元素順序來執行函數的陣列。尤其是當您已將函數附加至與 selectionListeners 內容相關聯的函數陣列時。 稍後,您將在本課程中對新函數進行編碼。
接聽器函數在回應使用者動作時執行,例如按一下動作或在某些情況下,回應用於選取/取消選取列或更新勾選框的函數呼叫時。
, alignment = DataGridLib.ALIGN_RIGHT例如,「本金」直欄的宣告現在與下列程式碼相似:
new DataGridColumn {name = "loanAmount", displayName = "Principal", width = 80,
alignment = DataGridLib.ALIGN_RIGHT},
, formatters = [ formatDollars ]現在,整個宣告與下列程式碼相似:
new DataGridColumn {name = "loanAmount", displayName = "Principal", width = 80,
alignment = DataGridLib.ALIGN_RIGHT, formatters = [ formatDollars ]},
, formatters = [ DataGridFormatters.percentage ]
, formatters = [ formatDollars ]現在,此程式碼將具有下列內容:

一般而言,formatters 內容會採用函數名稱陣列。可以預先定義這些函數,也可以撰寫自訂函數。例如,percentage 函數在隨附於 com.ibm.egl.rui.widgets 專案的 DataGridFormatters 程式庫中予以提供。
InfoBus.subscribe("mortgageApplication.mortgageCalculated", addResultRecord);
如先前所述,當服務傳回新的計算時,使用 Infobus 來呼叫函數。// Update the grid to include the latest mortgage calculation function addResultRecord(eventName STRING in, dataObject ANY in) resultRecord MortgageCalculationResult = dataObject as MortgageCalculationResult; historyResults.appendElement(resultRecord); historyResults_ui.data = historyResults as ANY[]; end這裡,會將送入的值強制轉型為 MortgageCalculationResult 記錄。然後,將新結果附加至結果陣列,並更新資料內容。該更新會導致小組件進行重新整理。
// Publish an event to the InfoBus whenever the user selects an old calculation
function cellClicked(myGrid DataGrid in)
updateRec MortgageCalculationResult = myGrid.getSelection()[1]
as MortgageCalculationResult;
InfoBus.publish("mortgageApplication.mortgageResultSelected", updateRec);
end
該函數會擷取使用者選取的資料網格列,並將該列提供給 Infobus。Infobus 會呼叫已訂閱 "mortgageApplication.mortgageResultSelected" 事件的任何處理程式中的函數。function formatDollars(class string, value string, rowData any in) value = mortgageLib.formatMoney(value); end因為參數修飾元依預設是 InOut,所以第二個參數的值可用於 EGL 執行時期程式碼。
請注意,您正在重複使用 mortgageLib 程式庫中的 formatMoney 函數。
在下一節課程中,您會將此處理程式與應用程式的其餘組件整合。