Get ID from DataTables-Table


Get ID from DataTables-Table



maybe a easy question for all of you, but I'm fairly new to JavaScript/jQuery and how AJAX calls work, so i Need a Little bit of help.



At the Moment im rebuilding my DataTable-Tables to use severside-processing because they grow very big and through this becoming very slow. I allready put a fair amount of time in it to understand how ajaxcall works, etc. but there is one Problem i can't solve: I can't get the ID from the DataTables im working with.



This is my HTML-Part:


<table id="tbl_user" class="table table-hover display ajaxTable" width="100%;" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Kostenstelle</th>
<th>Status</th>
<th>Aktionen</th>
</tr>
</thead>
</table>



This is my javascrip/jQuery-Part:


$('.ajaxTable').DataTable({
processing: true,
serverSide: true,
ajax: {
type: "POST",
dataType: 'json',
url: 'ajaxcall.php',
data: function(params) {
var query = {
ajaxcall: 'getSelectDataTable',
controlName: $(this).attr("id"),
length: 14,
draw: params['draw'],
start: params['start'],
search: params['search']['value'],
columns: params['columns'],
order: params['order']
}
return query;
}
},
columns: [
{"data": "user_id"},
{"data": "user_name"},
{"data": "user_kostenstelle"},
{"data": "user_status"},
{"data": "aktionen"}
]
});



I thought i can easly get the ID with $(this).attr("id") but i was wrong. Any has a Suggestion?



Thanks in advance.




2 Answers
2



You'll have to loop through all your tables with the class .ajaxTable in order to get their specific id:


.ajaxTable


id


$('.ajaxTable').each(function() { //Loop over your tables
let that = this; //Store your current table
$(that).DataTable({
processing: true,
serverSide: true,
ajax: {
type: "POST",
dataType: 'json',
url: 'ajaxcall.php',
data: function(params) {
var query = {
ajaxcall: 'getSelectDataTable',
controlName: $(that).attr("id"), //Reuse your current table
length: 14,
draw: params['draw'],
start: params['start'],
search: params['search']['value'],
columns: params['columns'],
order: params['order']
}
return query;
}
},
columns: [{
"data": "user_id"
},
{
"data": "user_name"
},
{
"data": "user_kostenstelle"
},
{
"data": "user_status"
},
{
"data": "aktionen"
}
]
});
});





Works like a charm! I thought if i pick a class it will iterate over all tables anyway. Thank you!
– Daniel
Jul 3 at 7:18





$('.ajaxTable').DataTable().data().each(function (d){
console.log(d)
});






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

PHP contact form sending but not receiving emails