How to get selectedKey of sap.m.Select from outside?

纵饮孤独 提交于 2019-12-20 05:24:09

问题


Having this select control:

new Select("id", {
  items: {
    path: "/cards",
    template: new ListItem({
      key: "{Kunnr}",
      text: "{Descrip}"
    }),
  },
});

I need it be able to get the selected key of it, but not in the change event. I need it from outside (another function).

I have tried using the ID but I just get undefined as result.


回答1:


There is a some information missing from your question, mostly around how oSelectMarca is added to your view. This makes a difference in how it's made available in your app. There are two places to get something by Id:

this.getView().byId('id');
sap.ui.getCore().byId('id');

You'll have to check which one it is... Another option is to add that model to your view instead of oSelectMarca in which case your view and your select can share data. But once again that depends on how you add the select to the screen.




回答2:


Depending on where You need to use it You can do:

this.oSelectMarca = new sap.m.Select('id',{});
this.oSelectMarca.setModel(myModel);

or If You'll be using it in say another controller try:

sap.ui.getCore().oSelectMarca = new sap.m.Select('id',{});
sap.ui.getCore().oSelectMarca.setModel(myModel);



回答3:


Try with two-way data binding in selectedKey which helps to keep the MV* pattern.

new Select({
  selectedKey: "{/selectedCard}" // <-- It's TwoWay
  items: {
    path: "/cards",
    template: new ListItem({
        key: "{Kunnr}",
        text: "{Descrip}"
    }),
  },
});

I'm assuming that the default model is accessible across the application. Hence, as long as you can access that model, you can get the selected key by myModel.getProperty("/selectedCard");




回答4:


I solved it by accesing to the properties from outside using the sap core:

var myvar = sap.ui.getCore().getModel("marcas");
var selectedKey= myvar.getProperty('/cards/Kunnr');


来源:https://stackoverflow.com/questions/53563493/how-to-get-selectedkey-of-sap-m-select-from-outside

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