I have tried many times but it seems that something is wrong from my side, I would like to get(retrieve) the attribute value of every row of a table for example productID attribute which is written below.
e.g..
<button id="button">Show ID Product Attribute</button>
<table class="display" width="100%" id="example" >
<thead>
<tr>
<th>#</th>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
$answer = ProductsController::showProducts();
foreach ($answer as $key => $value) {
echo '<td>'.($key+1).'</td>
<td productID="'.$value["id"].'">'.$value["productName"].'</td>
<td>'.$value["price"].'</td>';
}
?>
<script type="text/javascript">
$(document).ready(function () {
var table = $('#example').DataTable();
$('#example tbody').on('click', 'tr', function () {
$(this).toggleClass('selected');
});
$('#button').click(function () {
console.log(table.rows('.selected').data()); //return all selected row data
});
});
</script>
The problem is here if I select more than 1 row and click on the Show ID Product Attribute button it returns the below text but I would like to access only the productID attribute which is hidden in the below result
{
"0": ["1", "Milk", "100"],
"1": ["2", "Tea", "200"]
}
How can I able to do find it, I really appreciate your help.
2
Answers
you can use
data-search
attribute to indicate actual value for#
column, like this:You could loop through the selected rows and output the relevant entry from the array for that row: