【问题标题】:How to delete multiple selected rows from array in angular如何以角度从数组中删除多个选定的行
【发布时间】:2021-04-08 04:50:36
【问题描述】:

我有表格,我想在单击按钮时删除多个选定的行。单击按钮时我在数组下方。数组的 PFA..

【问题讨论】:

  • 你在html中使用ngFor来显示数组吗
  • 请添加该组件的代码以便理解你已经做了什么?
  • 不,我在 html 中没有任何循环,我只在 HTML 中粘贴了按钮。我从 tableModel 中获取选定的行值,如下代码 const selectedValueIndex = Object.keys(this.staticValuemodel.rowsSelected);
  • 我正在粘贴 this.staticValuemodel.rowsSelected 的输出

标签: javascript angular angular8


【解决方案1】:
deleteAllRows() {         
    this.staticValuemodel.rowsSelected.forEach(key => {
      this.StaticData.records.splice(key, 1);      
    });  
    this.populateStaticTable(this.StaticData.records);  

  }

由于您的 this.staticValuemodel.rowsSelected 输出图片是对象还是数组,因此令人困惑。(最好使用 google chrome 控制台发送屏幕截图)

【讨论】:

  • 它是一个包含多个对象的数组
  • 分享一下它包含什么属性
【解决方案2】:

有些喜欢:

//get an array with the index of elements selected, e.g. [0,3]
const rowsSelects=Object.keys(this.staticValuemodel.rowsSelected); 

//filter the data
this.StaticData.records=this.StaticData.records
   .filter((x,index)=>rowSelects.indexOf(index)<0)

是的,过滤器可以有一个“第二个参数”,即元素的索引

【讨论】:

    【解决方案3】:

    您的循环应该基于 Id 并在其循环中找到索引,然后拼接数组元素。

    应该像下面这样

    【讨论】:

    • 我已经编辑了我的问题并再次粘贴了所有代码..你可以再次审查..是的,你是对的,它删除了错误的键
    猜你喜欢
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2022-11-18
    相关资源
    最近更新 更多