Introduction

I have Ag-Grid and on page load. It shows all the rows selected.

Now, I am going to show how to deselect single or multiple rows one by one.

Explanation

Now, in this image, I have selected All Rows on Page Load and I want to deselect the rows one by one and then select rows one by one.

Now, you can see that in the second picture, I deselect Single without deslecting other rows.

Code For This

  1. $scope.gridOptions = {
  2. columnDefs:
  3. { headerName: "CompanyName", field: "CompanyName", editable: true, width: 130 },
  4. { headerName: 'ProductName', field: "ProductName", editable: true, width: 130 },
  5. { headerName: "ProjectID", field: "ProjectID", editable: true, width: 100 },
  6. { headerName: "Status", field: "Status", editable: true, width: 100 },
  7. { headerName: "Delete", field: "Delete", editable: true, editable: false, width: 100 cellRenderer: customEditorImageWQ },
  8. { headerName: "Update", field: "Update", editable: true, editable: false, width: 100, cellRenderer: customEditor }],
  9. rowData: null,
  10. enableColResize: true,
  11. enableSorting: true,
  12. rowSelection: 'multiple',
  13. angularCompileRows: true,
  14. rowDeselection: true,
  15. onRowClicked: RowClick,
  16. enableFilter: true,
  17. editable: true,
  18. suppressRowClickSelection: true,
  19. };
  20. function RowClick(param) {
  21. var currentRowIndex = param.rowIndex;
  22. $scope.temp = 0;
  23. $scope.gridOptions.api.forEachNode(function select(node, idx) {
  24. if (idx == currentRowIndex && $scope.temp == 0) {
  25. var success = false;
  26. var data = $scope.gridOptions.api.getSelectedRows();
  27. for (var iRows = 0; iRows < data.length; iRows++) {
  28. if (data[iRows]["CompanyName"] == param.data.CompanyName) {
  29. success = true;
  30. break;
  31. }
  32. }
  33. if (success == false) {
  34. $scope.gridOptions.api.selectNode(node, true, true);
  35. $scope.temp++;
  36. }
  37. else {
  38. $scope.gridOptions.api.deselectNode(node);
  39. $scope.temp++;
  40. }
  41. }
  42. });
  43. }