Changing bSort on the fly
Changing bSort on the fly
DudeSquare
Posts: 2Questions: 0Answers: 0
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?
[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?
This discussion has been closed.
Replies
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
$('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
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
"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
I've disabled initial sorting [code]"aaSorting": [][/code],
but after I added new data with
[code]for (var i=0; i
Thanks.
http://www.datatables.net/api#fnAddData
param 2 is boolean (default true) requesting a redraw/re-sort.
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]