Single Select Row Problems

Single Select Row Problems

nscottbrownnscottbrown Posts: 8Questions: 0Answers: 0
edited April 2014 in DataTables
I have the following code for the user selection of a single row. I can't seem to get this to work properly.
This code does not select anything. I know the event is firing because off my alert statement. If I comment out the line that references oTable variable the code will select the row but will not deselect any other rows. If I click on the row, it will deselect. Any help or suggestions would be great.

Thanks

[code]
$(document).ready(function() {

var oTable;

/* Init the table */
oTable = $('#example').dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers"
});
/* Add a click handler to the rows - this could be used as a callback */
$("#example tbody tr").click( function( ) {
alert('Click');
if ( $(this).hasClass('row_selected') ) {
$(this).removeClass('row_selected');
}
else {
oTable.$('tr.row_selected').removeClass('row_selected');
$(this).addClass('row_selected');
}
});

});
[/code]

Replies

  • fortunepjfortunepj Posts: 1Questions: 0Answers: 0
    I'm having the same problem. If I comment out the line:

    [code]oTable.$('tr.row_selected').removeClass('row_selected');[/code]

    I can select and de-select multiple rows. As soon as I include that line, however, I can select nothing. My goal is to be able to select and de-select just one row at a time. Here is my code:

    [code]

    <?php
    include 'includes/dbconnection.php';
    ?>



    DataTables example

    @import "/css/demo_page.css";
    @import "/css/demo_table.css";




    var oTable;
    $(document).ready(function() {
    $("#example tbody tr").click( function( e ) {
    if ( $(this).hasClass('row_selected') ) {
    $(this).removeClass('row_selected');
    }
    else {
    oTable.$('tr.row_selected').removeClass('row_selected');
    $(this).addClass('row_selected');
    }
    });
    oTable = $('#example').dataTable({
    "bJQueryUI": true,
    "sPaginationType": "full_numbers"
    });
    });
    function fnGetSelected( oTableLocal )
    {
    return oTableLocal.$('tr.row_selected');
    }







    First Name
    Last Name
    Phone



    <?php
    $sql="SELECT First_Name, Last_Name, Phone_1 FROM Patient";
    $result=mysql_query($sql);
    while($row=mysql_fetch_array($result)){
    echo "".$row['First_Name']."".$row['Last_Name']."".$row['Phone_1']."";
    }
    ?>



    First Name
    Last Name
    Phone





    [/code]

    The dbconnections file is exactly what it sounds like, and has no css, jquery, javascript, or html; just php code that connects to my database.

    Thank you very much in advance for your help!
  • nscottbrownnscottbrown Posts: 8Questions: 0Answers: 0
    I was able to get this working by changing the

    oTable.$('tr.row_selected').removeClass('row_selected');

    To

    $('tr.row_selected').removeClass('row_selected');

    Now when I click on 1 row all the other rows are deselected and the one I click is selected.
This discussion has been closed.