角度材质2预选垫子表行

时间:2018-02-26 19:09:22

标签: angular angular-material2

我正在尝试使用角度材质表预选行。 initialSelection中有SelectionModelexample。但是没有确切的解释它是如何工作的。是否可以使用此initialSelection来预选某些行?

编辑1:

看起来,initialSelection需要是作为dataSource传递给mat-table的数据数组的子集。它现在正在运作。

编辑2:

Updated官方例子。查看app/table-selection-example.ts21 - 25

  subSet1 = this.dataSource.data.slice(0,2);
  subSet2 = this.dataSource.data.slice(3,5);
  preselectExample = this.subSet1.concat(this.subSet2);

  selection = new SelectionModel<Element>(true, this.preselectExample);

1 个答案:

答案 0 :(得分:1)

因此,在与表进行了长时间的争斗之后,我做了很多选择,比对数组进行切片和切块要好一些。

this.initialSelection = this.data.filter((element,e) => {
     return this.savedSelection.some( (val,i) => { 
         if(element.key === val.key) { 
             this.data[e]['amount'] = val['amount']; 
             return true;
         }
     return;
  });
});
this.dataSource.data = this.data;
this.selection = new SelectionModel<any>(true, this.initialSelection);

对于可编辑值还有一个额外的功能,以后会将选定的值分配回表中。

干杯。

https://stackblitz.com/edit/angular-urd5c4

相关问题