2017-04-14 66 views
0

我有一張通過數據綁定填充數據的表格。現在在我的控制器中,我試圖循環所有數據綁定項目。這是我迄今爲止,但我不能得到它的工作。如何從控制器中的數據綁定中獲取1個項目?

colorRows : function(oTable) { 
    var items = oTable.getBinding("items"); 
    var rowCount = items.length; //number of visible rows 
    var currentRowContext; 
    for (var i = 0; i < rowCount; i++) { 
     currentRowContext = items[i].getValue(); //this won't work 
    } 
} 

所以我需要從與匹配我的索引項目中獲得一個值。

編輯:我使用sap.m.table

回答

0

我假設你使用sap.m.Table

colorRows : function(oTable) { 

    var sPath = oTable.getBinding("items").getPath(); //path to table's data 
    var oModel = this.getView().getModel(); //model which is bound to the table 
    //or var oModel = oTable.getModel(); if the model is bound directly to the table 
    var aData = oModel.getProperty(sPath);//array of rows 
    var rowCount = aData.length; 
    var currentRowContext; 
    for (var i = 0; i < rowCount; i++) { 
     currentRowContext = aData[i]; 
    } 
} 

Here是一個工作示例。

+0

是有可能的項目[I]不工作?我得到錯誤Uncaught TypeError:無法讀取未定義的屬性'getCells'。項目中有數據,但項目[i]導致未定義。 – freshrebel

+0

@freshrebel我的不好,我已經更新了答案。對不起,誤導你。 – keshet

+0

現在它的oModel.getProperty(sPath)不起作用。我得到錯誤:Uncaught TypeError:無法讀取未定義的屬性「長度」。其中aData未定義。 sPath由值/ InvoiceSet定義。 – freshrebel

0

正如Keshet所說,它取決於您使用的表格。這裏是sap.ui.table.Table的一個例子。首先,你得到的每行的背景下,然後你可以訪問保存在該行的數據(順便說一句,有沒有這樣的事情RowValue):

colorRows: function(oTable) { 
var aRows = oTable.getRows(); 
var currentRowValue; 
for (var i = 0; i < aRows.length; i++) { 
    var oRowContext = oTable.getContextByIndex(i); 
    if (oRowContext) { 
    var oRowObject = oRowContext.getObject(); 
    // or you can use the getProperty method 
    var oSingleValue = oRowContext.getProperty("yourPropertyName"); 
    } 
} 
} 
相關問題