Deleting Nodes/tr

Deleting Nodes/tr

ezos86ezos86 Posts: 22Questions: 0Answers: 0
edited December 2013 in General
When I run this:

[code]
$('.delete').click( function() {
var oTable = $('#demo').dataTable();
var anSelected = fnGetSelected(oTable,'Laf2');
console.log(anSelected);
if ( anSelected.length !== 0 ) {
for(q = 0; q < anSelected.length; q++){
oTable.fnDeleteRow(anSelected[q]);
}
}
[/code]

I have 500 rows showing on the first page, and it deletes all of those with .Laf2 class but it will not delete all of them from the overall table.

Ex: I have 518 .Laf2 classes - when I click this - it will remove 500, but 18 will be left because they were on another page. This is important to my project. It would be great to get some help on it. Thanks.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    What is `fnGetSelected` and how does it work? The problem is likely to be in there.

    If you fancy giving 1.10-dev a go you could do:

    [code]
    $('#demo').DataTable().rows( 'tr.Laf2' ).remove().draw();
    [/code]

    assuming when you say "class" you mean it is applied to the `tr` element?

    Allan
  • ezos86ezos86 Posts: 22Questions: 0Answers: 0
    Yes, that's right allan. The weird thing is, I think when I first did this, the fnDeleteRow was working for all [code]tr[/code] that existed.

    I will give 1.10 a try - do you have a link to load it through CDN?

    I want to delete all rows, on any page of my results that have the .Laf2 class. I figured there would be fnGetNodes method or function to do this.
  • ezos86ezos86 Posts: 22Questions: 0Answers: 0
    Here is this function - I got it from another forum post. oTableLocal is var oTable = $('#demo').dataTable();
    [code]
    function fnGetSelected( oTableLocal,loc )
    {
    return oTableLocal.$('tr.'+loc);
    }
    [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > I will give 1.10 a try - do you have a link to load it through CDN?

    Not yet. There will be when it goes beta.

    The method you are using at the moment should work, unless you are using server-side processing of course.

    Allan
  • ezos86ezos86 Posts: 22Questions: 0Answers: 0
    well it deletes all of the tr.Laf2 on the first page but not the second page. I don't think they are loaded into the DOM until they are on the first page, correct? so it looks like that don't have an associative class of Laf2 if I look at the console log output of all the nodes. they just say tr, or is a Class associated with them when they are on the second page? Would there be another method to Delete based on data? So if the row contained the 'Laf 2' the class is added in a fnRollCallback.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > I don't think they are loaded into the DOM until they are on the first page, correct?

    It depends upon your DataTables initialisation. Can you please link to a test page, or use the debugger so I can see how you are using DataTables and be able to help further.

    Allan
  • ezos86ezos86 Posts: 22Questions: 0Answers: 0
    Yeah - http://pivideos.com/dt/datatables.php

    Try this. Hit create - then Filter with 'Laf 2' - there will be 560 results. then hit delete. It will delete 500 but leave 60, it's deleting based on the class. Looking at the console output, it looks like the classes 'Laf2' are not added until it's displayed on the page.
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > var tr = $(nodes).filter('Laf2')[0];

    Should that not be:

    [code]
    var tr = $(nodes).filter('.Laf2')[0];
    [/code]

    ?

    Allan
  • ezos86ezos86 Posts: 22Questions: 0Answers: 0
    tr returns undefined either way. Not sure why. but that isn't actually associated with the delete problem, a few lines above.
  • ezos86ezos86 Posts: 22Questions: 0Answers: 0
    I got that from this discussion: http://datatables.net/forums/discussion/6629/get-value-of-row-on-another-page/p1
  • ezos86ezos86 Posts: 22Questions: 0Answers: 0
    I found a solution, maybe it will help someone else sometime.
    [code]
    function subtotal_calculate(field,i,stem){
    /* Add a 3rd varible that represents the type of Sum, such as Location or MID*/
    var oTable = $('#demo').dataTable();
    var data = oTable.fnGetData();
    var subtotal = 0;
    if(stem == 'Location'){
    var selection_current = selected_locations[i];
    } else {
    var selection_current = selected_mids[i];
    }

    for(w = 0; w < data.length; w++){
    if(data[w].User == ''){
    console.log(data[w][field]);
    } else {
    var value_check = data[w][stem];
    if(value_check == selection_current){
    //console.log(data[w][field]);
    subtotal += eval(data[w][field]);
    }
    }
    }
    return subtotal;

    }
    [/code]
This discussion has been closed.