如何在MVC中绑定数据

时间:2014-04-01 05:02:33

标签: sapui5

我有一个视图页面:

sap.ui.jsview("mvc.oPage1", {

    getControllerName : function() {
        return "mvc.oPage1";
    },

    createContent : function(oController) {
        var oPage1 = new sap.m.Page({
            title : "something",
            enableScrolling : true,
            footer : new sap.m.Bar({
                id : 'page1_footer',
                contentMiddle : [ new sap.m.Button({

                })]
            })
        });

        oTable = new sap.m.Table({
            id: "first_table",
            inset : true,
            visible : true,
            getIncludeItemInSelection : true,
            showNoData : false,
            columns : [ new sap.m.Column({
                styleClass : "name",
                hAlign : "Left",
                header : new sap.m.Label({

                })
            }) ]
        });

        var template = new sap.m.ColumnListItem({
            id: "first_template",
            type : "Navigation",
            visible : true,
            selected : true,
            cells : [ new sap.m.Label({
                text : "{name}"
            }) ]
        });

        var oModel1 = sap.ui.getCore().getModel("jsonModel");
        oTable.setModel(oModel1);
        oTable.bindItems("/landscapes", template);
        oPage1.addContent(oTable);

        return oPage1;
    }
});

我有一个包含json数据的控制器页面:

sap.ui.controller("mvc.oPage1", {

    onInit: function() {

        var jsonData = {
            "landscapes" : []
        };
        $.getJSON(URI, {}).done(function(data) {
            $.each(data.landscapes, function(i, field) {
                jsonData.landscapes.push({
                    "name" : field
                });
            });
        }).fail(function() {
            alert("Error");
        });

        var oModel = new sap.ui.model.json.JSONModel();
        oModel.setData(jsonData);
        sap.ui.getCore().setModel(oModel, "jsonModel");
    },
});

我无法使绑定工作。如您所见,我想在视图中将jsonData连接到模板,并在表列中显示它。

2 个答案:

答案 0 :(得分:5)

您有两种解决方案:

1。您可以从

中删除“jsonModel”
sap.ui.getCore().setModel(oModel, "jsonModel");

2。或者您可以将 jsonModel> 添加到数据绑定中

oTable.bindItems("jsonModel>/landscapes", template);

答案 1 :(得分:0)

试试这个

var template = new sap.m.ColumnListItem({ id: "first_template", type : "Navigation", visible : true, selected : true, cells : { **path:"/", template: new sap.m.Label({ text : "{name}"** }) } });