Reputation: 713
I've a datatable, which is created dynamically. Number of rows differs from user to user. Some may create 2 rows, some may create 3 etc. For example, the table may look like this:
| Category | User |
| Manager | Krishna |
| Assistant Manager | Guru |
They will be selected from a select dropdown. This is how I tried to retrieve all the data from the table when the submit button is clicked:
var noofrows = tabResources.data().count();
var data = tabResources.rows(0).data();
alert(data);
In the alert, I get [object, object]
. But, I don't know how to retrieve each value. How can I do that? This is the table:
tabResources = $("#tabResources").DataTable({
"sDom":"t",
ajax:{
"url":"../api/projectapi.php?action=getresourcebyprojectid",
"type":"GET",
"data":{"projectid": <?php echo $_GET ["projectid"]?>}
},
"destroy":true,
"ordering":false,
"columns":[
{"title":"Resource Category", "data":"resourcecategoryid","render":function(data, type, row){
return '<select class="form-control selectresourcecategoryid" name="resourcecategoryid[]" valueid=' + data + '></select>';
}},
{"title":"User", "data":"userid","render":function(data, type, row){
return '<select class="form-control selectuserid" name="userid[]" valueid=' + data + '></select>';
}},
{"orderable":false, render: function ( data, type, row ) {
return '<a href="javascript:void(0)" class="btn btn-info btn-sm btn-remove">Remove</a>';
}
}
]
});
Upvotes: 0
Views: 871
Reputation: 2748
Checkout the snippet, does this solve your problem?
$(document).ready(function() {
var data = [
['name1', 'position1', 'office1', 15],
['name2', 'position2', 'office2', 30]
];
var table = $('#example').DataTable({
data: data,
columns: [
{
title: "Name"
},{
title: "Position"
},{
title: "Office"
}
]
});
var data = table.data();
for (var i = 0; i < data.length; i++) {
console.log(data[i]);
}
} );
<script src="//code.jquery.com/jquery-1.12.3.js"></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet"/>
<table id="example" class="display" cellspacing="0" width="100%"></table>
Upvotes: 0