问题描述
我有一个JS视图,其中正在创建一个sap.m.Table.它的列"绑定到JSON模型.它的项目"绑定到odata服务.我有两个问题已经困扰了一段时间了.
I have a JS view in which I am creating a sap.m.Table. It's "columns" are bound to a JSON model. It's "items" are bound to odata service. I have two issues I have been struggling with for a while now.
问题1-如何通过单击columnlistitem中的图标来删除表中的行?
问题2-我为此创建了另一个问题-
Issue 1 - How to delete the row a table on click of an icon inside columnlistitem?
Issue 2 - I have created another question for this - How to access row and column data on click of an icon in ColumnListItem
我的视图如下.
createContent : function(oController) {
var oTable = new sap.m.Table("table1", {
width: "auto",
noDataText: "Please add rows to be displayed!"
}).addStyleClass("sapUiResponsiveMargin");
oTable.bindAggregation("columns", "Periods>/periods", function(sId, oContext) {
var sColumnId = oContext.getObject().period;
return new sap.m.Column({
hAlign: "Center",
vAlign: "Middle",
header: new sap.m.Text({
text: sColumnId
})
});
});
oTable.bindItems("zStatus>/StatusSet", function(sId, oContext) {
var row = new sap.m.ColumnListItem(sId, {
type : "Inactive",
cells: [
new sap.ui.core.Icon({
src: "sap-icon://delete",
hoverColor: "red",
activeColor: "red",
press: [oController.onDeleteIconPress, oController]
}),
new sap.m.Text({
text: "{zStatus>Description}"
}),
new sap.ui.core.Icon(sId, {
src: {
path: "zStatus>Status1",
formatter: function(status) {
switch(status) {
case "R":
return "sap-icon://sys-cancel";
case "G":
return "sap-icon://sys-enter";
case "Y":
return "sap-icon://notification";
default:
return "sap-icon://sys-help";
}
}
},
size: "1.5em",
press: [oController.onStatusIconPress, oController]
}) ]
});
return oTable;
}
在我的控制器中,我创建一个数组,然后从中创建一个JSON模型"Periods",并将其设置为该视图.清单文件中定义了Odata模型"zStatus".
In my controller I create an array, then a JSON model "Periods" from it and set it to this view. Odata model "zStatus" is defined in manifest file.
我的控制器代码-
onInit : function() {
// array aPeriods is populated first then
var oPeriodsModel = new sap.ui.model.json.JSONModel();
oPeriodsModel.setData({
periods : aPeriods
});
this.getView().setModel(oPeriodsModel, "Periods");
}
onDeleteIconPress : function(oEvent) {
// I manage to get the selected row
var oSelectedRow = oEvent.getSource().getParent().getBindingContext("zStatus").getObject();
var oOriginalRows = oTable.getBinding("items").getModel().getProperty("/");
var found = false, i;
for (var key in oOriginalRows) {
var oOriginalRow = oOriginalRows[key];
if(oOriginalRow.Name === oSelectedRow.Name) {
delete oOriginalRows[key];
found = true;
break;
}
}
if(found) {
// Problem 1 : Even though I delete row from the model it is still shown on the UI
oTable.getBinding("items").getModel().setProperty("/", oOriginalRows);
oTable.getBinding("items").getModel().refresh();
}
}
我尝试了其他几种方法,但是没有运气.看来我还不完全了解绑定.
I tried several other ways but no luck. Looks like I do not understand binding completely yet.
推荐答案
请尝试使用
oTable.getBinding("items").getModel().refresh(true);
这是我要完成相同任务的方式
Here is how I would achieve the same task
onDeleteIconPress : function(oEvent) {
//get the index of the selected row
var array = oEvent.oSource.sId.split('-')
var index = array[array.length - 1];
//get the model
this.getModel("Periods").YourListItemsOBJECTorArray.splice(index, 2);
//refresh the model to let the UI know that there's change in the data.
sap.ui.getCore().getModel("Periods").refresh(true);
}
谢谢.
这篇关于单击ColumnListItem内的图标后如何删除sap.m.Table行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!