Custom Column Order & Visibility

Custom Column Order & Visibility

Ironwil616Ironwil616 Posts: 50Questions: 0Answers: 0
edited September 2011 in DataTables 1.8
Ideally, what I want to do is set the visibility and order of the columns in my table at runtime. I do not want to do this via server-side code. It's also not an option to send this data to the server for a SQL query. The list of objects that populates my table aren't simple database objects. I have to aggregate data from multiple tables and resolve this at runtime. Custom views are created at runtime, so this won't be static.

What I need to do is take in a list of columns and render them in that order. All columns not in the list should not be visible at all. Rewriting my server code to return this dynamically isn't a viable option for me now. I just want to return the complete table, and set order and visibility based on the list of columns in the datatable options. Is this possible to do?

Replies

  • Ironwil616Ironwil616 Posts: 50Questions: 0Answers: 0
    OK, I found the answer (or an answer). It would be very inconenient at this time to rewrite my server code to return the necessary JSON data, so the table needs to be handled mostly client-side. Custom views can allow users to select any number and order of available columns to be visible. Tomorrow I'll create a new model for the View I'm loading that contains the collection being rendered in the table, a collection of integers for the visible columns in the order they should appear, and a collection of integers for columns that should not be visible. It will look something like this:

    [code]

    $(document).ready(function () {
    var oTable = $('#example').dataTable({
    "sDom": 'Rlfrtip',
    "oColReorder": {
    "aiOrder": [0 , 4, 3, 2, 1]
    },
    "aoColumnDefs": [
    { "bVisible": false, "aTargets": [3] }
    ]});
    });

    [/code]

    My table has a possible 28 columns, so I just used this combination of examples from the website. I've tested them together, and they work as expected. I read that 'aoColumnDefs' are supposed to be able to accept a string as well as an integer index, and I thought that meant that 'aTargets' would accept the string. This didn't seem to be the case. Having the columns accessible by table head cell values would be better than integer indexes, and maybe I just haven't found the correct means yet. I'll look over it more tomorrow. At the very least, I have a workable solution, which is great, as time is critical right now.
  • allanallan Posts: 63,494Questions: 1Answers: 10,470 Site admin
    The best way of separating the order and structure of the data source from the order from what is displayed in the table is to use the mDataProp option that was introduced in 1.8: http://datatables.net/blog/Extended_data_source_options_with_DataTables . With this property, you can give DataTables an array of objects, rather than an array of arrays, allowing to you pick out whatever data you want from the source object. It also means you don't need to have hidden columns just to store extra data (if that is something you need), since the original data source object is retained and available with fnGetData.

    Good to hear you got a workaround for now with ColReorder though :-)

    Regards,
    Allan
This discussion has been closed.