We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

manually check/uncheck checkbox in treegrid

Hi Syncfusion team,


I want to add a custom button to check the certain records. How to scan all the records and check if meet certain criteria? For example below screenshot, check the records under Clause(s).



Regards


6 Replies 1 reply marked as answer

PS Pon Selva Jeganathan Syncfusion Team April 27, 2023 04:27 AM UTC

Hi Jinchuan DU,


Query:  I want to add a custom button to check the certain records


To achieve this requirement, we suggest using the selectCheckboxes method of the TreeGrid. You can call this method inside your button click event and pass the record indexes of the rows that you want to check as a parameter. The selectCheckboxes method will check the checkboxes for the specified rows.


Please refer to the below code snippet,


 

   <ejs-button id="show" content="SHOW"></ejs-button>

<ejs-treegrid id="TreeGrid" dataSource="ViewBag.dataSource" height="400" autoCheckHierarchy="true" childMapping="Children" treeColumnIndex="1">

            <e-treegrid-columns>

                <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="95"></e-treegrid-column>

                <e-treegrid-column field="TaskName" headerText="Task Name" showCheckbox="true" width="220"></e-treegrid-column>

                …..

        </ejs-treegrid>

document.getElementById('show').addEventListener('click', () => {

  var treegrid =

    document.getElementsByClassName('e-treegrid')[0].ej2_instances[0];

  treegrid.selectCheckboxes([2]);//Here pass the rowindex value as array

});

 

 


Please refer to the below screenshot,



Please refer to the below API documentation,

https://ej2.syncfusion.com/documentation/api/treegrid#selectcheckboxes


Kindly get back to us for further assistance.


Regards,

Pon selva


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.



JD Jinchuan DU April 27, 2023 06:46 AM UTC

Hi  Pon selva,


it only works on current page. Any other method to check in all pages.

Thanks.


Regards



PS Pon Selva Jeganathan Syncfusion Team May 4, 2023 08:19 AM UTC

Hi Jinchuan DU,


Query it only works on current page. Any other method to check in all pages.


Based on your query, we suspect that you are trying to check some records across all pages in a grid using a specific condition. To achieve your requirement by using the actionComplete event with args.requestType as ‘paging’ to select records in all pages based on a condition.


In the actionComplete event, you can use the getCurrentViewRecords() method to get the current page records, and then loop through each record to check if it meets your condition. If the record meets your condition, you can collect the index of this records and select the records using selectCheckboxes method of the treegrid.


Please refer to the below code snippet,


 

<ejs-treegrid id="TreeGrid" dataSource="ViewBag.dataSource" height="400" autoCheckHierarchy="true" childMapping="Children" treeColumnIndex="1" actionComplete=”actioncomplete”>

            <e-treegrid-columns>

                <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="95"></e-treegrid-column>

                <e-treegrid-column field="TaskName" headerText="Task Name" showCheckbox="true" width="220"></e-treegrid-column>

                …..

        </ejs-treegrid>

 

actionComplete (args) {

  if (args.requestType == 'paging') {

      SelectedNodeIndex = [];

 

      var recs = treegrid.getCheckedRecords();

      var indx = treegrid.getCheckedRowIndexes();

      var currentData = treegrid.getCurrentViewRecords();

      if (recs.length != 0) {

        if (

          currentData.length >

          treegrid.pageSettings.pageSize * treegrid.pageSettings.pageCount

        ) {

          var index2 = 0;

          for (var i = 0i < currentData.lengthi++) {

            alreadyChecked2 = false;

            for (var j = 0j < recs.lengthj++) {

              if (currentData[i].taskID == recs[j].taskID) {

                alreadyChecked2 = true;

              }

            }

            if (!alreadyChecked2) {

              if (recs.Result[j]) {

                SelectedNodeIndex.push(index2);

              }

            }

            index2++;

          }

          alreadyChecked2 = false;

        } else {

          for (var i = 0i < currentData.lengthi++) {

            for (var j = 0j < recs.lengthj++) {

              if (currentData[i].taskID == recs[j].taskID) {

                alreadyChecked = true;

              }

            }

          }

          if (!alreadyChecked) {

            var dtaSource = treegrid.getCurrentViewRecords();

            var index = 0;

            for (var i = 0i < dtaSource.lengthi++) {

              if (dtaSource[i].progress == 50) {

                SelectedNodeIndex.push(index);

              }

              index++;

            }

          }

        }

      } else {

        var dtaSource = treegrid.getCurrentViewRecords();

        var index = 0;

        for (var i = 0i < dtaSource.lengthi++) {

          if (dtaSource[i].progress == 50) {

            SelectedNodeIndex.push(index);

          }

          index++;

        }

      }

      if (!alreadyChecked) {

        treegrid.selectCheckboxes(SelectedNodeIndex);

      }

      alreadyChecked = false;

    }

  }

 


In the above code example, we have checked particular column’s value (field value) and selected the record based on its value while performing paging action in the actionComplete event of Tree Grid.


Please refer to the below sample,

https://stackblitz.com/edit/2qh7dd-wzjmjv?file=index.html,index.ts


Please refer to the below API documentation,

https://ej2.syncfusion.com/documentation/api/treegrid/#actioncomplete

https://ej2.syncfusion.com/documentation/api/treegrid/#getcurrentviewrecords

https://ej2.syncfusion.com/documentation/api/treegrid/#selectcheckboxes


If the above solution does not meet your requirement, kindly share the below details which is helpful to proceed further.


  1. Complete treegrid code example
  2. Data binding details( whether you are using a local data binding or remote data binding)
  3. Share the issue reproducible sample.
  4. Stacktrace details(if face any)


Kindly get back to us for further assistance.


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.









Marked as answer

JD Jinchuan DU May 4, 2023 08:29 AM UTC

Thanks for your reply and sample.



SG Suganya Gopinath Syncfusion Team May 5, 2023 06:07 AM UTC

Jinchuan, 

If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.





JD Jinchuan DU May 15, 2023 06:49 AM UTC

Hi Suganya,


Yes. I accepted. Thanks for your support.


Loader.
Live Chat Icon For mobile
Up arrow icon