将新项目添加到列表 [英] Add New Item to List
问题描述
我正在使用 SAPUI5,我想在按下按钮时动态添加一个项目.我有一个 XML 视图和一个 XML 模型.模型正确显示为列表,我有一个表单来添加新项目.
I am working with SAPUI5 and I want to add an item dynamically when pressing a button. I have an XML View and an XML Model. The model is being displayed as an List correctly and I have a form to add a new item.
我想将表单条目添加到列表中.由于我的模型是 XML 模型,因此我尝试制作另一个 JSONModel 但它不起作用.
I want to add the form entries to the List. As my model is an XML Model I have tried making another JSONModel but it doesn't work.
<Page>
<Button text="Siguiente" press=".onPress"></Button>
<List headerText="Productos" items="{/book}">
<ObjectListItem title="{title}"
type="Active"
press=".handlePressItem">
</ObjectListItem>
</List>
<form:SimpleForm id="f1" layout="ResponsiveLayout">
<!-- here are the form fields -->
<Button id="Add"
text="Agregar"
press=".addItem" >
</Button>
</form:SimpleForm>
</Page>
控制器
onInit: function() {
var oModel = new sap.ui.model.xml.XMLModel();
oModel.loadData("model/sampleData.xml");
sap.ui.getCore().setModel(oModel);
},
推荐答案
您可以使用 .setProperty()
方法来完成.这里 是 API 的链接.
You may do it using .setProperty()
method. Here is a link to the API.
作为一种选择,您可以遍历表单的字段,从中读取数据并更新模型,如下所示:
As an option, you may loop over the fields of the form, read data from them and update the model as follows:
addItem: function(){
var oModel = sap.ui.getCore().getModel();
oModel.setProperty("/<key1_path>", sap.ui.getCore().byId(this.createId("<relevat_field's_id>").getValue());
oModel.setProperty("/<key2_path>", sap.ui.getCore().byId(this.createId("<relevat_field's_id>").getValue());
...
oModel.refresh();
}
这篇关于将新项目添加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!