我一直在玩SapUI5一段时间,现在我正面临另一个问题,这让我很困惑。
我的目标:将一个ComboBox添加到oTable。
我尝试了什么:我决定进行“关注点分离”,以便更好地进行调查,因此我从表中提取了“ComboBox代码”并自行测试像这样:
var cbWizardTypes = [
{
Code: "0",
Name: "Name0",
AdditionalText: "Additional0"
},
{
Code: "1",
Name: "Name1",
AdditionalText: "Additional1"
},
{
Code: "2",
Name: "Name2",
AdditionalText: "Additional2"
},
];
// now the template to use when showing the items
var cbWizardTypesTemplate = new sap.ui.core.ListItem({
key: "{Code}",
text: "{Name}",
additionalText: "{AdditionalText}"
});
// now let's create it and place it
var cbWizardType = new sap.m.ComboBox({
items: {
path: cbWizardTypes,
template: cbWizardTypesTemplate
},
showSecondaryValues: true
});
cbWizardType.placeAt(containerID, 'only');
此外,我尝试不使用模板,只是为了看看会发生什么
var cbWizardType = new sap.m.ComboBox({
//items: {
// path: cbWizardTypes,
// template: cbWizardTypesTemplate
//},
items: cbWizardTypes,
showSecondaryValues: true
});
在这种情况下,Chrome开发者工具 - 控制台中没有错误。我得到一个包含3个项目的ComboBox,但它们都是空白的。
现在,我将至少尝试进一步调查,尽管library-preload.js已被缩小,因此浏览所有这些'd','p'将非常困难和耗时。 -s,'j'-s等,我想。
一如既往,我将不胜感激。谢谢!
答案 0 :(得分:1)
问题在于您分配给ComboBox的绑定路径。绑定应该是一个字符串&不是一个数组。您必须将数据存储在模型和模型中。然后将它绑定到您的控件。 下面的代码应该可以使用
var cbWizardTypes = [
{
Code: "0",
Name: "Name0",
AdditionalText: "Additional0"
},
{
Code: "1",
Name: "Name1",
AdditionalText: "Additional1"
},
{
Code: "2",
Name: "Name2",
AdditionalText: "Additional2"
},
];
var oModel = new sap.ui.model.json.JSONModel({ items: cbWizardTypes});
// now the template to use when showing the items
var cbWizardTypesTemplate = new sap.ui.core.ListItem({
key: "{Code}",
text: "{Name}",
additionalText: "{AdditionalText}"
});
// now let's create it and place it
var cbWizardType = new sap.m.ComboBox({
items: {
path: "/items",
template: cbWizardTypesTemplate
},
showSecondaryValues: true
});
cbWizardType.setModel(oModel);
cbWizardType.placeAt(containerID, 'only');