1

I would like to use a checkbox in the header of ag-grid for Select All option. The row model type I'm using is infinite. I guess this model doesn't support headerCheckboxSelection=true.

How else I can use a checkbox inside the header cell?

I will be making a service call on click of this checkbox for further processing.

This is how i am configuring the table:

this.columnDefs = [
      {
       headerCheckboxSelection:true
       checkboxSelection: true,
       width: 40,
      },
      {
        headerName: "Date/Time",
        field: "createdDate",
        width: 230
      },
      {headerName: 'File Name', field: 'fileName', width: 240},
      {headerName: 'Count', field: 'count', width: 100}
    ];

    this.gridOptions = {
      rowSelection: 'multiple',
      cacheBlockSize: 30,
      rowModelType: 'infinite',
      paginationAutoPageSize: true,
      suppressRowClickSelection: true
    };
UI Dev
  • 167
  • 1
  • 4
  • 13

1 Answers1

0

1. In Column defs, add checkbox column:

columnDefs: [
    {
        headerName: '',
        width: 40,
        field: 'checkbox',
        width: 50,
        checkboxSelection: true,
        showDisabledCheckboxes: true
    }
]

2. Custom Checkall checkbox:

<script>
    $(document).ready(function () {
        //--Add CheckAll Checkbox
        let checkAllHtml = '<div ref="eWrapper" class="ag-wrapper ag-input-wrapper ag-checkbox-input-wrapper divCheckAll" id="divCheckAll" role="presentation"><input ref="eInput" class="ag-input-field-input ag-checkbox-input" type="checkbox" id="checkAllCheckbox" tabindex="-1"></div>';
        $('.ag-header-cell-label').eq(0).append(checkAllHtml);

        $(document).on('click', '#checkAllCheckbox', function () {
            let checking = 'checking';
            let checkClass = "ag-checked";
            $('#divCheckAll').toggleClass(checkClass);

            var gridOptionApi = gridOptions.api;

            if ($(this).hasClass(checking)) {
                $(this).removeClass(checking);
                gridOptionApi.forEachNode(function (rowNode) {
                    rowNode.setSelected(false);
                });
            } else {
                $(this).addClass(checking);
                gridOptionApi.forEachNode(function (rowNode) {
                    rowNode.setSelected(true);
                });
            }
        });
    });
</script>
O Thạnh Ldt
  • 1,103
  • 10
  • 11