Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot put a button in SlickGrid cell

I am trying to put a button in the cells of one of the columns and do something when it's clicked.

For example I add these lines to the SlickGrid example 1 (http://mleibman.github.io/SlickGrid/examples/example1-simple.html)

First to the column array I add:

    {id: "Report", name: "Report", field: "Report", width: 40, sortable: true, formatter:reportFormatter}

then I add:

      function reportFormatter(row, cell, value, columnDef, dataContext) {
          return "<input type='button' value='show' id='reportBtn'/>";
      }

      $('#reportBtn').click(function() {
          alert("hello");
      });

The buttons appear in the cells but the click event is not being called ! I must be doing something wrong but can't for the life of me figure it out can anyone help ? Thanks!

like image 366
kofifus Avatar asked Sep 06 '25 03:09

kofifus


2 Answers

slick.formatters.js

...
"btnForm": buttonsFormatter // add Slick.Formatters
..
  function buttonsFormatter(row, cell, value, columnDef, dataContext) {
      return  "<input type='button' value='"+value+"' id='btnForm' value2='"+row+"' value3='"+cell+"' onClick='fnBtnClick(this);'/>";
  }

add your html Script

function fnBtnClick( objBtn ){
 alert( "row::[" + objBtn.value2 + "]\n\ncell::[" + objBtn.value3 + "]" );      
}
like image 141
teja Avatar answered Sep 07 '25 23:09

teja


you should use the grid events not button event like below ,

  1. will call the onclick event of the grid ,
  2. check if the clicked field is your button one
  3. do your action

    grid.onClick.subscribe(function (e, args) {
      //check if your button was clicked  
      if ($(e.target).hasClass("btn")) {
            var item = dataView.getItem(args.row);
            ///do your action
        }
    
    });
    
like image 33
Ali Avatar answered Sep 08 '25 00:09

Ali