Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear DataTable with onchange in jQuery?

I have used fnDestroy() to clear the dataTable on change of a select element, but it doesn't clear the table, instead it appends the data into the table. Here's the code.

HTML:

<div class="col-sm-2">
    <select class="form-control" id="changeView">
        <option value="1">All</option>
        <option value="2">Compiled</option>
        <option value="3">On-Going</option>
        <option value="4">Cancelled</option>
    </select>
</div>

<table class="table table-condensed" id="documentsTable">
    <thead>
        <tr>
           <th>From</th>
           <th>Status</th>
           <th>Subject</th>
           <th>Date Received</th>
           <th>Due Date</th>
           <th>Actions</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

JQuery:

$('#changeView').on('change', function() {
    var value = $(this).val();
    var change = '';
    if (value === '1') {
        change = 'All';
    } else if (value === '2') {
        change = 'Compiled';
    } else if (value === '3') {
        change = 'On-Going';
    } else if (value === '4') {
        change = 'Cancelled';
    }

    loadDocuments(change);
});

function loadDocuments(change) {
    $('#documentsTable').dataTable().fnDestroy();

    $.post(base_url + "admin/document/getAllDocuments", {change: change}, function(response, status) {
        var result = JSON.parse(response);

        $.each(result, function(i, field) {
            var dateReceived = format_mysqldate(field['dateReceived']);
            var dueDate = format_mysqldate(field['dueDate']);

            $('#documentsTable tbody').append('<tr><td>' + field['from'] + '</td><td>' + field['status'] + '</td><td>' + field['subject'] + '</td><td>' + dateReceived + '</td><td>' + dueDate +
                    '</td><td><div class="visible-md visible-lg visible-sm visible-xs btn-group">' +
                    '<a href="' + base_url + 'admin/document/view/' + field['id'] + '" class="btn btn-primary btn-xs" title="View Details" data-toggle="tooltip"><i class="glyphicon glyphicon-search"></i></a>' +
                    '<a href="' + base_url + 'admin/document/edit/' + field['id'] + '" class="btn btn-success btn-xs" title="Edit Document" data-toggle="tooltip"><i class="glyphicon glyphicon-pencil"></i></a>' +
                    '</div></td></tr>');
        });
        $('#documentsTable').dataTable({
            "aoColumns": [
                null,
                null,
                null,
                null,
                null,
                {"bSortable": false}
            ],
            "order": [[3, "desc"]],
            "bDestroy": true
        });
    });
}

Can any one tell me what is wrong with my code? I can't get to clear the dataTable in every change of selection. Thank you ahead.

like image 446
TheGPWorx Avatar asked Oct 29 '14 14:10

TheGPWorx


4 Answers

You may try to clear the datatable first and then destroy it.

dataTable.fnClearTable();
dataTable.fnDraw();
dataTable.fnDestroy();
like image 165
Kursad Gulseven Avatar answered Nov 15 '22 22:11

Kursad Gulseven


Clear and destroy methods could be called like this,

var table = $('#example').DataTable();

//clear datatable
table.clear().draw();

//destroy datatable
table.destroy();

Reference;

  • Clear API method
  • Destroy method

Note: This code will work for datatable version 1.10 and above.

like image 31
Lucky Avatar answered Nov 15 '22 21:11

Lucky


It could be destroy() http://datatables.net/reference/api/destroy(). Then empty the tbody.

$('#documentsTable').dataTable().destroy();
$('#documentsTable tbody').empty();
like image 20
cforcloud Avatar answered Nov 15 '22 23:11

cforcloud


Use This Code to clear and destroy datatable

    $('#example1').dataTable().fnClearTable();
    $('#example1').dataTable().fnDraw();
    $('#example1').dataTable().fnDestroy();
like image 35
Arun Thimas Avatar answered Nov 15 '22 22:11

Arun Thimas