Problems with Delete Row

Problems with Delete Row

mvillavicenciomvillavicencio Posts: 6Questions: 0Answers: 0
edited September 2011 in General
Good afternoon. I'm using this and it's awesome! But I have a problem implementing the click handler for Delete row. I'm obtaining the data from server, the javascript code is written bottom.
I called the delete function in the html page:
Delete selected row

But it seems that I can't obtaining the row selected. I probed introducing "alert( anSelected[0] );" in the delete function to see what parameter I'm receiving, but the message that it displays is "[object HTMLTableRowElement]", instead the row.
Please your help! I would appreciate a lot!




var oTable;
var giRedraw = false;

$(document).ready(function() {

$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/serverprocessing.php"
} );

/* Add a click handler to the rows - this could be used as a callback */
//$("#example tbody").click(function(event) {
$("#example tbody").click(function(event) {
$(oTable.fnSettings().aoData).each(function (){
$(this.nTr).removeClass('row_selected');
});
$(event.target.parentNode).addClass('row_selected');
});

/* Add a click handler for the delete row */
$('#delete').click( function() {
var anSelected = fnGetSelected( oTable );
alert( anSelected[0] );
oTable.fnDeleteRow( anSelected[0] );
} );

/* Init the table */
var oTable = $('#example').dataTable( );

} );
/********************* */

/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
var aReturn = new Array();
var aTrs = oTableLocal.fnGetNodes();

for ( var i=0 ; i

Replies

  • allanallan Posts: 63,531Questions: 1Answers: 10,475 Site admin
    Because you are using server-side processing ( bServerSide ), fnDeleteRow won't really do very much. It will delete the row on the client-side, and then redraw the table, but because DataTables doesn't know anything about your sever-side (SQL, noSQL, CSV whatever) it can't delete the row for you - so the redraw will add it back in. Thus to delete a row you need to make a call to the server to have it do the delete from the data store and then call fnDraw.

    Allan
  • mvillavicenciomvillavicencio Posts: 6Questions: 0Answers: 0
    edited September 2011
    Allan, thanks for your answer. I understand that I have to call to the server to delete the row from the data store. But, before that, I need to select correctly the row. It seems that the first problem is there. I tried to select the row using the javascript line in the bottom, but, when I want to show the value of the variable that recieve the row selected, the message that it displays is "[object HTMLTableRowElement]", instead the row. I would appreciate any help!


    var anSelected = fnGetSelected( oTable );
    alert( anSelected[0] );
  • fbasfbas Posts: 1,094Questions: 4Answers: 0
    fnGetSelected gets the DOM row selected ([object HTMLTableRowElement] is the row)

    you can use fnGetPosition(anSelected[0]) to get the index of the row within DataTables

    fnDeleteRow can accept either the DOM row or the index of the row.
  • mvillavicenciomvillavicencio Posts: 6Questions: 0Answers: 0
    fbas, thank you very much. I solved my problem. Besides, once I get the position of the selected row, I needed to get de content of one specific column. So I did it using fnGetData. For anyone that in the future require to do this, I put the code:

    var anSelected = fnGetSelected( oTable );
    var aPos = oTable.fnGetPosition(anSelected[0]);
    var aData = oTable.fnGetData( aPos,here_the_number_of_column );
This discussion has been closed.