multi-filter-select table row not appears when performed ajax call
multi-filter-select table row not appears when performed ajax call
Hi Allen,
I am using datatable for loading data (ServerSide Processing), so i have dropdown list <Departments>, by default first option selected when page loads so using ajax call I will pull the data from db related department employees.
On the first load, I am sending json data from server where i am use initcomplete function to form the Multi select dropdown and populate the data into list
It works well.
when I make another select from dropdownlist <departments> data getting populated but I am seeing row (multi select row) and initcomplete not hitting
I went across multiple Q&A, i found ajax.table.reload to use and initcomplete will fire only once
Can you please provide me working example where I can load and show data when i performed ajax call as well?
Answers
The
initComplete
callback runs only once after Datatables initializes. Without knowing when you need to update the dropdown list its hard to provide a specific answer.Does this list contain all the options in the DB?
How are you building the options list?
The -option drawCallback` runs each time the table draws and in the case of server side processing runs each time data is fetched from the server. Maybe you need to use this instead.
Please post your code so we can see what you are doing. Better is to provide a link to your page or a test case so we can actually see the script in action.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
Hi Kevin,
Thank you quick feedback, I have created a test using js bin but it is not working example as I dont have data to populate
https://jsbin.com/qeleledunu/edit?html,js,output
I am using MVC5, HTML, Javascript,
I will populate the data
Html code :
The fiddle doesn't run. We would also need to see the issue occurring - you can load data with
data
so you don't need to link to your data source.Colin
Yeah, I know sample I tried to create is now working as I not able to load the data as expected and I want to show exact code what i am using so i provided snippet of my code if you see where i am going wrong
Maybe you can explain a bit more about what you are trying to achieve. You have server side processing enabled and are sending search parameters to the server script. Sounds like what you want to do is update the dropdown lists when the filter values change, correct? Or do you want to update the dropdown list each time the server side script responds with table data, each search, sort or page of the table?
Basically the answer of when you want to update the dropdown lists will dictate where the code to rebuild the DDL will need to be.
Kevin
Let me explain
MVC Index View, I have initiated as below
when Index page loaded (starting / Initial page) I have dropdown List which will be populated by Departments
Physics
Mathematics
etc.
On document.ready() for the first time I am calling, passing default one selected item.
in the getEmpList()
I will detroy and recreate datatable.
I am using function drawCallBack and fnInitiComplete where i am converting
"fnInitComplete": function (oSettings, json) {
//alert('DataTables has finished its initialisation.');
//debugger;
when the page loads initially for the first time, i am seeing below the header there are dropdown boxes filled with data and am able to do filter without any issue.
but If I change / select new value from the select item list, i,e, Physics is default selection if I change to Mathematics.
In onChange i am calling getEmpList($('#ddlDepts').val());
which will call getEmpList which will destroy the current table and recreate it.
I See initiComplete being hit but I am not seeing dropdowns in the view
Basically when I change the Dropdown, I am not seeing the multi-select column displayed apart from the first time.
if we change the selection or while recreating i am losing the dropdown filters
When the Datatable is destroyed and rebuilt you say you aren't seeing the column dropdown list. Do you see the second header?
You are appending using this:
Maybe you need to use jQuery empty first. If this doesn't help inspect the second header to see what it contains after the
destroy()
and reinitialize.If you still have difficulties the test case can be narrowed down to the process you are using to initially build the selects and the destroy and rebuild of the Datatable. The actual data doesn't matter. This page contains templates for ajax and server side loaded data if you want. But you can probably just fake the options list to demonstrate the issue.
Kevin
I am not seeing second header at all (not displayed)
Link above provide shows me 404
404 - Page not found
Sorry, the page you are looking for does not exist. Please feel free to get in touch if you feel you have reached this page in error.
If you typed the url yourself please check the address and spelling
It is possible that the page you are looking for has been removed
Remove the closing bracket from the url.
Sorry, mistyped the link. Fixed above. It had an extra
)
at the end.Kevin
do you have any sample which loads data from array or text file using server side processing so that I can modify that test to replicate mine which have some options where i can select or display required data
That will be easy for me
Did you look at the link I provided?
https://datatables.net/manual/tech-notes/9
There are starter templates for ajax and server side sourced data using either arrays or objects.
Kevin
Thanks Kevin,
If i call table.destroy() is the thead and tfoot will be deleted?
As shown in below, if I created in html and before calling can I clone the thead with class customdropdown?
The
destroy()
docs explain this:Kevin
Thanks Kevin, I have fixed issue myself
Glad you fixed it! Do you mind passing along what you fixed in case others have the issue?
Kevin
Sure Kevin,
First Time when we load page, HTML content is formed in View is been considering as shown below.
So
1. Filter row being added below the Header row with dropdowns
when we do any ajax calls in the page, after reload in the function getEmpList() first we are destroying the table
Once we call the destroy(), it is been destroyed the thead settings which we defined in HTML View (not sure why it happens as it remembers Thead column definitions). So we need to recreate the header part by using the clone function
and replicated the filter row as specified in Index view
** <thead>
<tr class="customDropdown">
<th class="Name">Name</th>
<th class="Age">Age</th>
<th class="Address">Address</th>
<th class="City">City</th>
<th class="text-right btnAction">Action</th>
</tr>
</thead>**
As the table is destroyed, and recreated InitiComplete is called and populating the data which I am passing from server using json.
Thanks
Suresh
Kevin,
with this I have few questions.
** <thead>
<tr class="customDropdown">
<th class="Name">Name</th>
<th class="Age">Age</th>
<th class="Address">Address</th>
<th class="City">City</th>
<th class="text-right btnAction">Action</th>
</tr>
</thead>**
As it holds the HTML view defined for Table header, is this a bug?
Note:
I can understand if we loose the theader information if we clone it but this is set from Index
Not sure I understand your question. Can you provide a simple example using http://live.datatables.net/ ?
Kevin