Search code examples
jquery-bootgrid

How to access BootGrid cell by column index or column name?


In selected data and other properties events and methods I get a rows[] object.

  1. How do I access a certain cell in that row. Can I use the column index?
    e.g. for column 2 (starting with zero) can I write: var text = rows[5][2]?

  2. Can I somehow get the cell by the row number and column id?

  3. Can I somehow get the column index from its id?

I can get the columnid from the getColumnHeaders()... but:

  1. Can I somehow get the column NAME (as set in the html) from its id or index?

Solution

  • If you get an Array of rows, you can access a specific row by its index and a cell by its column identifier.

    Access a single row:

    var row = rows[0];
    

    Access a specific cell of the first row:

    var cell = rows[0]["column-id"];
    

    If you need access to the columns, you can request the Array of columns via the method getColumnSettings. See example below:

    var columns = $("#grid").bootgrid("getColumnSettings");
    

    By the way, the column Array has the same order as the columns of your HTML table columns, because they are linked to them.

    Here is the complete example:

    var columns = $("#grid").bootgrid("getColumnSettings");
    var firstColumn = columns[0];
    var cell = rows[0][firstColumn.id];