首页 > 解决方案 > 渲染器检查的 ag-grid 复选框列

问题描述

我正在使用带有复选框标题组件的 ag-grid 来允许选择/取消选择所有行。现在,当我获取网格数据时,我想根据某些字段值选中/取消选中复选框列。我尝试了一个渲染器,但它不适用于下面的代码。如果我从列定义中删除“checkboxSelection”,它会完美运行。

  this.gridClientOptions = {
  enableFilter: true,
  enableSorting: true,
  enableColResize: true,
  pagination: true,
  paginationPageSize: 5,
  rowSelection: 'multiple',
  suppressRowClickSelection: true,
  columnDefs: [
      {
        headerName: '',
        width: 40,
        headerCheckboxSelection: true,
        headerCheckboxSelectionFilteredOnly: true,
        checkboxSelection: true,
        cellRenderer: 'selectedClient'
    },
    {headerName: 'Client Code', field: 'clientCode'},
    {headerName: 'Client Name', field: 'clientName'},
    {headerName: 'Group Name', field: 'groupName'},
  ],
  components: {
    'selectedClient': this.selectedClient
  },
  getRowStyle: function(params) {
    if (params.node.rowIndex % 2 === 0) {
        return { background: '#dfffdf' }
    }
  }      
}

selectedClient(params) {
return params.data.assignmentId > 0 ? `<input type='checkbox' checked>` : `<input type='checkbox'>`;
  }

标签: angularag-grid

解决方案


我在https://embed.plnkr.co/awEnTpOLOuXDereXrYi0/为您构建了一个成功可行的项目。

正如您在下面看到的那样,我删除了 headerCheckboxSelection、headerCheckboxSelectionFilteredOnly 和 checkboxSelection,并使 cellRenderer 内联实现而不是 selectedClient,并且一切正常。

var data = [
  {'clientCode':1,'clientName':'client1', 'groupName' : 'groupA', 'assignmentId':1},
  {'clientCode':2,'clientName':'client2', 'groupName' : 'groupA', 'assignmentId':0},
  {'clientCode':3,'clientName':'client3', 'groupName' : 'groupA', 'assignmentId':1},
  ];

var gridOption = {
  enableFilter: true,
  enableSorting: true,
  enableColResize: true,
  pagination: true,
  paginationPageSize: 5,
  rowSelection: 'multiple',
  suppressRowClickSelection: true,
  rowData: data,
  columnDefs: [
      {
        headerName: '',
        width: 40,
        editable: true,
        cellRenderer: params => {
          return `<input type='checkbox' ${params.value ? 'checked' : ''} />`;
        },
        field: 'assignmentId'
    },
    {headerName: 'Client Code', field: 'clientCode'},
    {headerName: 'Client Name', field: 'clientName'},
    {headerName: 'Group Name', field: 'groupName'},
  ],
  getRowStyle: function(params) {
    if (params.node.rowIndex % 2 === 0) {
        return { background: '#dfffdf' }
    }
  }      
};


var mygrid = new agGrid.Grid(document.querySelector('#myGrid'),gridOption);

在此处输入图像描述

如果您想要一个标题选择,并且在加载数据时选择所有过滤的行,请使用以下源。实际上,我已经为要过滤的行添加了一个 RowDataChange 侦听器 setSelected(true) (assignmentId > 0):

var data = [
  {'clientCode':1,'clientName':'client1', 'groupName' : 'groupA', 'assignmentId':1},
  {'clientCode':2,'clientName':'client2', 'groupName' : 'groupA', 'assignmentId':0},
  {'clientCode':3,'clientName':'client3', 'groupName' : 'groupA', 'assignmentId':1},
  ];

var gridOption = {
  enableFilter: true,
  enableSorting: true,
  enableColResize: true,
  pagination: true,
  paginationPageSize: 5,
  rowSelection: 'multiple',
  suppressRowClickSelection: true,
  rowData: data,
  columnDefs: [
      {
        headerName: '',
        width: 40,
        editable: true,
        headerCheckboxSelection: true,
        headerCheckboxSelectionFilteredOnly: false,        
        checkboxSelection: true,
    },
    {headerName: 'Client Code', field: 'clientCode'},
    {headerName: 'Client Name', field: 'clientName'},
    {headerName: 'Group Name', field: 'groupName'},
  ],
  getRowStyle: function(params) {
    if (params.node.rowIndex % 2 === 0) {
        return { background: '#dfffdf' }
    }
  },
  onRowDataChanged: event => {
    event.api.forEachNode( function(rowNode, index) {
      if(rowNode.data.assignmentId > 0)
        rowNode.setSelected(true);
    });
  } 
};
this.isRowSelectable = function(rowNode) {
      return rowNode.data ? rowNode.data.year < 2007 : false;
    };

var mygrid = new agGrid.Grid(document.querySelector('#myGrid'),gridOption);

新来源的输出:

在此处输入图像描述


推荐阅读