我有一个JS视图,我在其中创建一个sap.m.Table。它"列"绑定到JSON模型。它"项目"必然会有odata服务。我有两个问题,我现在已经苦苦挣扎了一段时间。
问题1 - 如何在columnlistitem内点击图标时删除表格的行?
问题2 - 我为此创建了另一个问题 - 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"在清单文件中定义。
我的控制器代码 -
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();
}
}
我尝试了其他几种方法,但没有运气。看起来我还不完全理解绑定。
答案 0 :(得分:1)
请尝试使用
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);
}
感谢。