当自定义Cell Renderer组件内部发生更改时,如何刷新Ag-Grid?

时间:2019-05-28 11:21:42

标签: ag-grid ag-grid-angular

在我的网格中,我有以下列:
enter image description here

  {
      headerName: "Generic/Specific",
      field: "genericspecific",
      id: "6",
      cellRenderer:'genericSpecificCellRenderersComponent',
      cellStyle: params => {
        const colors: string[] = ["red", "orange"];
        const genericSpecific: string[] = ["Generic", "Specific"];
        return {
          "background-color": colors[genericSpecific.indexOf(this.selectedOptions[params.node.id])]
        };
      }
    }

如您所见,它具有自定义的Cell Renderer。这是它的定义:

 @Component({
  selector: "app-generic-specific-renderers",
  template: `
    <hr />
    <select class="form-control" id='0' (change)="updateChosenValue($event.target); refresh(params)">
      <option >{{ selectedOptions[params.node.id] }}</option>
      <option >Specific</option>
      <option >Generic</option>
    </select>
    <hr />
  `
})
export class GenericSpecificCellRenderersComponent implements OnInit {
  updateChosenValue(event) {
    if (event.value==='Specific'){
      this.selectedOptions[this.params.node.id]='Specific'
    }
    else if (event.value==='Generic'){
      this.selectedOptions[this.params.node.id]='Generic'
     }
    }
  selectedOptions:string[]=[];
  constructor(private controlAssessmentData: ControlAssessmentService) {
    this.controlAssessmentData.currentSelectedOptions.subscribe(
      receivedSelectedOptions =>
        (this.selectedOptions = receivedSelectedOptions)
    );
  }
  ngOnInit() {}
  public params: any;
  public gridApi:any;

  // called on init
  agInit(params: any): void {
    this.params = params;
    this.gridApi= params.api;
  }
  // called when the cell is refreshed
  refresh(params: any): boolean {
    this.params = params;
    this.gridApi.refreshCells()
    console.log('this.params: ', this.params);
    return true;
  }
}

我想每次用户从下拉列表中选择一个选项时刷新网格。
我认为我的代码应该可以工作,因为刷新定义的网格时使用了相同的逻辑。
但是没有。
知道为什么吗?也许我该怎么解决?
谢谢!

2 个答案:

答案 0 :(得分:1)

更改选项时(如现在一样,不在refesh()中),请在将force选项设置为true的情况下运行api.refreshCells。 api.refreshCells({ force: true });

如果可以,还可以在调用中放入所需的行/列,以避免重绘整个网格。

答案 1 :(得分:0)

例如,调用组件渲染器“ mycomponentRenderer”上的刷新

 const rowNode = this.gridApi.getDisplayedRowAtIndex(1);
 rowNode.setDataValue('mycolumn', 'myvalue');
 this.gridApi.refreshCells({ columns: ['mycolumn'] });

和“ mycolumn”必须在“ 字段”属性中的列配置期间放入columnDefs中。

html:

html: <ag-grid-angular
        [rowData]="items"
        [columnDefs]="columnDefs"
        [defaultColDef]="defaultColDef"
        (gridReady)="onGridReady($event)"
        [frameworkComponents]="frameworkComponents">
</ag-grid-angular>

ts:

 this.columnDef[0] = {
            field: 'mycolumn',
            headerName: 'mycolumnHeader',
            cellRenderer: 'mycomponentRenderer',
}