How to Call/Reload DataTable On Button Click ?
How to Call/Reload DataTable On Button Click ?
Wait, Please Read Question before Giving answer, it's bit tricky.
I am having a dataTable with ServerSide Rendering, And Custom Pagination Designs (Which I created using "dom" Property).
And a Text box Outside Data table Which is used for searching.
Now, I am getting values that are inside dataTable (i.e Sort Direction, Sort Column, Page Size, etc), and also Datatable is getting refreshed after these values are changed.
But the problem is When I Press "Search" Button (not default search of the data table), the request is not reaching to the server
Here is my data Table
I am Calling this method on click function..
function OnSearchButtonPressed() {
if ($.fn.DataTable.isDataTable("#myTable"))
{
$('#myTable').DataTable().destroy();
}
$("#myTable").DataTable({
"destroy": true,
"serverSide": true,
"bRetrieve": true,
"searching": false,
"order": [0, "asc"],
"pagingType": "simple",
"dom": 'tilp',
"lengthMenu" : [3,4,5],
"autoWidth": false,
"dom": '<<"table-responsive dark-table border-bottom-0">t<"pagination-datatable"<"row align-items-center"<"col-12
col-sm-6 col-md-6 col-xl-6 col-lg-6"<"d-flex align-items-center"i>><"col-12 col-sm-6 col-md-6 col-xl-6 col-lg-6"
<"d-flex align-items-center justify-content-end"<"pagination-select"l>p>>>>',
"language": {
"paginate": {
"next": '<span><img src="/images/next.svg" alt="next-arrow" /></span>',
"previous": '<span><img src="/images/previous.svg" alt="prev-arrow" /></span>'
},
},
"ajax": {
"url": "/controller/action/",
"type": "Post",
"dataType": 'json',
"data": function (d) {
return getSearchValues(d);
},
"dataSrc": "data",
},
columns: [
{ "data": "id","name":"P.Id", "searchable": false, "visible": false },
// Other Columns
],
});
}
Please help me with this...
Note:: it was working before I add the "dom" element for custom paging...
This question has an accepted answers - jump to answer
Answers
This will disable searching. It also removes the search input. Remove this line and use the -option dom` option like you have to remove the search input.
Kevin
I tried "searching": false, so far no luck...
also, Table refreshed every time I change some inside values (ie. Sort Dir, Page), but Datatable is not reloading after the outside Search button Click.
You need to remove
"searching": false,
. It disables the search functionality. In the code you posted you have it on line 13.Are you saying the
OnSearchButtonPressed()
function is not called?Kevin
I try debugging it .. if goes into the function
OnSearchButtonPressed()
but not in data tableRemove the
"searching": false,
option for search functionality.Use the
dom
option, like you have, without thef
.If the above doesn't help then please post the code for this.
Kevin
Here I am sharing My Code...
I have...
1. A DataTable With Server-Side Rendering.
2. 3 Search Boxes (None of them is DataTable's Default) & a Search Button
here is My Html it also has one Checkbox....
Here is script
Please Help me with this .. Sorting and Paging is working but when I Enter Values in those Text boxes and Press Search button is it not working (Request is not reaching to Server.)
It was Working Before I added dom for Custom paging..
Did you verify this by looking at the browser's network inspector tool?
Is the
OnSearchButtonPressed()
function called when clicking the button?Do you get errors in the browser's console?
Simply saying the request doesn't reach the server doesn't provide enough information to help. What debugging have you done and what specifically have you found to narrow down the problem? Can you post a link to your page or a running test case so we can help debug?
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Here is a Test Case...
http://live.datatables.net/woderoce/2/edit
As you can see, A request is made every time I change the Database's internal Parameters like Sorting Links, Paging links, but not at the time I put values in search boxes and hit the search button.
You still have "searching:false" which Kevin told you twice to remove. You also have two "dom" settings, which isn't going to work.
The first problem is this error:
You have
onclick="BindProductTable()"
. I changed it toonclick="BindTable()"
.The problem with the
dom
string is you are missing a closing>
. This causes thetable
to be removed from the HTML which means the IDexample
is no longer in the DOM when re-initializing Datatables. You have:I added a trailing
>
to the string and now it is sending the request when clicking the button.http://live.datatables.net/bapavufe/1/edit
Kevin