我将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'>`;
}
答案 0 :(得分:7)
我在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);
如果要选择标题,并且在数据加载时选择了所有过滤的行,请使用以下源。实际上,我已经为要过滤的行(assignmentId> 0)向setSelected(true)添加了RowDataChange侦听器:
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);
新来源的输出: