How can I disable sorting, page size change and search when table has no data.
How can I disable sorting, page size change and search when table has no data.
I have a DataTable on my page which is initialized with no data and is using server side, I'm using this to return no data:
ajax: function(data, callback) {
if (data.draw == 1) {
callback({
'draw': 1,
'recordsTotal': 0,
'recordsFiltered': 0,
'data': []
});
return;
}
var column = data.columns[data.order[0].column].data;
var dir = data.order[0].dir;
$.ajax({
url: '/ssp/server_processing.php',
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: 'json',
async: true,
data: JSON.stringify(data)
}).done(function(data) {
callback(data); //here I' adding extra data
if (!added) {
new $.fn.dataTable.FixedHeader(table);
added = true;
}
$("span#gridLoading").hide();
});
},
I'm returning empty array when data.draw==1
My table is reloaded from external button click, so I would like to disable searching, sorting and page size change until my table will have data.
For now I'm disabling page size change using this:
drawCallback: function(settings) {
if (settings.aoData.length > 0) {
$(this).closest('.dataTables_wrapper').find('.dataTables_length select').prop('disabled', false);
} else {
$(this).closest('.dataTables_wrapper').find('.dataTables_length select').prop('disabled', 'disabled');
}
}
but maybe there is an easier way to do this?
I think that this should be done "by-design" and should be build-in by default.
So my question is: How can I disable sorting, page size change and search when table has no data.
Here is JS Bin to play: http://live.datatables.net/forakuzu/1/edit
This question has an accepted answers - jump to answer
Answers
You cannot dynamically enable and disable features in DataTables. Your current method is probably the best workaround available. The alternative is to create the initial table with those features disabled and then destroy it to be replaced with a new table when he data is available.
Allan
It would be nice to have this build in.
I think that it is unnecessary to sort or filter data if we have no data :)
I'm looking forward to see this build-in maybe someday.
For now I'm using this code
Maybe someone will find it useful.
Allan thank You for fast reply and as always any comments and fixes to code are welcome! :)
Ultimately yes, I want to add the ability to enable and disable a table (and therefore all ors input controls). Its on the list of things to do, but I'm not sure when I'll introduce it as there are a number of other things that are more pressing.
Regards,
Allan