Joofe Kopp Joofe Kopp - 2 months ago 17
Javascript Question

jQuery Datatable delete row function

Here is a function for jQuery datatables which deletes all rows which contains tree in the table with the id testtable.

function deletedatarow()
{
var oDatatables = $('#testtable').dataTable();
$("#Testtable tr td").filter(function ()
{
var x = "tree";
return $(this).text() === x;
}).closest('tr').each(function()
{
oDatatables.fnDeleteRow(this);
});
};


But this function is very slow on a big html side and i dont want to delete all rows with tree or treeapple which contains tree.

Is there a quicker function which only deletes a datatable row which contains exactly the value "tree"?

Jai Jai
Answer

Try this:

function deletedatarow() 
{
var oDatatables = $('#testtable').dataTable();
$("#Testtable").find("td").filter(function () 
    {
    var x = "tree";
    return $(this).text() === x;
    }).closest('tr').each(function() 
    {
    oDatatables.fnDeleteRow(this);
    });
};

or you can try this with :contains:

function deletedatarow(){
    var oDatatables = $('#testtable').dataTable();
    $("#Testtable").find("td:contains(tree)").closest('tr').each(function(){
      oDatatables.fnDeleteRow(this);
    });
};

Selector optimization is the way to boost up some operation in js as being said that dom is slow.

Comments