如何用数据动态填充sap.m.Select?

时间:2018-11-13 13:17:16

标签: javascript sapui5

我需要在sap.m中显示一些odata的数据。选择但不知道为什么不起作用,这是我到目前为止的代码

    var oModel = new sap.ui.model.json.JSONModel();
    var data = [];
    var sUrlCard = "odata's url";
    var oDataModel = new sap.ui.model.odata.ODataModel(sUrlCard, true);

    oDataModel.read("CardBrandCollectionSet", {
        async: false,
        success: function(oData, response) {


            $.each(oData.results, function(i, val) {
                data.push(val);
            });

            oModel.setData({
                'card': data
            });


            sap.ui.getCore().setModel(oModel, "card");
        },
        error: function(oError) {
            console.log(oError);

        }
    });

选择输入所在的表

    var oTable = new sap.m.Table({
        mode: oMode,
        columns: [
            {
            hAlign: 'Center',
            header: new Text({
                text: "Card"
            })
            }
        ]
    });

选择我需要填写数据的输入

    var oSelectMarca = new sap.m.Select({
        items: {
            path: "/card",
            template: new sap.ui.core.ListItem({
                key: '{Codcard}',
                text: '{Descript}'
            }),
            templateShareable: true
        },
        selectedKey: '{Marca}'
    });

2 个答案:

答案 0 :(得分:1)

您不想创建所有这样的odata模型的拳头,请在清单中指定它:

在“ sap.app”部分:

"dataSources": {
    "yourRemote": {
        "uri": "YOUR_URI",
        "type": "OData",
        "settings": {
            "odataVersion": "2.0"
        }
    }
}

在“ sap.ui5”部分:

"models": {
    "i18n": {
        "type": "sap.ui.model.resource.ResourceModel",
        "settings": {
            "bundleName": "YOUR_i18n"
        }
    },
    "remoteModel": {
        "dataSource": "yourRemote"
    }
}

2您不想在js中创建控件,而是在xml文件中执行此操作: https://sapui5.hana.ondemand.com/#/topic/1409791afe4747319a3b23a1e2fc7064
https://blogs.sap.com/2018/05/01/why-do-we-use-xml-views-rather-js-views-in-sapui5/

您的选择需要这样绑定:

<Select
    id="anID"
    valueState="{vsModel>/otherText}"
    valueStateText="{i18n>someText}"
    forceSelection="false"
    items="{
        path: 'remoteModel>/CardBrandCollectionSet',
        sorter: {
            path: 'Descript'
        }
    }">
    <core:Item key="{remoteModel>Codcard}" text="{remoteModel>Descript}" />
</Select>

答案 1 :(得分:0)

选择控件的绑定路径错误:

sap.ui.getCore().setModel(oModel, "card"); // model is set at core with name as card

$.each(oData.results, function(i, val) {
     data.push(val);
 });
  oModel.setData({
       'card': data // setting data in an object with name as card
  });


var oSelectMarca = new sap.m.Select({
        items: {
            path: "card>/card/", //Binding path model name along with array name
            template: new sap.ui.core.ListItem({
                key: '{card>Codcard}', // model name with property name
                text: '{card>Descript}' // model name with property name
            }),
            templateShareable: true
        },
        selectedKey: '{card>Marca}' // model name with property name
    });