how to read cell values when row is selected

how to read cell values when row is selected

DenonthDenonth Posts: 76Questions: 0Answers: 0
edited September 2012 in General
Hi all,

Is there any example that shows how to read cells from the table when the row is selected?

Replies

  • girishmrgirishmr Posts: 137Questions: 0Answers: 0
    Hope this helps

    [code]
    //Assuming you are clicking on a cell ( or even a row)
    $("#TransDetails td:nth-child(9)").live("click", function (){

    // Get the position
    var aPos = oTable.fnGetPosition(this);

    // Get the row data
    var aData = oTable.fnGetData(aPos[0]);

    // Get the cell data
    var id = aData[0];
    ....
    var startDate = aData[8];

    });

    [/code]
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    Slightly shorter:

    [code]
    //Assuming you are clicking on a cell ( or even a row)
    $("#TransDetails td:nth-child(9)").live("click", function (){

    // Get the row data
    var id = oTable.fnGetData(this);

    });
    [/code]

    fnGetPosition is not needed since you can just pass the td element to fnGetData. If you pass in a TH/TD cell then you get the cell's value. If you pass in a TR (this.parentNode in the above case) you get an array (or object) with the data for the row.

    Allan
  • DenonthDenonth Posts: 76Questions: 0Answers: 0
    @allan So I am getting data as id[8] for example?
    How to highlight that row which is clicked?
  • allanallan Posts: 63,534Questions: 1Answers: 10,475 Site admin
    > @allan So I am getting data as id[8] for example?

    As I say:

    > If you pass in a TR (this.parentNode in the above case) you get an array (or object) with the data for the row.

    So in the case above, you are passing in a TD element, so you get the data for the cell.

    > How to highlight that row which is clicked?

    http://datatables.net/release-datatables/examples/api/select_row.html

    Allan
This discussion has been closed.