Reputation: 87
I am using ag-grid with checkbox header component to allow select/deselect all rows. Now when I fetch the data for grid, I want to check/uncheck checkbox column on based of some field value. I tried a renderer but it's not working with below code. If I remove 'checkboxSelection' from the column definition, it works perfectly.
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'>`;
}
Upvotes: 1
Views: 21543
Reputation: 1084
I built a successfully workable project for you at https://embed.plnkr.co/awEnTpOLOuXDereXrYi0/ .
As your can see in below, I removed headerCheckboxSelection, headerCheckboxSelectionFilteredOnly and checkboxSelection, and make cellRenderer inline implementation instead of selectedClient and all the thins goes fine.
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);
If you want a header selection, and when data loaded all filtered row be selected, use the below source. Actually I've added a RowDataChange listener to setSelected(true) for the row that I want to filter (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);
Output of new source :
Upvotes: 12