Reputation: 2072
How can I store in an array of arrays in JavaScript / jQuery the values of each selected row of the table.
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
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:
Upvotes: 0
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