Using model for ID assignment

北战南征 提交于 2019-12-19 04:39:11

问题


I tried to assign ID to items in my select control using model binding, but ended with an error.

Here is my code:

Model:

{
    "languages": [
        {
        "language": "English",
        "locale": "en"
        }, 
        {
        "language": "German",
        "locale": "de"
        }
    ]
}

Select control:

var oLanguageSelection = new sap.m.Select({
            name:   'languageSelection',
            id:     'languageSelection',
            items: { 
                path: "languages>/languages",
                template: new sap.ui.core.Item({
                    id: "{languages>locale}", //this is the problematic part
                    text: "{languages>language}",
                }),

            },
});

Error I get:

Uncaught Error: "{languages>locale}" is not a valid ID.

Is it possible at all to do what I am trying to do - assign element ID using model binding?

Thank you.


回答1:


I think id is not bindable. ID refers to the control id that is also used in the DOM. What you are trying to do should be achieved with the key property :)

var oLanguageSelection = new sap.m.Select({
    name:   'languageSelection',
    id:     'languageSelection',
    selectedKey: "{languages>/languages/0/locale}", // you might want to set a default selection
    items: { 
        path: "languages>/languages",
        template: new sap.ui.core.Item({
            key: "{languages>locale}", // no longer problematic ;)
            text: "{languages>language}"
        })  
    }
});


来源:https://stackoverflow.com/questions/27274452/using-model-for-id-assignment

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