Problem with row select and delete example on site

Problem with row select and delete example on site

naturalgoodnessnaturalgoodness Posts: 3Questions: 1Answers: 0
edited July 2011 in General
The following example is not working as expected:

http://www.datatables.net/examples/api/select_single_row.html

When the page first loads (i.e. no rows are selected) then clicking on the delete selected row link does delete rows. I guess this isn't how it meant to be - surely when the page first loads nothing should be selected and clicking the link shouldn't delete anything.

Does anyone know how to modify the code to stop this from happening? Thanks in advance!!

Replies

  • resnblresnbl Posts: 3Questions: 0Answers: 0
    The example is indeed busted as described above. The problem is fnGetSelected() returns an empty array when no items are selected. The delete click handler ignores this fact and passes the first item of the array ("undefined"?) to fnDeleteRow which interprets this parameter as "row index #0" and promptly deletes the first table row. Note that after doing this, clicking on a header item to perform a column sort no longer works, and the pagination results are no longer correct. (I think fnDeleteRow needs better parameter validation...).

    The solution is to modify the click handler to something like:
    [code]
    var anSelected = fnGetSelected( oTable );
    if (anSelected.length > 0)
    oTable.fnDeleteRow( anSelected[0] );
    else
    alert('Please select a row...');
    [/code]

    A few other notes on the "row selection" examples:
    1) The "preamble" of the several row selection examples contains the sentence "The example below uses the fnRowCallback()...", but none of them actually include fnRowCallback() in the code.
    2) There appears to be 2 ways to pick rows for selection: either go through the aoData list or via jQuery selector ("#example tr"). Under what conditions does one work when the other fails?
    3) The selector approach seems overly broad: it assigns 'row_selected' to rows in thead and tfoot as well as tbody. (This doesn't affect the example pages because the example CSS is more restrictive and only applies highlighting to rows in tbody, but this could trip up someone who crafts their CSS from scratch.) I would recommend changing the selector to $('#example tbody tr').
This discussion has been closed.