Best Method of Deleting Rows?
Best Method of Deleting Rows?
I am just curious on what the most efficient way of removing a row from the data tables would be. Below is the method I have chosen, but I suspect there is a quicker way to loop through.
[code]
//Clear Table Data
function ClearTableData(groupId)
{
var dataTableRows = userDataTableList.$('tr');
var groupId = "smith";
dataTableRows.each(function(){
var groupIdColumnData = $(this).find('td').eq(1).html();
var rowPosition = userDataTableList.fnGetPosition(this);
if (groupIdColumnData == groupId)
{
userDataTableList.fnDeleteRow(rowPosition,false);
}
});
//Re-Draw Table
userDataTableList.fnDraw();
}
[/code]
[code]
//Clear Table Data
function ClearTableData(groupId)
{
var dataTableRows = userDataTableList.$('tr');
var groupId = "smith";
dataTableRows.each(function(){
var groupIdColumnData = $(this).find('td').eq(1).html();
var rowPosition = userDataTableList.fnGetPosition(this);
if (groupIdColumnData == groupId)
{
userDataTableList.fnDeleteRow(rowPosition,false);
}
});
//Re-Draw Table
userDataTableList.fnDraw();
}
[/code]
This discussion has been closed.
Replies
The other option (although your solution looks absolutely fine!) would be to use $.map or the :contents selector to find the elements you want to delete, getting an array of elements and then looping over that to delete them.
Allan
Not at all no - its just an unnecessary step and can often lead to inadvertent errors. I'm trying to minimum its use as much as possible (v1.10 will make it much easier to not use it ever again :-) ).
Allan
Do you think you will be adding in the option to pass the TD element instead of Row/Col or TR/Col?
Most of my updates are done through user interaction with the TDs and so I'm finding myself using fnGetPosition on the TD in order to pass fnUpdate the Row/Col numbers.
[code]
table.cell( {cell selector} ).update( ... );
[/code]
rather than using fnUpdate and fnGetPosition .
There will be a backward compatibility shim for the old API to map onto the new one, but I think the old one has had its day now... :-)
Allan
I am using latest release DT 1.9.3 and was looking through the discussion lists for a better way of making column(s) (read as TDs) clickable and not making the whole TR clickable. The reason for this is if I have two different actions on a single row like Edit and Delete options with relevant icons made clickable, and while not using href, by clicking Edit opens a twitter bootstrap modal window. If at any point in time I click on delete which does not have any modal window associated with it, opens the Edit Modal window. To overcome this issue I have used the following code and is working as expected : Note: I am using fnRender to display the icons for each action in the columns
[code]
// For retrieiving the data for editing
$("#Remittance td:nth-child(8)").live("click", function () {
var rowIndex = oTable.fnGetPosition($(this).closest('tr')[0]);
var aPos = oTable.fnGetPosition(this);
var aData = oTable.fnGetData(aPos[0]);
var remitId = aData[0];
var remitStatus = aData[6];
// Get the remit data and load it into the modal
$.fn.getRemits(remitId);
});
//For deleting the record/ row clicked
// This just set the status as 'Active/Inactive' to enable and disable a record and will not be deleted
// from DB as such
$("#Remittance td:nth-child(9)").live("click", function () {
var aPos = oTable.fnGetPosition(this);
var aData = oTable.fnGetData(aPos[0]);
var remitId = aData[0];
var remitStatus = aData[6];
// Change the status of the remittance record in question
$('#processing').removeClass('hide invisible').addClass('alert alert-info');
$.fn.setRemitStatus(remitId, remitStatus);
});
[/code]
Instead of hardcoding the column number, is there a way to pass it as a parameter similiar to how we get the row Index using the following code
[code]
$(document).ready(function(){
var rowIndex = oTable.fnGetPosition($(this).closest('tr')[0]);
});
[/code]
But from the discussion just above mine, I read it as fnGetPosition() is going to get depricated softly.
From maintenance perspective of the app, when is the next release scheduled(honestly I haven't seen the road map, apologies) so that the new API can be used :)
Once again an awsome plugin and this is my 3rd project with DataTables and with each implementation, learning new ways of doing things.
A Big thank you to you and all those who are involved and to the discussion forum where I get most of my questions answered.
Thanks,
Girish