Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery dataTables - how to extract row data onclick event

I'm trying to familiarize myself with the jquery dataTables plug in: http://www.datatables.net/manual/server-side#Sent-parameters

What's Working

I have json data being returned from the server to my client and the table is being populated.

What's Not working

I need to be able to capture the data in a given row, when the row is selected / clicked on by the end user.

Here's my code: http://jsfiddle.net/e3nk137y/1515/

$("#users tr").click(function(){
  alert($(this).find("pID").val());
   });

The above javascript is what I've been playing around with. Trouble is that the ajax call happens automagically and I'm not the one creating the rows in the table. Ultimately, I need to be able to grab some of the fields in each row, in addition to the ID / pID

What I've tried so far

Besides the playing with the javascript code, I've also been reviewing this: http://datatables.net/examples/api/select_single_row.html But in that example, all the data is defined client side, so it makes it easy to specify either an id or a class for each table row

Any suggestions would be appreciated.

like image 538
dot Avatar asked May 12 '15 12:05

dot


People also ask

How can I get specific row from DataTable?

The DataTables rows() and row() (also optionally cells() and cell() ) methods provide the ability to select rows from the table.

HOW CAN GET row column value from DataTable in jQuery?

How to get all rows data from DataTable in jQuery? You can use rows(). data() to get the data for the selected rows.

What is Rowcallback in DataTable?

This callback allows you to 'post process' each row after it have been generated for each table draw, but before it is rendered into the document. This means that the contents of the row might not have dimensions ( $(). width() for example) if it is not already in the document.

What is stateSave in DataTables?

DataTables saves the state of a table (its paging position, ordering state etc). When the stateSave option is enabled, it can be restored when the user reloads a page, or comes back to the page after visiting a sub-page.


1 Answers

Hope this is what you r looking for

Html

<table id="users" class="display" width="100%" cellspacing="0">
    <thead>
        <tr>
            <th id="pID">ID</th>
            <th>Name</th>
            <th>Code</th>
            <th>Available</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1-1</td>
            <td>1-2</td>
            <td>1-3</td>
            <td>1-4</td>
        </tr>
        <tr>
            <td>2-1</td>
            <td>2-2</td>
            <td>2-3</td>
            <td>2-4</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Code</th>
            <th>Available</th>
        </tr>
    </tfoot>
</table>

Script

$(document).ready(function () {   
    var table = $('#users').DataTable();
    $('#users tbody').on('click', 'tr', function () {
        console.log(table.row(this).data());
    });
});

Fiddle Link

like image 96
Aru Avatar answered Oct 05 '22 18:10

Aru