Enable/Disable features after initializing the table

Enable/Disable features after initializing the table

kinankskinanks Posts: 2Questions: 0Answers: 0
edited July 2013 in General
Hello,

I have a table that has this feature "bSort": false. Later if the user clicks on "Make table sortable" the datatable should be sortable. I can't figure out a way to change bSort: true after the datatable is loaded. I looked it up online, and some are saying it is impossible. Please let me know if it can be done, or if there is an alternative to fix my problem.

PS: I am new to JQuey, Jscript, so please try to be more specific.

Many thanks in advanced.

Replies

  • koosvdkolkkoosvdkolk Posts: 169Questions: 0Answers: 0
    The other options would be:
    - reconsider why you want to have this functionality (no kidding: why would you want to offer this functionality?)
    - create a new Datatable when user clicks 'Make table sortable', but this time with sorting enabled.
    - write your own sort-functionality using the fnSort function and add this functionality when user clicks 'Make table sortable'.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    You can get the settings object from your datatable and change things.

    I don't know where the table-wide sortable setting is, but here is an example of disabling sort on a certain column:

    [code]
    var oTable = $('#example').dataTable();

    // get settings object after table is initialized
    var oSettings = oTable.fnSettings();

    // disable sorting on column "1"
    oSettings.aoColumns[1].bSortable = false;
    [/code]
  • kinankskinanks Posts: 2Questions: 0Answers: 0
    Thanks I will try that
  • majortommajortom Posts: 29Questions: 0Answers: 0
    edited July 2013
    To add onto that answer, if you want table wide you could use a for-loop

    [code]
    var len = oTable.fnGetData(0).length;

    for (var i = 0; i < len; i++) {
    oSettings.aoColumns[i].bSortable = false;
    }
    [/code]

    I don't know if this works, but it's the right idea. You'd have to play with it
  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Modifying the options in the settings object is a sure fire way of breaking things unless you know the DataTables code very well. For example you can't just disable sorting on a column using bSortable since it won't remove the sort listener on the header.

    The short answer is that you cannot alter initialisation options after the table has been initialised, unless there is an API available for it. You need to reinitialise the table.

    Allan
This discussion has been closed.