Modify columns width before initilization

Modify columns width before initilization

dcpdcp Posts: 5Questions: 3Answers: 0

Good morning,

as Datatables doesn't support resizing out of the box, I am currently using the https://github.com/jeffreydwalter/ColReorderWithResize/blob/master/ColReorderWithResize.js which works okay.

One of the issue I am having currently, is trying to preserve the state (width) of the columns. I came with the idea to save the width of tables (using localStorage) or something, but I can't find a way to restore those before table initialization. Is there any way I can modify the column widths before table initilization?

Something like:

$(document).on( 'preInit.dt', function (e, settings) {
    var api = new $.fn.dataTable.Api( settings );
    ....
});

Any ideas will be really appreciated. Thanks.

Answers

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    https://datatables.net/reference/option/stateSave

    //Data tables default settings
        $.extend( true, $.fn.dataTable.defaults, {
            stateSave: true,
        } );
    

    If you don't want it for every table you can use this for an individual table:

    var yourTable= $('#tyourTable').DataTable( {
        dom: "Bfrltip",
        stateSave: false,
        ajax: { ....
    

    To allow the user to delete the saved states and go back to the defaults you could use something like this

    .......
    <li><a href="" onclick="localStorage.clear();" class="delLocalStorageLink"><span class="navDelLocalStorage">
    <span class="glyphicon glyphicon-erase pull-right"></span><?php echo $en?('Clear Saved Layouts'):('Layouts Zurücksetzen');?></span></a></li>
    ........
    
  • colincolin Posts: 15,176Questions: 1Answers: 2,589

    Hi @dcp ,

    As @rf1234 said, stateSave is the way to go. By default, column widths aren't saved - only ordering and filtering properties are. However, you can add to what's being saved by using stateSaveParams, and then you can reapply those settings on reload inside stateLoadParams,

    Cheers,

    Colin

This discussion has been closed.