Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SAPUI5 Sap.m.Table Dynamic creation

Tags:

sapui5

I have created a responsive sap.m.table. But am not able to load values from the data Object. I want to laod the "subvariants" array of objects.Pls help

 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() + "}" })
            })
        }));
like image 264
Papu Avatar asked Feb 15 '23 04:02

Papu


1 Answers

The way you bind the model to the table is not completely correct. You have to use bindItems to dynamically bind the table rows (items) to a model. The columns aggregation is used to define the column layout of the table whereas the items aggregation is responsible for the table records.

In your case I would create the columns in the control definition and bind the items to the model with your own template.

This should do what you´ve expected (tested it):

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));

Hopefully this helps you!

like image 89
Tim Gerlach Avatar answered Feb 25 '23 00:02

Tim Gerlach