14

I am trying to implement a function on jquery datatable, that returns the 1st and the 4th column of a clicked row

i am following this example, which allows me to manipulate a clicked row http://datatables.net/examples/api/select_single_row.html

thinking that i can change this handler to do the read cell value procedures and use the value on my own logic

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

i have also come over with this little code segment from dataTable forum http://datatables.net/forums/comments.php?DiscussionID=1384&page=1#Item_0

$('#example tbody tr').click( function () {
    // Alert the contents of an element in a SPAN in the first TD
    alert( $('td:eq(0) span', this).html() );
} );

may i have any pointer so i can get the 1st and 4th column of the clicked field?

next part I have the above solved, thanks nick

however i have the next part of the problem. when i init the table i use

/* Init the table */
    oTable = $('#filetable').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/crvWeb/jsonFileList.do",
        "fnServerData": function ( sSource, aoData, fnCallback ) {
            $.ajax( {
                "dataType": 'json', 
                "type": "POST", 
                "url": sSource, 
                "data": aoData, 
                "success": fnCallback
            } );
        }
    } );

my servlet takes a dir request parameter and returns a listing to the datatable as json response.

/crvWeb/jsonFileList.do

how can i add and get serlvet response with post request so i can have my table updated?

2 Answers 2

26

You can use .delegate() easiest here, like this:

$("#example tbody").delegate("tr", "click", function() {
  var firstCellText = $("td:first", this).text();
  var fourthCellText = $("td:eq(3)", this).text();
});

You can try out a demo here

With .delegate() this refers to the <tr> since that's the click we're handling, making things quite a bit cleaner..and it's still only one event handler at the <tbody> level, not one per <tr>.

Sign up to request clarification or add additional context in comments.

4 Comments

one more follow up question. if i would like to pass the value into the grid to update it, how i should do it to use the funtion of post to update the table on the fly ?
@nokheat - How do you mean? If you can describe a bit more about where the values comes from and goes I might be able to help...I have to crash for the night, but reply here and I'll check it in the morning :)
i need to show some code segment so please refer to my newly edit
@nokheat - Please take what you added and form a new question with it, since that's a totally different topic...it's better for a question/answer to be about one thing, it helps future googlers finding this better :)
0

This should do the trick, if I'm reading your code correctly:

$("tr.row_selected td:nth-child(1), tr.row_selected td:nth-child(4)");

It should return the first and fourth child of all tr elements with the class row_selected.

1 Comment

i am checking my code and your solution however i am still trying to work around it . the main convern is, can i really take a reference from the handler input ? like $("#example tbody"), and use the this identifier to get the reference of the cell and hence its value?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.