SAPUI5 Sap.m.Table动态创建

时间:2014-01-09 07:31:49

标签: sapui5

我创建了一个响应式sap.m.table。但我无法从数据对象加载值。我想申请“subvariants”数组对象。帮助

 summaryDetailData={"subvariants":[{"currentValue":"","Article":"1234567","question":"Carpet Installation type"},{"currentValue":"","question":"CarpetQuantity"},{"currentValue":"","Article":"1234568","question":"Underpad type"},{"currentValue":"","question":"UnderpadQuantity"},{"currentValue":false,"Article":"1234568","question":"Rapid Install"}]}



  var oTable = new sap.m.Table("idRandomDataTable", {
    headerToolbar: new sap.m.Toolbar({
    content: [
    new sap.m.Label({text: "Summary Data"}),
    new sap.m.ToolbarSpacer({}),
    new sap.m.Button("idPersonalizationButton", {
    icon: "sap-icon://person-placeholder"
    })]}),
     columns: summaryDetailData.cols.map(function (colname) {
        return new sap.m.Column({ header: new sap.m.Label({ text: colname })})
            })      
    });

    oTable.setModel(new sap.ui.model.json.JSONModel(summaryDetailData));
    oTable.bindAggregation("subvariants", "/subvariants", new sap.m.ColumnListItem({
        cells: oData.cols.map(function (colname) {
            return new sap.m.Label({ text: "{" + colname.toLowerCase() + "}" })
            })
        }));

1 个答案:

答案 0 :(得分:8)

将模型绑定到表的方式并不完全正确。您必须使用bindItems将表行(项)动态绑定到模型。 columns聚合用于定义表的列布局,而items聚合负责表记录。

在您的情况下,我将在控件定义中创建列,并使用您自己的模板将项绑定到模型。

这应该做你所期望的(测试它):

var oTable = new sap.m.Table("idRandomDataTable", {
        headerToolbar : new sap.m.Toolbar({
            content : [ new sap.m.Label({
                text : "Summary Data"
            }), new sap.m.ToolbarSpacer({}), new sap.m.Button("idPersonalizationButton", {
                icon : "sap-icon://person-placeholder"
            }) ]
        }),
        columns : [ new sap.m.Column({
            width : "2em",
            header : new sap.m.Label({
                text : "Current Value"
            })
        }), new sap.m.Column({
            width : "2em",
            header : new sap.m.Label({
                text : "Article"
            })
        }), new sap.m.Column({
            width : "2em",
            header : new sap.m.Label({
                text : "Question"
            })
        }) ]
    });

    oTable.bindItems("/subvariants", new sap.m.ColumnListItem({
        cells : [ new sap.m.Text({
            text : "{currentValue}"
        }), new sap.m.Text({
            text : "{Article}"
        }), new sap.m.Text({
            text : "{question}",
        }), ]
    }));

    oTable.setModel(new sap.ui.model.json.JSONModel(summaryDetailData));

希望这有助于你!