Trying to delete ALL rows permanently

Trying to delete ALL rows permanently

jackkerouacjackkerouac Posts: 3Questions: 0Answers: 0
edited September 2010 in General
Hello,

I am trying to delete ALL the rows in my table permanently when a user clicks a button outside the table. I am using the following code to get the number of table rows and loop through them, but it isn't working.

var oSettings = $('.dataTable').dataTable().fnSettings();
var iTotalRecords = oSettings.fnRecordsTotal();
for (i=0;i<=iTotalRecords;i++) {
$('.dataTable').dataTable().fnDeleteRow(i,null,true);
}

Any ideas?

Replies

  • jackkerouacjackkerouac Posts: 3Questions: 0Answers: 0
    Oh, forgot to add - right after the data is deleted, new data is added. The problem is, the new data is repeating the previous data that was there. That's how I know the original rows haven't been deleted.
  • jackkerouacjackkerouac Posts: 3Questions: 0Answers: 0
    Okay, I figured it out. Just needed to change the code to:

    var oSettings = $('.dataTable').dataTable().fnSettings();
    var iTotalRecords = oSettings.fnRecordsTotal();
    for (i=0;i<=iTotalRecords;i++) {
    $('.dataTable').dataTable().fnDeleteRow(0,null,true);
    }

    So it deletes the first row every time until all of them are gone.
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Which version of DataTables are you using? 1.7 will properly delete the rows, 1.6 won't truly do so ( http://datatables.net/upgrade/1.7 ). Either way though, the quickest way to delete all data is to use the fnClearTable API function: http://datatables.net/api#fnClearTable .

    Allan
  • holobeatholobeat Posts: 5Questions: 0Answers: 0
    Hi Alan,

    The fnClearTable() for some reason does not remove rows in my table. I am using dataTables version 1.7.4. This is my initialization function:

    [code]
    function InitTable ( url , elementname ) {
    var T = $(elementname).dataTable( {
    "bProcessing": true,
    "bPaginate" : false,
    "bSort" : false,
    "bLengthChange" : false,
    "bScrollInfinite" : false,
    "sScrollY" : '500px',
    "bScrollCollapse": false,
    "bFilter": false,
    "bServerSide": true,
    "sAjaxSource": url
    });
    return T;
    }
    [/code]

    It loads the data and returns the variable of dataTables object I am able to manipulate, but the method fnClearTable() does not delete rows in the table.

    Gustav
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    That's because you are using server-side processing. The fnClearTable call will delete all client-side rows, and then do a redraw - which gets information from the server and displays it - hence the rows are shown again!

    If you want to delete all rows from the database, you'll need to make a call to the server to tell it to delete the data, and then redraw the table. The reason for this is that DataTables can't know how your server is set up - so the API functions can only manipulate data on the client side.

    Allan
  • holobeatholobeat Posts: 5Questions: 0Answers: 0
    Thank you for your heplful insight. I just did exactly that. I created a simple controller method on the server that returns json response with no data:

    [code]
    public function resetAction()
    {
    echo '{"sEcho":'.intval($_GET['sEcho']).',"iTotalRecords":0,"iTotalDisplayRecords":0,"aaData":[]}';
    $this->getHelper('viewRenderer')->setNoRender();
    }
    [/code]

    ...and it works like a charm.

    Thank you,
    Gustav
This discussion has been closed.