0

I'm using Bootgrid with command buttons, and if there is only one row in the grid (I can determine if there's only one row from the data in php) then I want to trigger the command button click event. The command button has a class called "command-view". I've tried the following:

$(".command-view").click();

$("#grid .command-view").click();
Nick W
  • 877
  • 2
  • 15
  • 30
  • If I understood correctly, you already have click handlers for your command buttons, but you'd like to trigger it when there is only one row. If so, why not just calling the same method you call inside existing click handler, instead of trying to trigger the click by yourself? – Alisson Reinaldo Silva Feb 26 '17 at 09:18

2 Answers2

4

A more conventional approach would be to use the built-in BootGrid click handler (which is essentially row based as you can see below), then deal with the clicks after that.

This is how BootGrid expects you to work, and also means you don't need to put a separate event handler on each row.

  .on('click.rs.jquery.bootgrid', function (e, cols, row, target) {
     if (typeof row != "undefined")    // Only rows, not headers
     {
         // You will get control here when anything in the row is clicked.
         // In your case just filter for your target and you're done.
     }
philw
  • 661
  • 10
  • 20
  • I was getting javascript error on console when sorting the row because I was trying to get data attribute id on row click. The if statement of checking undefined was what I was missing on the row click event to determine if it is a row or the header. Thanks @philw. – ajexpress Jan 05 '18 at 21:06
0

You must do like:

var grid = $("#your_grid").bootgrid({
    formatters: {
        "commands": function(column, row)
         {
            return '<button class="command-select"data-row-id='+ row.id +'></button>';
         }
    }
    }).on("loaded.rs.jquery.bootgrid", function () {
        grid.find(".command-select").on("click", function (e) {
            var id = $(this).data("row-id");
            console.log(e)
            console.log(id);
        });
    });

Pay attention that grid.find refers to the grid variable, created on first line.

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81