Json 如何在单击ColumnListItem内的图标时删除sap.m.Table的行?
我有一个JS视图,我正在其中创建一个sap.m.Table。它的“列”绑定到JSON模型。它的“项目”绑定到odata服务。我有两个问题,我已经挣扎了一段时间了 问题1-如何在单击columnlistitem内的图标时删除表中的行a?Json 如何在单击ColumnListItem内的图标时删除sap.m.Table的行?,json,odata,sapui5,Json,Odata,Sapui5,我有一个JS视图,我正在其中创建一个sap.m.Table。它的“列”绑定到JSON模型。它的“项目”绑定到odata服务。我有两个问题,我已经挣扎了一段时间了 问题1-如何在单击columnlistitem内的图标时删除表中的行a? 第2期-我为此提出了另一个问题- 我的观点是这样的 createContent : function(oController) { var oTable = new sap.m.Table("table1", { width: "a
第2期-我为此提出了另一个问题- 我的观点是这样的
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”在清单文件中定义
我的控制器代码-
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();
}
}
我尝试了其他几种方法,但没有成功。看起来我还不完全理解绑定。请尝试使用
oTable.getBinding("items").getModel().refresh(true);
下面是我将如何完成同样的任务
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);
}
谢谢。谢谢美达。这是行不通的。我的图标被命名为“\uuuicon0”。然而,我设法使用正则表达式获得索引。但我的模型中不是数组,而是对象的对象。因此,我不得不做循环。即使使用我的代码,我也看到该行已从模型中删除,并且由于再次调用bindItems,刷新也起到了作用。令人惊讶的是,这里的模型更改并没有反映和渲染所有的线条。我不确定您是否收到了通知。哦,好的。可能您可以尝试取消绑定项(),然后再次尝试重新绑定?只是一个想法。那也行不通。我找到了背后的原因。ODataModel是服务器端。由于我更改了模型,它会触发后端往返并再次获取原始数据。我有一个解决办法。如果它工作,我会张贴在这里