Changing bSort on the fly

Changing bSort on the fly

DudeSquareDudeSquare Posts: 2Questions: 0Answers: 0
edited October 2010 in General
For my application users need to be able to turn table sorting on or off by click on a button, but I cannot get it to work. I have tried (after initiating with oSort: false):

[code]
var oSettings = oTable.fnSettings();
oSettings.oInit.bSort = true;
oTable.fnDraw();
[/code]

But it doesn't work. Am I looking at it the wrong way or is it not possible to change it after initialization?

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    It is not currently possible to change feature parameters on the fly - which is why there is no public API for it. It is possible to "get away" with changing some parameters such as the processing indicator, but not others (again why there is no public API :-) ).

    There are three ways to do what you want to do though:

    1. Detach the sort listeners from the table header when not sorting (unbind('click');) and then attach them again your self using fnSortListener when you want to allow sorting.

    2. A variation on 1 - remove DataTables' default event handlers, and then apply your own which will check if it should sort before doing the sort.

    3. Destroy the table and re-initialise it with bSort set to the new value.

    Allan
  • DudeSquareDudeSquare Posts: 2Questions: 0Answers: 0
    Option 3 it is! Thanks for the quick reply, Allan!
  • azzazeliazzazeli Posts: 9Questions: 0Answers: 0
    Allan, could you give an example how to detach sort listeners from table header, and them attach same listeners to the table. Thanks.
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    [code]
    $('thead th').unbind('click');
    oTable.fnSortListener( $('thead th:eq(0)')[0], 0 );
    oTable.fnSortListener( $('thead th:eq(1)')[0], 1 );
    [/code]

    Here is a running example showing fnSortListener in action: http://live.datatables.net/evijax/edit

    Allan
  • azzazeliazzazeli Posts: 9Questions: 0Answers: 0
    edited November 2011
    Allan, thanks for response.
    But I have a little problem with enabling sorting at runtime.

    Here is my sample:
    [code]
    $('#example').dataTable({
    "sAjaxSource": "getData",
    "sScrollY": "100%",
    "bPaginate": false,
    "bFilter": false,
    "bProcessing": true,
    "bSort": false,
    "bInfo": true,
    "bStateSave": true,
    });

    $('#fetch_more').click(function(event){
    $.getJSON("getMoreData" function(json){
    for (var i=0; i
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    [code]
    "bSort": false,
    [/code]

    You've got the sorting feature disabled - that will result in sorting simply not occurring, regardless of anything else.

    Is it just that you don't want an initial sort? If so you can set aaSorting to an empty array (i.e. "[]").

    Allan
  • azzazeliazzazeli Posts: 9Questions: 0Answers: 0
    Thanks Allan.
    I've disabled initial sorting [code]"aaSorting": [][/code],
    but after I added new data with
    [code]for (var i=0; i
  • azzazeliazzazeli Posts: 9Questions: 0Answers: 0
    edited November 2011
    I think that when I add new row to the table (using fnAddData) the table is redrawed taking account of sorting. Am I right? Is there a possibility to add new row to the table without taking acount of sorting?

    Thanks.
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    fnAddData (like most draw/data routines) has a paramater to that controls redraw/re-sort.

    http://www.datatables.net/api#fnAddData

    param 2 is boolean (default true) requesting a redraw/re-sort.
  • azzazeliazzazeli Posts: 9Questions: 0Answers: 0
    edited November 2011
    Thanks, fbas.
    Your advice was very helpful. I think I've found the solution. New rows should be added in the following mode:
    [code]
    $('#example').dataTable().fnAddData(json.aaData, false);
    $('#example').dataTable().fnDraw(false);
    [/code]
This discussion has been closed.