Nick W
Nick W

Reputation: 907

How to trigger the click event of a Bootgrid command button

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();

Upvotes: 0

Views: 2393

Answers (2)

philw
philw

Reputation: 687

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.
     }

Upvotes: 4

Tiago Gouvêa
Tiago Gouvêa

Reputation: 16770

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.

Upvotes: 0

Related Questions