通过选择选项进行过滤时,添加数据而不是替换数据表中的数据

时间:2018-11-27 11:34:20

标签: angular datatable

我有一个数据表,我正在尝试通过选择选项实现过滤。但是,每次我更改选项时,该特定选项的数据都会添加到先前的选择选项中。考虑选项1显示来自筛选的5个数据,选项2显示2个数据。最初将有5个数据,但是当我单击第二个选项时,这2个数据将添加到之前的5个数据中;如果我然后更改为选项1,它将再次将5个数据添加到现有的7个数据中。该如何解决?

  

component.ts

// plot metric master table
metricMasterTable(){
this.dtOptions = { 
  "destroy": true, 
  "scrollY": '280',
  "scrollX": true,
  data: this.metric_master_data,
  columns: [{
    title: '#',
    data: 'id',
    } ,{
    title: 'Metric Name',
    data: 'metric_name',  
  },{
    title: 'Formula',
    data: 'formula',  
  },{
    title: 'Target Red',
    data: 'target_red',  
  },{
    title: 'Target Amber',
    data: 'target_amber',  
  },
  {
    title: 'Target Green',
    data: 'target_green',  
  },{
    title: 'Remarks',
    data: 'remarks',  
  },{
    title: 'Action',
    className: "control",
    width: '100px',
    "render": function ( flag, a, r ) {
      return `<button class="btn btn-edit"> 
                <i class="fas fa-pencil-alt"></i>
              </button> 
              <button class="btn btn-delete">
                <i class="far fa-trash-alt"></i>
              </button>`;
    },
  }],
  // Function for inline click
  rowCallback: (row: Node, data: any[] | Object, index: number) => {
    const self = this;
    $('td button.btn-edit', row).unbind('click');
    $('td button.btn-edit', row).bind('click', () => {
      this.openEditMasterModal(data);
    });

    $('td button.btn-delete', row).unbind('click');
    $('td button.btn-delete', row).bind('click', () => {
      this.deleteMetricMaster(data);
    });
    return row;
    }
  };
 }

// Fetch data for master table and dropdown
fetchData() {
this.metricService.lifecycle().subscribe((data: any,) => {
  for(let val of data) {
    this.lifecycle_dropdown.push(val);
  }
  this.selectedLifecycle = this.lifecycle_dropdown[0]['id']
  this.onFilter(this.selectedLifecycle)
});
}

onFilter(id) {
console.log(id);
this.metricService.fetch_metric_master(id).subscribe((res: any,) => {
  console.log(res);
  for(let val of res){

    this.metric_master_data.push(val);

  }

    console.log(this.metric_master_data);
  this.dtTrigger.next();
  });
  }

0 个答案:

没有答案