Cheknov
Cheknov

Reputation: 2072

How to get and store the values of a specific row in a table via JavaScript / jQuery

How can I store in an array of arrays in JavaScript / jQuery the values of each selected row of the table.

JSFiddle

The event to listen to would be (I think) when the 'Show selected' button is clicked.

For example in JS we would have an array like this:

result = []
result[0] = row1
result[0][1] = Gene value for row1 (Gene1)
result[0][2] = Variant value for row1 (Variant1)
result[0][7] = #5 value1 for row1
result[1] = row2
result[1][1] = Gene value for row2 (Gene2)
etc
...

Upvotes: 0

Views: 152

Answers (2)

Dani
Dani

Reputation: 2036

With something like this:

$("#show-selected").click(function () {
        var tr = $("tr");
        var array1 = [];
        tr.each(function(index1, trCurrent) {
            var array2 = [];
            var trChildrens = $(trCurrent).children();
            if(($(trChildrens[0]).children()[0]).checked){
                trChildrens.each(function(index2, trChildrensCurrent) {
                    var innerHtml = $(trChildrensCurrent).html();
                    array2.push(innerHtml);
                });
                array1.push(array2);
            }
        });
        console.log(array1);
    });

TEST:

IMAGE

Upvotes: 0

john Smith
john Smith

Reputation: 17906

You could do this just like so:

$("#show-selected").click(function () {
  var results =[];
  $(".variants-table-tbody tr").each(function (index,item) {
      if($(item).find('[name="select-item"]').prop("checked")){
        results.push([
            [$(item).find("td").eq(1).text()],
            [$(item).find("td").eq(2).text()],
            [$(item).find("td").eq(3).text()],
        ]);
      }
  });
  console.log(results);
});

this would create the data on checked rows in the format you described. see https://jsfiddle.net/y4deut3s/

Upvotes: 1

Related Questions