如何将 sap.ui.core 模型绑定到选择列表中? [英] how to bind sap.ui.core model into a select list?

查看:23
本文介绍了如何将 sap.ui.core 模型绑定到选择列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要像这样放一个json数据

Need to put a json data like this

    {  
       "marcas":[  
          {  
             "__metadata":{  
                "id":"data id",
                "uri":"data url",
                "type":"data type"
             },
             "Codcard":"01",
             "Descript":"MasterCard"
          },
    ]
}

进入像这样的选择输入...

Into a select input like this one...

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

我正在尝试将这些数据保存到模型中,然后像上面看到的那样调用它,

I'm trying to do it saving this data into a model and then calling it as you can see above,

        var oModelListMarcasTarjeta = new sap.ui.model.json.JSONModel();
        var marcas = [{
                Descrip: "",
                Kunnr: ""
            }];
        var sUrlCard = "data url";
        var oDataModelCards = new sap.ui.model.odata.ODataModel(sUrlCard, true);

        oDataModelCards.read("dataCollection", {
            async: false,
            success: function(oData, response) {

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

                oModelListMarcasTarjeta.setData({
                    'cards': marcas
                });

                sap.ui.getCore().setModel(oModelListMarcasTarjeta, "marcas");

            }
        });

但不起作用,知道出了什么问题吗?

but is not working, any idea what is wrong?

如果我将模型直接设置为选择输入,当然可以,但由于某种原因,输入不会设置列表中所选项目的值.

If I set the model direct to the select input, of course works, but for some reason the input doesn't set te value of the selected item in the list.

推荐答案

既然你有模型名称,你也需要在绑定路径中使用它

As your have the model name, you need to use it in the binding path as well

假设您有以下来自后端的 JSON 数据.

Let say you have the following JSON data from backend.

JSON 数据

{
  'cards': [{
    'Descrip': "",
    'Kunnr': ""
  }, {
    'Descrip': "ss",
    'Kunnr': "asf"
  }, {
    'Descrip': "fff",
    'Kunnr': "asdf"
  }, {
    'Descrip': "fas",
    'Kunnr': "asdf"
  }, {
    'Descrip': "asdfa",
    'Kunnr': "asdfwer"
  }]
}

绑定

var oModelListMarcasTarjeta = new sap.ui.model.json.JSONModel();
oModelListMarcasTarjeta.setData({ //for implementation I am setting the data like this
  'cards': [{
    'Descrip': "",
    'Kunnr': ""
  }, {
    'Descrip': "ss",
    'Kunnr': "asf"
  }, {
    'Descrip': "fff",
    'Kunnr': "asdf"
  }, {
    'Descrip': "fas",
    'Kunnr': "asdf"
  }, {
    'Descrip': "asdfa",
    'Kunnr': "asdfwer"
  }]
});
sap.ui.getCore().setModel(oModelListMarcasTarjeta, "marcas");

//Use Model name for binding 
var oSelectMarca = new sap.m.Select({
  items: {
    path: "marcas>/cards",
    template: new sap.ui.core.ListItem({
      key: '{marcas>Kunnr}',
      text: '{marcas>Descrip}'
    })
  }
});

这篇关于如何将 sap.ui.core 模型绑定到选择列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆