How to get selectedKey of sap.m.Select from outsid

2019-03-02 07:26发布

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.

标签: sapui5
4条回答
We Are One
2楼-- · 2019-03-02 07:48

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");

查看更多
放我归山
3楼-- · 2019-03-02 07:53

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');
查看更多
啃猪蹄的小仙女
4楼-- · 2019-03-02 07:54

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.

查看更多
走好不送
5楼-- · 2019-03-02 08:12

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);
查看更多
登录 后发表回答