How to load state with less columns than the DataTable initialisation
How to load state with less columns than the DataTable initialisation
nathanbobato
Posts: 12Questions: 1Answers: 0
Hi everyone! When i have to add more columns to my datatable initialisation, the stateLoadCallback doesn't load the content properly. Someone can help me? Thanks!
This discussion has been closed.
Answers
Can you post your Datatables code so we can see what you are trying to do in the
stateLoadCallback
? Or better is a link to your page or a test case showing the issue.https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
I'll try to explain a little more about it. We save the state content in the Database. So, when we need to add more columns to the DataTable, the previous states stop working because the number of the columns in the json state from the db isn't the same of the DataTable initialisation.
The stateLoadCallback:
Edited by Kevin: Syntax highlighting. Details on how to highlight code using markdown can be found in this guide
I created a test case but don't see any issues.
http://live.datatables.net/xivifowu/11/edit
You can add or remove columns and it seems to work.
Are you doing something more than what you posted? Are you getting errors? Please describe the problem you are having.
Can you please post a link to your page or a test case showing the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
I saw your demo and actually it's the opposite, datatable needs to have 1 more column than json, with another data, then you can see the problem.
Ok, lets change it a bit, comment out the last column:
Reload the page and the
stateLoadCallback
has only the five columns:Add the last column:
Reload the page and the
stateLoadCallback
shows the same five columns. There are no errors and the Salary column is shown.Maybe you can tell me how to recreate the issue with this test case. Also you didn't answer my questions:
EDIT: Here is the updated test case:
http://live.datatables.net/xivifowu/14/edit
Kevin
Hi! No, i'm not getting any errors.
The datatable has 1 more column than the json in my case, you can see here: live.datatables.net/xeyutize/1/edit
This error is being shown:
The reason is that the Ajax response from loading the table, ie,
"ajax": "/ajax/objects.txt",
does not have the objectcte_peso
. This error is not coming from using statesave.If your JSON response doesn't have the object defined you can use
defaultContent
. Like this:http://live.datatables.net/xeyutize/2/edit
Kevin
Sorry for my bad communication in English.
This defaultContent didn't work. So i'll try to show a little more about how i'm doing this here.
My table:
So, in my json state i have the values for marcs and emit but not for emit_cn. Because of this nothing happens.
What is your data source?
Why are you using
async: false,
?Does this mean the Datatable doesn't appear?
Kevin
I have an database with a lot of states saved. So the user can change between those states, he can save an state with some order and some columns and another one different and i get these states from there.
Actually there is no difference between async: false or true here for what i can see.
The DataTable appear, but only like in "default" state.
When I asked
What is your data source?
I want to know where the row data comes from. Is it loaded into the DOM, are you usingajax
, etc.Can you post a link to your page or a test case replicating the issue? I'm trying to replicate it for you but that is not working because I don't have the specifics of your solution.
Kevin
I'll create a test case for you to check and i'll bring for you shortly.
Thank you for your attention.
Hi. I'm bringing the test case and i'll show you in parts.
I'm getting this same JSON from DB for both DataTables:
This first case shows the DataTable with 7 columns as the JSON. All is working so far, including order, ColReorder.
Link to the case: https://testedevdocs.000webhostapp.com/
DataTable without the new column:
And now the problem begins. Here in this another case i've add a new column to the DataTable and to the data.
Because of this new column the Order, ColReorder, from JSON isn't working anymore. I think the DataTable shows as something like a "Default State". But the data is still showing.
Link to the case: https://testedevdocs.000webhostapp.com/new_columns/
DataTable with the new column:
I hope with this you can see what's happening.
Thank you for all the support.
Sorry, actually those are the DataTables:
Without the new column:
With the new column:
The only difference between this and the previous answer are only the names of the columns.
Thank you!
Thanks for the added detail to understand the problem you are having. @allan or @colin can confirm but I suspect you are correct that Datatables will use defaults if the column definitions are different.
Sounds like you might need to use
stateSaveParams
to save additional information about the table that you can use instateLoadParams
to manipulate the table ordering and column ordering as appropriate.Kevin
I see. Actually I was trying to manipulate adding name do the columns through SaveParams and then doing this:
But i don't know how to manipulate the ColReorder.
The
colReorder.order()
API is used to get and set the column order.Kevin
Interesting, the problem is that i don't know which is the new index. The new index could be index 40 and the 40 was a index that is visible in the json, so intead of show the old data, it will show the new data field for index 40.
But i'll try something and i'm wait for the confirm for Allan and Colin.
Thank you.
The
column-reorder
event might be useful for you to keep track of the column reordering changes.Kevin