How to get definition via column() function

How to get definition via column() function

AllanCochraneAllanCochrane Posts: 41Questions: 1Answers: 2
edited February 2014 in DataTables 1.10
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.

Replies

  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    You could use `fnSettings().aoColumns[ columnIndex ]` / `settings()...` in 1.10) - however, as I just wrote in another thread, the settings object is considered to be private and can, and will, change between versions.

    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
  • AllanCochraneAllanCochrane Posts: 41Questions: 1Answers: 2
    I can retrieve the columns from the definition object to get mData, but I do think that there ought to be a public API call to retrieve dynamic information such as current visibility, position, order (i.e. asc/desc) and search values.

    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.
  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    edited February 2014
    > current visibility:

    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
  • AllanCochraneAllanCochrane Posts: 41Questions: 1Answers: 2
    When I wrote order I mean the ascending or descending order of the data, i.e. is the data to be displayed in largest to smallest or smallest to largest order, not the data itself.

    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
  • AllanCochraneAllanCochrane Posts: 41Questions: 1Answers: 2
    Here's an example of what I'm trying to achieve: http://jsfiddle.net/ucP7s/8/
  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    edited February 2014
    > is the data to be displayed in largest to smallest or smallest to largest order, not the data itself.

    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
  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    Ability to use `column[s]().visible()` as a getter or setter committed here: https://github.com/DataTables/DataTablesSrc/commit/9e8608fd . It will be in the nightly very shortly (once the build completes).

    Thanks for the suggestion to add this!

    Allan
  • AllanCochraneAllanCochrane Posts: 41Questions: 1Answers: 2
    Can you check the fiddle http://jsfiddle.net/ucP7s/9/ as it doesn't use column().order() or columns().order(). I assume it's just a case of adding column(i).order('asc') or the like.

    Is the findSortDir() function an example of how to get the current sorting direction?
  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    I don't quite understand I'm afraid. What are you trying to do with `column().order()` ? Are you trying to restore the state by setting the values using the method? If so, why not just save the order using the return from `order()` and then set the order using `order( savedOrder )` . That will allow multi-column ordering to be saved as well.

    > 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
  • AllanCochraneAllanCochrane Posts: 41Questions: 1Answers: 2
    My use case: I am trying to save the state of the whole table (column position, asc/desc order, visibility) in a database and restore it. The user moves the columns around, hides them and sets the desired asc/desc sort and then saves the state. The user can then rearrange the same table's columns and hide/reveal columns etc and save that new arrangement as another state. There may be many states from which the user can then choose to apply to a table and the table should rearrange columns, hide them and apply sorting and then finally fetch the appropriate data. The idea is that other users can pick up the first user's table arrangement and not have to go through the arranging of columns themselves.

    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.
  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    > The documentation is misleading because this page (http://next.datatables.net/reference/api/) says that order() redraws the table, so I never investigated it.

    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
  • AllanCochraneAllanCochrane Posts: 41Questions: 1Answers: 2
    [quote]
    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()
  • AllanCochraneAllanCochrane Posts: 41Questions: 1Answers: 2
    OK, so I'm back again :-)

    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?
  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    > One possible issue is that stateSave()/stateLoad() writes/reads a private object.

    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
  • AllanCochraneAllanCochrane Posts: 41Questions: 1Answers: 2
    That's the approach I'm going with; record the state via stateSaveCallback() into a variable somewhere whenever the user makes changes, when the user is satisfied with those changes then save that variable into the database. When the user wants to apply a previously saved state then make the table change via the public API.

    A long thread but I think I know what to do now, thanks.
This discussion has been closed.