fnDeleteRow isn't really deleting the row?
fnDeleteRow isn't really deleting the row?
I'm attempting to update a table with new information. If a row isn't visited during the update, we need to remove that row from the table. I wrote the following method that removes the selected row from the rendered output. However, the call to fnGetNodes() keeps returning the original number of nodes. Using firebug, I can see that the deleted row has some special attribute (it is greyed out). Calls to fnGetData() still have the deleted row in the returned result. I'm using datatables, version 1.5.2 at the moment.
I realize that I can change the code from fnGetNodes() to $('#theTable).find('tr').... But this handles only the visible rows. If I have a large table with pagination, I will miss deleting some rows that could later be displayed.
Hopefully, I'm doing something crazy.
// take a list of processed ids and remove any un-processed rows from the table.
function cleanUpOldIds(procid) {
var nodes = theTable.fnGetNodes();
var oid = [];
oid.length = 0;
var ii=0;
$(nodes).filter('tr').each( function() { oid[ii++] = $(this).attr('id'); });
alert(' number of ids before deletion: ' + oid.length + ', values=' + oid.join(',') + '\n number of processed ids: ' + procid.length + ', values=' + procid.join(','));
// slow!
for(ii=0; ii < oid.length; ++ii) {
if($.inArray(oid[ii], procid) == -1)
alert('must delete row for id=' + oid[ii]);
var pos = vmTable.fnGetPosition($('tr#' + oid[ii]).get(0));
vmTable.fnDeleteRow(pos);
}
}
var nodes = theTable.fnGetNodes();
var noid = []
noid.length = 0;
ii=0;
$(nodes).filter('tr').each( function() { noid[ii++] = $(this).attr('id'); }); /// this still has the data.
$('#vmTable tbody').find('tr').each( function() { noid[ii++] = $(this).attr('id'); }); // this works...
alert(' number of ids in table after deletion: ' + noid.length);
}
I realize that I can change the code from fnGetNodes() to $('#theTable).find('tr').... But this handles only the visible rows. If I have a large table with pagination, I will miss deleting some rows that could later be displayed.
Hopefully, I'm doing something crazy.
// take a list of processed ids and remove any un-processed rows from the table.
function cleanUpOldIds(procid) {
var nodes = theTable.fnGetNodes();
var oid = [];
oid.length = 0;
var ii=0;
$(nodes).filter('tr').each( function() { oid[ii++] = $(this).attr('id'); });
alert(' number of ids before deletion: ' + oid.length + ', values=' + oid.join(',') + '\n number of processed ids: ' + procid.length + ', values=' + procid.join(','));
// slow!
for(ii=0; ii < oid.length; ++ii) {
if($.inArray(oid[ii], procid) == -1)
alert('must delete row for id=' + oid[ii]);
var pos = vmTable.fnGetPosition($('tr#' + oid[ii]).get(0));
vmTable.fnDeleteRow(pos);
}
}
var nodes = theTable.fnGetNodes();
var noid = []
noid.length = 0;
ii=0;
$(nodes).filter('tr').each( function() { noid[ii++] = $(this).attr('id'); }); /// this still has the data.
$('#vmTable tbody').find('tr').each( function() { noid[ii++] = $(this).attr('id'); }); // this works...
alert(' number of ids in table after deletion: ' + noid.length);
}
This discussion has been closed.
Replies
What you need to do is pass "true" as the third parameter to fnDeleteRow (and null as the second) in order to delete the row completely from DataTables, rather than just the display arrays: http://datatables.net/api#fnDeleteRow . The reason for this is exactly what you are seeing, so it is possible to add rows back in if you want later (but you don't want this - so fnDeleteRow allows for this too :-) ).
Regards,
Allan
my code:
[code]
$('#delete').click( function() {
var anSelected = fnGetSelected( oTable );
oTable.fnDeleteRow( anSelected[0], null, true );
} );
...
function fnGetSelected( oTableLocal ){
var aReturn = new Array();
var aTrs = oTableLocal.fnGetNodes();
for ( var i=0 ; i
[code]
this.fnDeleteRow = function (mTarget, fnCallBack, bNullRow, bRemoveFromaoData) {
...
if (typeof bRemoveFromaoData != "undefined" && bRemoveFromaoData === true) {
oSettings.aoData.splice(iAODataIndex, 1);
}
...
[/code]
but this results in an error in _fnBuildSearchArray...
I could follow it through and edit even more in the js file, but I would not like getting too far away from the base line.
The function "prototype" in 1.7 beta is:
[code]
this.fnDeleteRow = function( mTarget, fnCallBack, bRedraw )
[/code]
Allan
That fixed it.
You are welcome to remove what i am posting from here - don't know where i should post it, so i'll just do it here:
I'm using a datatable with about 1500 entries and every tablerow is drawn with several inputs and selectors. Often I would like to display 100 or more entries consequently drawing the table is quite heavy. This becomes a problem when searching, so I have added a search button, so that you only search when clicking it or pressing enter - just going to leave the code here:
[code]
/*
* Function: _fnFeatureHtmlFilter
* Purpose: Generate the node required for filtering text
* Returns: node
* Inputs: object:oSettings - dataTables settings object
*/
function _fnFeatureHtmlFilter ( oSettings )
{
var nFilter = document.createElement('div');
var filterId = "undefined";
if (oSettings.sTableId !== '' && typeof oSettings.aanFeatures.f == "undefined")
{
filterId = oSettings.sTableId + '_filter';
nFilter.setAttribute( 'id', filterId );
}
nFilter.className = oSettings.oClasses.sFilter;
var sSpace = oSettings.oLanguage.sSearch==="" ? "" : " ";
nFilter.innerHTML = oSettings.oLanguage.sSearch + sSpace + '';
var jqFilter = $('#' + filterId + '_TextField', nFilter);
var jqFilterButton = $('#' + filterId + '_SearchButton', nFilter);
jqFilter.val(oSettings.oPreviousSearch.sSearch.replace('"', '"'));
jqFilter.keydown(function () {
if (event.keyCode == 13) { $('#' + filterId + '_SearchButton').click(); return false; }
});
jqFilterButton.click(function () {
/* Update all other filter input elements for the new display */
var n = oSettings.aanFeatures.f;
for ( var i=0, iLen=n.length ; i
Regards,
Allan
Is there something to this?
Joakim
Allan
Would it be an idea to make "fnRemoveData(aaData)" in a similar way as fnAddData(aaData)?
At the moment I'm working with a datatable with over 2000 entries - each with about 30 fields. I have experienced that fnDeleteRow in a loop is detected as a slow script by both firefox and IE (those I've tested) and will bring up the are-you-sure-you-want-to-continue-to-run-this-script-dialog obnoxiously many times, whereas calling fnClearTable followed by fnAddData is not detected this way i.e. I'm adding over 2000 entries each time - and still this seems to perform better.
Joakim
Allan