How to get definition via column() function
How to get definition via column() function
AllanCochrane
Posts: 41Questions: 1Answers: 2
How do we retrieve the column meta-data (e.g. mData/data attribute, title, current sort direction)? I want to write my own version of the state save function. The only thing that is constant between my database and my JavaScript is the data aka mData attribute so I would like to save that as well as the column order and current sort order.
This discussion has been closed.
Replies
There is no public interface to get the data option at the moment, other that to save the object that you used to initialise the table with. Title can be obtained with `column().header().to$().html()` and the order applied to the table with the `order()` method.
Allan
I think the API just lacks the ability to get the ascending or descending state of the column at the moment. This is where I have to go spelunking into the settings().aoColumns[] array.
The column().visible() method should be extended to be able to get the current visibility - agreed. I'll add this in for the next beta for 1.10.
> position
column().index() gives you the position.
> order
order() gives you the order
> search
search() and column().search() .
Allan
As as example I store the settings:
[code]
[
...
{
"visible" : true,
"mData" : "TestMetrics.testbench_error_count",
"sort" : "asc"
},
{
"mData" : "TestMetrics.testbench_warning_count",
"visible" : false
}
...
]
[/code]
This defines the column order, visibility, mData attribute and sort order attribute for each column. I have many lists which the user can apply to the table to dynamically display different aspects of the data (there are many columns and not all of them are relevant to each user so this is the approach I'm taking to display only the relevant columns).
As users move and hide columns they can save the settings and it's those settings I want to reapply at some time in the future. Now I could do it with a page refresh but I had hoped to do it via JavaScript, the thought being that that approach would be faster.
The way I see it, the current public columns() API only allows me to set order (as in column order) visibility, not the sort order of each individual column.
So, through the public API we have:
order of columns: getter & setter
visibility: setter
sort order of individual column: no API
Certainly - that's what `order()` will give you. It is the sorting array applied to the table. It doesn't give you it column by column - if you want that, you need to transfer the array format from `order()` into the columns - but you would also need to add information to the columns, since otherwise you wouldn't know what order to do a multi-column sort in ( `order()` 's array does contain that information).
> The way I see it, the current public columns() API only allows me to set order (as in column order) visibility, not the sort order of each individual column.
That's correct - it doesn't have a getter method. That would add extra code to DataTables, and not be as complete as the current information returned by `order()` .
Updated fiddle using `order()` : http://jsfiddle.net/ucP7s/9/ . However, as I say, that doesn't retain multi-column ordering information.
Allan
Thanks for the suggestion to add this!
Allan
Is the findSortDir() function an example of how to get the current sorting direction?
> Is the findSortDir() function an example of how to get the current sorting direction?
Its just an example of how you translate a column index into the direction. It will return undefined if the column is not being sorted.
Allan
There are 4 functions called order():
1. table.colOrder.order() - setter & getter
2. table().order([3,'desc']) - setter & getter
3. columns().order('asc') - setter
4. column(i).order('asc') - setter
I see now how I can use #2 to get the current ordering for columns. Thanks for pointing that out.
The documentation is misleading because this page (http://next.datatables.net/reference/api/) says that order() redraws the table, so I never investigated it.
Is that index (the 3 in table().order([3,'desc'])) affected by the column reordering? I guess I'm getting a bit confused by the array that colOrder.order() returns and the order of the array that table.columns() returns and then I have the order that the user wants.
I've fixed that in git already and it needs pushed out. Apologies.
> Is that index (the 3 in table().order([3,'desc'])) affected by the column reordering?
ColReorder will alter the indexes of the sort array (and various other arrays).
Question - all of this information is already available in DataTables own state object. If you want to save this information to the database, why not just provide a `stateSave()` method that writes tot he database and `stateLoad()` which reads it back? Alternatively, would the `state.data()` method discussed here be useful: https://github.com/DataTables/DataTables/issues/255
Allan
Question - all of this information is already available in DataTables own state object. If you want to save this information to the database, why not just provide a stateSave() method that writes tot he database and stateLoad() which reads it back?
[/quote]
I had not thought to use stateSave()/stateLoad(), I'll think about that!
One possible issue is that stateSave()/stateLoad() writes/reads a private object. The app is set up such that the tables are dynamically created and the number and order of columns can change from time to time. My application ought to detect if there's a mismatch between the number of columns in that private object and the number in the table. I think I can just look into that private object and see what's there. If the format changes due to a DataTables release then my code can be updated.
The state.data() could be useful if it supported the column check above via a public API.
BTW This page (https://next.datatables.net/reference/option/stateSaveCallback) discusses stateLoadCallback() and not stateSaveCallBack()
Have been playing with stateSaveParams() etc. the save functions appear to hold the info I need and I can change that info to match my database.
Providing a function to stateSave does not appear to work, it looks like it's a boolean parameter.
As far as I can see, stateLoadParams() gets called only once, when the table initialises.
How do I load a different state object, for example, when the user selects a different set of columns to be visible?
Sure - you can have it read from where ever you want! It must be synchronous though.
My mistake with the reference to saveSave as a function - it isn't. As you say, it is a boolean parameter.
The callbacks you want are:
- `stateLoadCallback` : http://next.datatables.net/reference/option/stateLoadCallback
- `stateSaveCallback` : http://next.datatables.net/reference/option/stateSaveCallback
Annoyingly the stateSaveCallback documentation link is broken at the moment. I'll fix very shortly.
> How do I load a different state object, for example, when the user selects a different set of columns to be visible?
Oh I see - you want to change a bunch of parameters after the table has been initialised? The built in state save / load can only be restored at initialisation time. You would need custom code, using the API if you wanted to modify the table's state once it is up.
Allan
A long thread but I think I know what to do now, thanks.