user2242044
user2242044

Reputation: 9233

Getting the value of a cell when clicked into a Bootstrap-Table

I would like to click on a cell in a Bootstrap-Table table and get the value of a specific column (the Date column) in that row.

This code gets me the value index of the row, but my data is an array of JSONs. I don't think the index is helpful because I think the array is essentially considered unordered. Is there a way to use this index to get the value?

 $("#table").on("click", "tr", function (row, $el, field) {
   var row = $(this).index();
   alert(row);
    });

the data:

var data = [
    {
        "Date": "2015-02-03",
        "Contact": 1
    },
    {
        "Date": "2017-01-22",
        "Contact": 2

    }
];

http://jsfiddle.net/gv2mnwd9/

Upvotes: 1

Views: 5725

Answers (3)

Harj Sohal
Harj Sohal

Reputation: 141

I think you could do something follows

answer = table.row(0).data().Date;
answer = table.row(0).data().Contact;

Update after comment >> Apologies if I have misunderstood as I thought you wanted value of a column, i hope below works for your needs.

                $("#table").on("click", "tr", function (row, $el, field) {

                    tr = $(this).closest('tr');
                    row = table.row( tr );
                    answer = table.row(row).data().Contact;
                    alert(answer);
                });

Upvotes: 0

Karthik Ganesan
Karthik Ganesan

Reputation: 4222

you can attached an event to td instead of tr

and get the value as follow

  var col = $(this)[0].textContent;

here is a jsfiddle

http://jsfiddle.net/gv2mnwd9/1/

Upvotes: 2

Dan Philip Bejoy
Dan Philip Bejoy

Reputation: 4391

You could pick the Date cell by the data attribute.

 $("#table").on("click", "tr", function (row, $el, field) {
   var date = $(this).find("a[data-name='Date']").html();
 });

Or

$("#table").on("click", "tr", function (row, $el, field) {
   var date = $(this).find("a[data-name='Date']").data('value');
 });

Upvotes: 1

Related Questions