[英]SAP UI5 - Cannot delete selected row from table
我有一個帶有單選模式表的視圖,它的工具欄中有一個按鈕可以刪除所選行。
雖然當我按下按鈕時,它會刪除所有行。
我的代碼:
查看文件:
<template data-controller-name="myapplication.myview2">
<div data-sap-ui-type="sap.ui.table.Table" id="tb1" data-width="100%" data-title="Person Table"></div>
</template>
控制器文件:
onInit: function() {
try {
var oTab = [
// the table content
];
var oToolbar = new sap.ui.commons.Toolbar();
oToolbar.addItem(new sap.ui.commons.Button({text: "Delete selected row",
press: function() {
try {
var newTab = this.getParent().getParent();
var index = newTab.getSelectedIndex();
if (index == -1)
alert("No row selected");
else {
var currModel = newTab.getModel();
var selectedRow = newTab.getRows()[index];
newTab.removeRow(selectedRow);
currModel.setData({table: newTab});
newTab.bindRows("/table");
}
} catch (err) {
txt = "There was an error on this page.\n\n";
txt += "Error description: " + err.message + "\n\n";
txt += "Click OK to continue.\n\n";
alert(txt);
}
}}));
this.byId("tb1").setToolbar(oToolbar);
this.byId("tb1").setVisibleRowCount(5);
this.byId("tb1").setNavigationMode(sap.ui.table.NavigationMode.Paginator);
// Columns definition should be HERE
var oModel = new sap.ui.model.json.JSONModel();
oModel.setData({table: oTab});
this.byId("tb1").setModel(oModel);
this.byId("tb1").bindRows("/table");
} catch (err) {
txt = "There was an error on this page.\n\n";
txt += "Error description: " + err.message + "\n\n";
txt += "Click OK to continue.\n\n";
alert(txt);
}
},
// More functions....
有什么想法嗎?
您需要從模型中刪除行,而不是直接從表中刪除行。
在刪除按鈕的按下功能中,獲取有關表的更多詳細信息:
var tableIndex = newTab.getSelectedIndex();
var context = newTab.getContextByIndex(tableIndex);
var path = context.getPath();
然后,在變量路徑中,您將找到與表行索引相對應的數據索引。 使用此數據索引從模型中刪除行。
currModel.oData.table.splice(data_index, 1);
之后,所需要做的就是刷新模型,以通知控件有關已更改的數據。 而且,對於用戶來說,如果表中的選擇也被重置,那也可能很好。
currModel.refresh();
newTab.setSelectedIndex(-1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.