How can I select all checkboxes from outside the grid

I have a grid with a column checkbox.I want to be able to select all checkboxes, from outside the grid, when I click another checkbox. I try this but it doesn't seems to work.

Thank you in advance!





11 Replies

SK Sujith Kumar Rajkumar Syncfusion Team March 2, 2022 05:40 AM UTC

Hi Dana, 
 
Greetings from Syncfusion support. 
 
You can achieve your requirement of performing the header checkbox’s check all/uncheck all action externally by using the Grid’s checkSelectAll internal method as demonstrated in the below code snippet, 
 
onClick() { 
    this.gridObj.selectionModule.checkSelectAll() 
} 
 
We have prepared a sample based on this for your reference. You can find it below, 
 
 
Please get back to us if you require any further assistance. 
 
Regards, 
Sujith R 



DS Dana Sarghe replied to Sujith Kumar Rajkumar March 2, 2022 06:15 PM UTC

Hello,

Thank you for your answer, but still doesn't work with checkbox.(

<ejs-checkbox label="Selectează toate matricolele" (click)="selectAll()"></ejs-checkbox>

)

Works only with button.(

<button (click)='selectAll()'>Select/Unselect all</button>

)

And my requirements are whith a checkbox, as I said before.



SK Sujith Kumar Rajkumar Syncfusion Team March 3, 2022 10:01 AM UTC

Hi Dana, 
 
The previously shared solution is not working for your case because the EJ2 Checkbox does not have click event. So we suggest you to use the change event instead to achieve your requirement. 
 
We have modified the previously shared sample based on this for your reference. You can find it below, 
 
 
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 



DS Dana Sarghe March 3, 2022 07:22 PM UTC

Thank you!

This solution works just fine! 



SK Sujith Kumar Rajkumar Syncfusion Team March 4, 2022 06:59 AM UTC

Hi Dana, 

You’re welcome. We are glad to hear that your query has been resolved. 

Regards, 
Sujith R 



DS Dana Sarghe March 4, 2022 09:39 PM UTC

Hello,
One more thing...:)

If the SelectAll checkbox is checked  (the one that's outside of the grid) , and after that one of the row in the grid is unchecked, I want the SelectAll checkbox to be in the "indeterminate" state, like the one in the grid header.

Now it remain checked.

Thank you!





SK Sujith Kumar Rajkumar Syncfusion Team March 7, 2022 10:23 AM UTC

Hi Dana, 
 
You can achieve your requirement of customizing the external checkbox state based on the Grid header check state by using the Grid’s rowSelected and rowDeselected events to modify the check state(by comparing the selected record length to the local data source length) as demonstrated in the below code snippet, 
 
// Grid’s rowSelected event handler 
rowSelected(args) { 
    this.updateExternalCheckState(); 
} 
 
// Grid’s rowDeselected event handler 
rowDeselected(args) { 
    this.updateExternalCheckState(); 
} 
 
// Function for handling external header state 
updateExternalCheckState() { 
    if (this.gridObj.getSelectedRecords().length === this.gridObj.dataSource.length) { 
        // Condition executes when all the records are selected 
        this.checkObj.checked = true; 
    } else if (this.gridObj.getSelectedRecords().length === 0) { 
        // Condition executes when all the records are un-selected 
        this.checkObj.checked = false; 
    } else { 
        // Condition executes when one or more records are selected 
        this.checkObj.indeterminate = true; 
    } 
} 
 
We have modified the previously shared sample based on this for your reference. You can find it below, 
 
 
 
Let us know if you have any concerns. 
 
Regards, 
Sujith R 



DS Dana Sarghe March 7, 2022 07:25 PM UTC

Hello,

I have a problem with "lenght" property.
What can I do?





PS Pavithra Subramaniyam Syncfusion Team March 8, 2022 11:54 AM UTC

Hi Dana, 
 
We suggest setting the type for the dataSource as “Object[]” to overcome the issue. Please refer to the below code example for more information. 
 
updateExternalCheckState() { 
    if ( 
      this.gridObj.getSelectedRecords().length === 
      (this.gridObj.dataSource as Object[]).length 
    ) { 
      this.checkObj.checked = true; 
    } else if (this.gridObj.getSelectedRecords().length === 0) { 
      this.checkObj.checked = false; 
    } else { 
      this.checkObj.indeterminate = true; 
    } 
  } 
 
 
Please get back to us if you need further assistance on this. 
 
Regards, 
Pavithra S 



DS Dana Sarghe March 8, 2022 06:35 PM UTC

Thank you!

Exactly what I needed!



SK Sujith Kumar Rajkumar Syncfusion Team March 9, 2022 06:50 AM UTC

Hi Dana, 
 
Thanks for the update. We are glad to hear that your query has been resolved. 
 
Regards, 
Sujith R 


Loader.
Up arrow icon