fnDelete with third parameter set to true messes up the table

fnDelete with third parameter set to true messes up the table

douglazdouglaz Posts: 3Questions: 0Answers: 0
edited September 2009 in Bug reports
Hello,

I'm working with a completely dynamic datatable (version 1.5.1). I need to add and remove rows on the fly. After using fnDelete(pos, null, true) for the first time, I get errors like the following:

When I try to call fnGetPosition

oSettings.aoData[i] is null
(line 1292)

fnGetNodes:

oSettings.aoData[i] is null
(line 3677)

My code is something like:

[code]
var added_items_table = $j('#graph_added_items_table');
added_items_table.dataTable();

$j('#graph_button_add_serie,#graph_button_add_group_serie').click(function(event) {
var nodes = items_table.fnGetNodes();
var selected = fnGetSelected(nodes);
var added_nodes_indexes = [];
if (this.id == 'graph_button_add_serie') {
for (key in selected) {
var pos = items_table.fnGetPosition(selected[key]);
var data = items_table.fnGetData(pos);
// I get a error here after using fnDelete
var table_length = added_items_table.fnGetNodes().length;
var radio_check = table_length == 0 ? 'checked' : '';
var radio_ranking_str = '';
key_json = $j.toJSON([data[1]]);
var delete_image = ' ';
var index = added_items_table.fnAddData([delete_image, radio_ranking_str, data[0], data[0], 'avg', '', key_json], 0);
if (index >= 0)
added_nodes_indexes.push(index);
}
} else if (this.id == 'graph_button_add_group_serie') {
if (selected.length > 0) {
var key_array = [];
var name_array = [];
for (key in selected) {
var pos = items_table.fnGetPosition(selected[key]);
var data = items_table.fnGetData(pos);
key_array.push(data[1]);
name_array.push(data[0]);
}
var key_json = $j.toJSON(key_array);
var name_str = name_array.join(', ');
// I get a error here after using fnDelete
var table_length = added_items_table.fnGetNodes().length;
var radio_check = table_length == 0 ? 'checked' : '';
var radio_ranking_str = '';
var delete_image = ' ';
var index = added_items_table.fnAddData([delete_image, radio_ranking_str, name_str, name_str, 'avg', '', key_json], 0);
if (index >= 0)
added_nodes_indexes.push(index);
}
}
$j(nodes).filter('.row_selected').removeClass('row_selected');
added_items_elements = [];
// only apply the below setting for the added items
for (key in added_nodes_indexes)
added_items_elements.push(added_items_table.fnGetNodes(added_nodes_indexes[key]));
var added_items_nodes = $j(added_items_elements);

added_items_nodes.find('.ss_delete').parents('td').click(function(event) {
$j(this).parents('tr').fadeOut('fast', function() {
// I get an error here after the first delete
var pos = added_items_table.fnGetPosition(this);
added_items_table.fnDeleteRow(pos, null, true);
});
});

added_items_table.fnDraw();
});

[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Hi douglaz,

    Nice catch - thanks for that. A bug in 1.5.1 (and everything before it!).

    To fix the fnGetNodes issue, you can replace the function _fnGetTrNodes with:

    [code]
    /*
    * Function: _fnGetTrNodes
    * Purpose: Return an array with the TR nodes for the table
    * Returns: array array:aData - TR array
    * Inputs: object:oSettings - dataTables settings object
    */
    function _fnGetTrNodes ( oSettings )
    {
    var aNodes = [];
    var iLen = oSettings.aoData.length;
    for ( var i=0 ; itd', oSettings.aoData[i].nTr)[j-iCorrector] == nNode )
    if ( oSettings.aoData[i] != null &&
    oSettings.aoData[i].nTr.getElementsByTagName('td')[j-iCorrector] == nNode )
    {
    return [ i, j-iCorrector, j ];
    }
    }
    else
    {
    iCorrector++;
    }
    }
    }
    }
    return null;
    };
    [/code]

    I'm sort of not sure about my replacement _fnGetTrNodes() function. It puts nulls into the return array, but retains the fnGetPosition() results... Getting a little nervous about having nulls in aoData at all...

    It would be great to hear how you get on with this.

    Thanks,
    Allan
  • douglazdouglaz Posts: 3Questions: 0Answers: 0
    Hello Allan,

    I only will be able to test the new code tomorrow. But now I must warn you that fnGetData isn't working either (a problem on line 3660 I think). Please take a look at it too.

    Thanks for all,

    douglaz
  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited September 2009
    Another function needed then:

    [code]
    /*
    * Function: _fnGetDataMaster
    * Purpose: Return an array with the full table data
    * Returns: array array:aData - Master data array
    * Inputs: object:oSettings - dataTables settings object
    */
    function _fnGetDataMaster ( oSettings )
    {
    var aData = [];
    var iLen = oSettings.aoData.length;
    for ( var i=0 ; i
  • douglazdouglaz Posts: 3Questions: 0Answers: 0
    Allan,

    Now it works. Thanks!

    --
    douglaz
This discussion has been closed.