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.

MisiuMisiu Posts: 68Questions: 4Answers: 2
edited September 2014 in Free community support

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

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    Answer ✓

    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

    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

  • MisiuMisiu Posts: 68Questions: 4Answers: 2
    edited September 2014

    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

    drawCallback: function(settings) {
        if (settings.aoData.length > 0) {
            $(this).closest('.dataTables_wrapper').find('.dataTables_length select').prop('disabled', false);
            $(this).closest('.dataTables_wrapper').find('.dataTables_filter input').prop('disabled', false);
            $.each(settings.aoColumns, function(index, col) {
                col.bSortable = true;
            });
        } else {
            $(this).closest('.dataTables_wrapper').find('.dataTables_length select').prop('disabled', 'disabled');
            $(this).closest('.dataTables_wrapper').find('.dataTables_filter input').prop('disabled', 'disabled');
            $.each(settings.aoColumns, function(index, col) {
                col.bSortable = false;
            });
        }
    },
    

    Maybe someone will find it useful.

    Allan thank You for fast reply and as always any comments and fixes to code are welcome! :)

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin

    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

This discussion has been closed.