2017-04-14 62 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"); 
    } 
} 
} 
相关问题