我想将数据json绑定到包含列表项的片段。
UraianList.fragment.xml:
<core:FragmentDefinition
class="sapUiSizeCompact"
xmlns="sap.m"
xmlns:core="sap.ui.core">
<Dialog
noDataText="No Products Found"
title="Uraian"
search="handleSearch"
confirm="handleClose"
close="handleClose"
items="{
path: 'list>/'
}" >
<List
title="{list>Name}"
description="{list>ProductId}"
iconDensityAware="false"
iconInset="false"
type="Active" />
</Dialog>
</core:FragmentDefinition>
控制器:
listButton : function(oEvent){
this._oDialog = sap.ui.xmlfragment("com.taspen.acb.modules.Dosir.UraianList", this);
this._oDialog.setModel(this.getView().getModel());
this._oDialog.open();
var view = this;
var data = [
{"Name":"1","ProductId":"Atasan1"},
{"Name":"2","ProductId":"Atasan2"},
{"Name":"3","ProductId":"Atasan3"}
]
var oModel = new JSONModel(data);
view.getView().setModel(oModel, "list");
}
我想当我点击按钮(listButton函数)时,我打开片段并将数据绑定到列表,但数据没有显示。如何解决?
此致 波比
答案 0 :(得分:2)
您的代码有一些错误。
你已经以某种方式写了几乎正确的属性,但是在错误的控制中。
以下是更正后的代码:
片段:
<core:FragmentDefinition
class="sapUiSizeCompact"
xmlns="sap.m"
xmlns:core="sap.ui.core">
<Dialog id="UraianListDialog"> <!-- id provided so I dont have to create new Dialog Everytime -->
<content>
<List
noDataText="No Products Found"
title="Uraian"
search="handleSearch"
confirm="handleClose"
close="handleClose"
items="{
path: 'list>/'
}" >
<StandardListItem
title="{list>Name}"
description="{list>ProductId}"
iconDensityAware="false"
iconInset="false"
type="Active" />
</List>
</content>
</Dialog>
</core:FragmentDefinition>
控制器:
onInit: function() {
//Create Models in Init so they are not created everytime you open your dialog
var data = [
{"Name":"1","ProductId":"Atasan1"},
{"Name":"2","ProductId":"Atasan2"},
{"Name":"3","ProductId":"Atasan3"}
];
var oModel = new sap.ui.model.json.JSONModel(data);
this.getView().setModel(oModel, "list");
},
listButton : function(oEvent){
var oView = this.getView();
var oDialog = oView.byId("UraianListDialog");
// create dialog lazily
if (!oDialog) {
// create dialog via fragment factory
oDialog = sap.ui.xmlfragment(oView.getId(), "com.taspen.acb.modules.Dosir.UraianList");
oView.addDependent(oDialog);
}
oDialog.open();
}
}
答案 1 :(得分:-1)
您不能在JSONModel数据的根目录下拥有数组
此外,您应该将您的片段添加为视图的依赖项,并且您不需要为片段显式设置模型:)
查看更新:
<core:FragmentDefinition
class="sapUiSizeCompact"
xmlns="sap.m"
xmlns:core="sap.ui.core">
<Dialog
noDataText="No Products Found"
title="Uraian"
search="handleSearch"
confirm="handleClose"
close="handleClose"
items="{list>/items}" >
<List
title="{list>Name}"
description="{list>ProductId}"
iconDensityAware="false"
iconInset="false"
type="Active" />
</Dialog>
</core:FragmentDefinition>
将您的控制器更改为:
listButton : function(oEvent){
var data = {
items: [
{"Name":"1","ProductId":"Atasan1"},
{"Name":"2","ProductId":"Atasan2"},
{"Name":"3","ProductId":"Atasan3"}
]
}
var oModel = new JSONModel(data);
this.getView().setModel(oModel, "list");
this._oDialog = sap.ui.xmlfragment("com.taspen.acb.modules.Dosir.UraianList", this);
this.getView().addDependent(this._oDialog);
this._oDialog.open();
}