首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >删除多行(sap.ui.table.Table)

删除多行(sap.ui.table.Table)
EN

Stack Overflow用户
提问于 2018-01-25 04:47:13
回答 1查看 3K关注 0票数 0

我在SAPUI5表上工作,我已经创建了一个从表中删除选定行的功能,但当我选择删除多个行时,它删除了错误的行。虽然第一个选定行被正确删除,但其他行却没有被删除。

代码语言:javascript
运行
复制
fDeleteRow: function(oEvent) {
    var oTable = this.getView().byId("tbl");
    var data = oTable.getModel();
    var selRowCount = oTable._oSelection.aSelectedIndices.length;
    var Flag = false;
    for (var i = 0; i < selRowCount; i++) {
        var rowNum = oTable._oSelection.aSelectedIndices[i];
        data.oData.splice(rowNum, 1);
        data.oData.refresh();
        Flag = true;
    }
    if (Flag) {
        var oModelJson = new sap.ui.model.json.JSONModel();
        oModelJson.setData(data.oData);
        oTable.unbindColumns();
        oTable.unbindRows();
        oTable.setModel(oModelJson);
        oTable.bindRows("/");
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-25 06:55:12

下面是一个http://jsbin.com/ducuyah/edit?html,js,output示例

您需要先反转选定的索引,然后才能进行拼接。请不要在表中使用私有成员

代码语言:javascript
运行
复制
var oTable = new sap.ui.table.Table({
  rows: '{/d/results}',
  title: new sap.m.Button({
    icon: 'sap-icon://delete',
    press: function() {
      var oModel = oTable.getModel();
      var oData = oModel.getProperty('/d/results');
      var reverse = [].concat(oTable.getSelectedIndices()).reverse();
      reverse.forEach(function(index) {
        oData.splice(index, 1);
      });
      oModel.refresh();
      oTable.setSelectedIndex(-1);
    }
  }),
  columns: [
    new sap.ui.table.Column({
      width: '100px',
      label: new sap.ui.commons.Label({text: "District Name"}),
      template: new sap.ui.commons.TextView({text:"{District}"})
    }),
    new sap.ui.table.Column({
      width: '80px',
      label: new sap.ui.commons.Label({text: "County"}),
      template: new sap.ui.commons.TextView({text:"{County}"})
    })]
});


var model = new sap.ui.model.json.JSONModel({
  d: {
    results: [
      { District: "D1", County: "C1"},
      { District: "D2", County: "C2"},
      { District: "D3", County: "C3"},
      { District: "D4", County: "C4"},
      { District: "D5", County: "C5"},
      { District: "D6", County: "C1"},
      { District: "D7", County: "C2"},
      { District: "D8", County: "C3"},
      { District: "D9", County: "C4"},
      { District: "D5", County: "C5"},
      { District: "D10", County: "C1"},
      { District: "D11", County: "C2"},
      { District: "D12", County: "C3"},
      { District: "D13", County: "C4"},
      { District: "D14", County: "C5"},
    ] 
  }
});

oTable.setModel(model);
oTable.placeAt('content');
oTable.setFirstVisibleRow(10);
oTable.setSelectedIndex(10);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48431321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档