How can I get JSON as function Parameter in sapui5 m.table?

不问归期 提交于 2019-12-08 09:27:13

问题


I have tried to create dynamic combobox or multiInput in dynamic table. You can see below my the view screen that I expect and my own code.

onCreateTable: function () {
        var summaryDetailData = {
            "subvariants": [{
                "kontrolNesnesiTanim": "test 1 Tanım",
                "kontrolNesnesiBelirtim": "test 1 Belirtim",
                "type": "comboBox",
                "kontrolSonucu": {
                    "test0": "test00",
                    "test1": "test10",
                    "test2": "test20"
                },
                "kontrolEdilecek": 20,
                "kontrolEdilen": 0,
                "icon": "sap-icon://accept"
            }, {
                "kontrolNesnesiTanim": "test 2 Tanım",
                "kontrolNesnesiBelirtim": "test 2 Belirtim",
                "type": "input",
                "kontrolSonucu": {
                    "test0": "test00",
                    "test1": "test10",
                    "test2": "test20",
                    "test3": "test30"
                },
                "kontrolEdilecek": 10,
                "kontrolEdilen": 0,
                "icon": "sap-icon://decline"
            } ]
        };

        var oTable = new sap.m.Table("idRandomDataTable", {
            headerToolbar: new sap.m.Toolbar({
                content: [new sap.m.Label({
                    text: ""
                })]
            }),
            columns: [new sap.m.Column({
                width: "2em",
                header: new sap.m.Label({
                    text: "Kontrol Nesnesi"
                })
            }), new sap.m.Column({
                width: "2em",
                header: new sap.m.Label({
                    text: "Kontrol Sonucu"
                })
            }), new sap.m.Column({
                width: "2em",
                header: new sap.m.Label({
                    text: "Kontrol Edilecek"
                })
            })]
        });
        this.getView().byId("SimpleFormChange480_TrialSonuc").addContent(oTable);
        oTable.bindItems("/subvariants", new sap.m.ColumnListItem({
            cells: [new sap.m.ObjectIdentifier({
                    title: "{kontrolNesnesiTanim}",
                    "titleActive": true,
                    text: "{kontrolNesnesiBelirtim}"
                 }),
                 this.kontrolSonucu("{type}"), 
                 new sap.m.Input({
                    value: "{kontrolEdilen}"
                })
            ]
        }));

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

    kontrolSonucu: function (type) {
        if (type === "input") {
            return new sap.m.MultiInput({
                showValueHelp: false
            });
        } else {
            return new sap.m.ComboBox({
                items: {
                    path: "kontrolSonucu/",
                    template: new sap.ui.core.Item({
                        key: "{}",
                        change: "onChange",
                        text: "{}"
                    })
                }
            });
        }
    }

How can I get JSON as function Parameter?

And also I want to use sap.m.table library not sap.ui.table

I am looking forward your response.


回答1:


You can achieve it using the factory function

onCreateTable: function () {
        var summaryDetailData = {
            "subvariants": [{
                "kontrolNesnesiTanim": "test 1 Tanım",
                "kontrolNesnesiBelirtim": "test 1 Belirtim",
                "type": "comboBox",
                "kontrolSonucu": {
                    "test0": "test00",
                    "test1": "test10",
                    "test2": "test20"
                },
                "kontrolEdilecek": 20,
                "kontrolEdilen": 0,
                "icon": "sap-icon://accept"
            }, {
                "kontrolNesnesiTanim": "test 2 Tanım",
                "kontrolNesnesiBelirtim": "test 2 Belirtim",
                "type": "input",
                "kontrolSonucu": {
                    "test0": "test00",
                    "test1": "test10",
                    "test2": "test20",
                    "test3": "test30"
                },
                "kontrolEdilecek": 10,
                "kontrolEdilen": 0,
                "icon": "sap-icon://decline"
            } ]
        };

        var oTable = new sap.m.Table({
            headerToolbar: new sap.m.Toolbar({
                content: [new sap.m.Label({
                    text: ""
                })]
            }),
            columns: [new sap.m.Column({
                width: "2em",
                header: new sap.m.Label({
                    text: "Kontrol Nesnesi"
                })
            }), new sap.m.Column({
                width: "2em",
                header: new sap.m.Label({
                    text: "Kontrol Sonucu"
                })
            }), new sap.m.Column({
                width: "2em",
                header: new sap.m.Label({
                    text: "Kontrol Edilecek"
                })
            })]
        });
        sap.ui.getCore().byId("tableWrapper").addItem(oTable); //Comment this code and uncomment the below code for table rendering
        //this.getView().byId("SimpleFormChange480_TrialSonuc").addContent(oTable);

        oTable.bindItems({
            path: "/subvariants",
            factory: this.populateItems.bind(this) //factory function
        });
        oTable.setModel(new sap.ui.model.json.JSONModel(summaryDetailData));
    },
    /* Factory function */
    populateItems: function(sId, oContext) { 
        var oInput = null;
        switch(oContext.getObject().type) {
            case "comboBox":
                oInput = new sap.m.ComboBox({ });
                break;
            case "input":
                oInput = new sap.m.MultiInput({ });
                break;
        }       
        var oRow = new sap.m.ColumnListItem(sId, {
            type: "Active",
            cells: [
               new sap.m.Text({ text: "{kontrolNesnesiTanim}" }),
               new sap.m.Text({ text: "{kontrolNesnesiBelirtim}" }),
               oInput
            ]
        });
        return oRow;
    },


来源:https://stackoverflow.com/questions/55022874/how-can-i-get-json-as-function-parameter-in-sapui5-m-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!