I'm having troubles getting dynamic binding of both my table columns and rows to work.
Suppose I have two models, one holding the table column info:
var aColumnData = [
{
columnId : "col1"
},
{
columnId : "col2"
},
{
columnId : "col3"
},
{
columnId : "col4"
},
{
columnId : "col5"
}
];
and one with the data:
var aData = [
{
col1 : "Row 1 col 1",
col2 : "Row 1 col 2",
col3 : "Row 1 col 3",
col4 : "Row 1 col 4",
col5 : "Row 1 col 5"
},
{
col1 : "Row 2 col 1",
col2 : "Row 2 col 2",
col3 : "Row 2 col 3",
col4 : "Row 2 col 4",
col5 : "Row 2 col 5"
}
];
I then set the model :
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData({
columns : aColumnData,
rows : aData
});
I then create my table in the view:
var oTable = new sap.ui.table.Table();
var oColumnTemplate = new sap.ui.table.Column({
label : "{columnDesc}",
template : new sap.ui.commons.TextView().bindProperty("text", "<this_should_be_dynamic_but_how?>")
});
oTable.bindColumns("/columns", oColumnTemplate);
oTable.bindRows("/rows");
The part that bugs me is the binding to the current column in the TextView template; this should be dynamic ("col1", "col2", etc) and done on the fly -- that's what bindings are for anyway I assume -- but I can't get it to work...
I guess I'm missing something simple and trivial, but I'm a bit lost now...
Any help is highly appreciated!
==============================
EDIT: I got it to work by iterating through the columns array and using the addColumn() method:
jQuery.each(aColumnData, function(i, v) {
oTable.addColumn(new sap.ui.table.Column({
label : v.columnDesc,
template: new sap.ui.commons.TextView().bindProperty("text", v.columnId)
}));
});
...but I was hoping there would be a more cleaner approach using the bindColumns() / bindRows() approach:
See Question&Answers more detail:
os