Dynamic Column show/hide from saved state

Dynamic Column show/hide from saved state

rathinaganeshrathinaganesh Posts: 25Questions: 5Answers: 0

Hi,

I am trying to show/hide columns in the datatable using the state information stored in a database.

When the user hides few columns, and saves the information in the database using some profile name.

Later, when the user selects one of the saved profile names, from a dropdown, I need to -
1. Load the new state information from the database. How can I trigger the load state event?
2. How can I update the table with state information

I am able to do the save information. However, I am having trouble loading the saved state information.

I am using jquery.dataTables.js 1.10.6



stateLoadCallback: function (settings) { var o; //Get the selected profile name from the drop-down var profile_value = $("#selected_profile_name" ).val(); if(profile_value != null){ var url = "/state/"+user_name+"/"+table_id+"/profile1"; $.ajax({ "url": url, "dataType": "json", "type": "GET", "async":false }).done(function ( data ) { o = JSON.parse(data); console.log('In done of stateLoadCallback'+data); }).fail(function ( data ) { console.log('In fail of stateLoadCallback'+data); }); return o; } }

Thanks

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    1. Load the new state information from the database. How can I trigger the load state event?

    You can't I'm afraid.

    1. How can I update the table with state information

    You need to use the API methods to load the state. All of the state saved information can be set via the API. For example the column visibility information would be set by column().visible().

    The fact that there is no state.load() method at the moment is definitely an oversight on my part which will be fixed in future!

    Allan

  • rathinaganeshrathinaganesh Posts: 25Questions: 5Answers: 0

    Thanks. I fixed the issue using the column.visible API. But the performance is very slow.
    Is there an API to get the list of all the columns including the hidden ones?

    The following is for my understanding of the API -
    Even the stateLoadCallback function is not working correct.
    The stateLoadCallback is getting the right state information from the database but it is not getting refreshed on the table. (Although I am not using the stateLoadCallback function now because of the non-availability of state.load( ) method)
    I am using DataTables 1.10.6

    Thanks

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Is there an API to get the list of all the columns including the hidden ones?

    columns().

    Perhaps you can show me the code you used and I can take a look and see if there are any obvious areas where it can be changed to improve performance.

    Allan

  • rathinaganeshrathinaganesh Posts: 25Questions: 5Answers: 0

    Thanks Allan. I used the columns() API to manually show/hide the columns.

    //Iterate through the columns and show/hide the columns as per the saved profile
      var saved_cols = saved_json.columns;
      table.columns().eq(0).each( function ( index ) {
           var column = table.column( index );
           var col = saved_cols[index];
            column.visible(col.visible);
       } );
    
  • rathinaganeshrathinaganesh Posts: 25Questions: 5Answers: 0

    Is this the best way to show/hide the columns. This takes lot of time to process as I have more than 70 columns. For me to show/hide few of those columns takes more than 10 seconds. Is there a performance tuning we can do on it?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Yes, have a look at the column().visible() documentation. The second parameter can be set to false to speed things up. Then after the loop is finished call columns.adjust().

    Allan

This discussion has been closed.