DHTMLX Docs & Samples Explorer

attachEvent(evName, evHandler)

Required library edition: This method works with any edition of DHTMLX library
Required library file: dhtmlxgrid.js

adds any user-defined handler to available events

  • evName - name of the event;
  • evHandler - user-defined event handler.

Attaching anonymous event handler:

<script>
  grid.attachEvent("onRowSelect",function(rowId,cellIndex){
     alert("Row with id="+rowId+" was selected");
  });
</script>

Attaching pre-defined event handler:

<script>
   grid.attachEvent("onEditCell",doOnEditCell);
   ...
   function doOnEditCell(stage,rowId,cellIndex,newValue,oldValue){
      if ((stage==2)&&(newValue!=oldValue)){
         alert("Cell with id="+rowId+" and index="+cellIndex+" was edited");
         return true;
      }
      return true;
   }
</script>

Several handlers can be attached to one and the same event, and all of them will be executed.

<script>
   grid.attachEvent("onCheck",doOnCheck1);
   grid.attachEvent("onCheck",doOnCheck2);
   ...
   function doOnCheck1(rowId,cellIndex,state){
      if (state){
         alert("Checkbox in the row with id="+rowId+" was checked");
      }
   }
   function doOnCheck2(rowId,cellIndex,state){
      if (state){
         alert("Checkbox in column with index="+cellIndex+" was checked");
      }
   }
</script>

If it is necessary, the custom code can be detached from an event:

  var id1=grid.attachEvent("onRowSelect",myHandler);
  var id2=grid.attachEvent("onRowSelect",myHandler2);
  grid.detachEvent(id2);    // detach myHandler2 from onRowSelect event

Some of the events can be blocked. So based on the return value, the grid will react differently:

  • return 'true'; - confirm action
  • return 'false'; - deny action
  • no return command - treated as return 'false'
  grid.attachEvent("onEditCell",function(){
      return false;     // will block any edit operation
  })

Note: the names of the events are case-insensitive.

See also: detachEvent