Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ag-grid disable checkbox programatically via gridOptions API

Tags:

ag-grid

I have a column with checkboxSelection=true

Under some conditions and via the API, I would like to decide whether the checkbox is readonly, i.e., I can't check/uncheck.

Is this possible?

like image 480
Aviram Avatar asked Nov 27 '17 10:11

Aviram


People also ask

How do I implement the checkbox renderer in AG-grid?

You can also implement the checkbox renderer using JavaScript. In this case, the checkbox renderer is constructed using a JavaScript Class. An input element is created in the ag-Grid init lifecycle method (required) and it's checked attribute is set to the underlying boolean value of the cell it will be rendered in.

Why is my checkbox not visible in the grid?

When a callback is used and returns false, the grid assumes a checkbox is sometimes used and as such creates one that is not visible. It is possible to change the default behaviour for when a checkbox is not displayed, and instead have the checkbox visible but disabled. This can be done by enabling the column property showDisabledCheckboxes.

How do I create an input element in AG-grid?

An input element is created in the ag-Grid init lifecycle method (required) and it's checked attribute is set to the underlying boolean value of the cell it will be rendered in. A click event listener is added to the checkbox which updates this underlying cell value whenever the input is checked/unchecked.

How do I select a single row in AG grid?

Working with AG Grid nodes is preferred over the row data as it provides you with more information and maps better to the internal representation of AG Grid. The example below shows single row selection. Property rowSelection='single' is set to enable single row selection. It is not possible to select multiple rows.


4 Answers

If someone is still looking for an answer for this, I found a simple solution,

in your gridOptions add the following

gridOptions = {
columnDefs: [
  {
    checkboxSelection: true,
    cellStyle: params => 
      params.value === 'YOUR_VALUE' ?  
          {'pointer-events': 'none'}
          : ''
   }
 ]  
};
like image 170
rowadz Avatar answered Oct 06 '22 10:10

rowadz


The property checkboxSelection can either be a boolean OR a function.

https://www.ag-grid.com/javascript-grid-column-properties/#gsc.tab=0

By using a function you can show or hide the checkbox row-based:

checkboxSelection = function(params) {
   // add your cancheck-logic here
   if (params.data.yourProperty) {
      return true;
   }
   return false;
}

Disabling a checkbox is not possible out of the box. One possible way is to reset the checkbox back to it's original state:

onRowSelected:(params) => {
    if(params.data.yourProperty && params.node.isSelected())
        params.node.setSelected(false);
}
like image 25
Alexander Zbinden Avatar answered Oct 06 '22 11:10

Alexander Zbinden


Instead of using checkboxSelection=true, you can try cellRenderer

  field: 'isMandatory',
  cellRenderer: (params) => {
       if (params.value) {
          return `<input type="checkbox" checked/>`;
       }
       else {
          return `<input type="checkbox" />`;
       }
 }
like image 2
bensonissac Avatar answered Oct 06 '22 09:10

bensonissac


As per documentation, you can you isRowSelectable in your gridConfig

    isRowSelectable: function(rowNode) {
        return rowNode.data ? rowNode.data.condition: false;
    },
like image 1
Jaime Oliveira Avatar answered Oct 06 '22 11:10

Jaime Oliveira