1

So I've got an implementation of dataTables plugin with pagination. I want a bootstrap modal to appear when I click on any row in the table. So far I can get the modal working when I click rows on the first page. But once I go to the next page none of the rows are clickable.

This is what I have so far:

    $(document).ready(function() {
        $("tr").click(function(){
            $('#myModal').modal('show');
        });
    });

I have a feeling that this can be done using DataTables API functions but my inexperience is holding me back. What is the simplest way of doing this?

1
  • hey buddy welcome to community . Post your html as well Commented Feb 27, 2013 at 16:56

3 Answers 3

2

Take a look at the fnGetData example on this page

http://www.datatables.net/api

  // Row data
$(document).ready(function() {
  oTable = $('#example').dataTable();

  oTable.$('tr').click( function () {
    var data = oTable.fnGetData( this );
    // populate your modal with the data from data variable which is the data that the row contains
    //show your modal
  } );
} );

when you save the data in your modal and close it, just reload the datatable ...

You really need to look at the api documentation, everything is there .. really

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

3 Comments

@theBookeyMan The paginated table doesn't have all of its rows in the DOM when the page loads; only the first page of table rows is in the DOM at that point, and DataTables dynamically adds/removes rows from the DOM as you page through your table. Your click event doesn't get bound to these dynamically-created rows because they don't exist in the DOM yet. Instead, you have to tell jQuery to listen to an element that does exist and is not removed/replaced during pagination, namely the table body.
Ok that makes sense. If jQuery is listening to the whole tbody element, can it differentiate between the table rows? I want a separate modal for each row.
well ... what is your logic for assigning a specific modal to a specific row ? what is the relationship between the table row and the modal ?
1

Use a delegated event:

$('#myTable tbody').on( 'click', 'tr', function () { ... } );

See http://datatables.net/faqs#events for more information.

Comments

0

Since no one actually posted the final answer, I'm following up with this because I had the same problem.

COS.coupon.table_columns_titles = new Array();
COS.coupon.table_columns_titles.push({"sTitle": "Code"});
COS.coupon.table_columns_titles.push({"sTitle": "Status"});
COS.coupon.table_columns_titles.push({"sTitle": "Date"});
COS.coupon.table_columns_titles.push({"sTitle": "","sClass": "hide"});

$j('#listcoupons').dataTable({
  "bProcessing":true,
  'bJQueryUI': true,
  'sPaginationType': 'full_numbers',
  'oLanguage': {'sSearch': 'Filter:', 'sEmptyTable': 'Processing...'},
  'aoColumns': COS.coupon.table_columns_titles,
  "sScrollX": "100%",
  "iDisplayLength": 10,
  "bAutoWidth": false
});

...

// So this is the on click. I'm referencing the tbody because it's there
// throughout all the pagination.
$j("table#listcoupons tbody").on("click", function(event) {

...

// This is how I'm refering the specific item I wanted.
// Then I do get the id from the hidden column.
  $j(event.target).closest('tr').children('td').eq(1).text()

The whole on click event looks like this ish.

$j("table#listcoupons tbody").on("click", function(event) {
    if ($j(event.target).closest('tr').children('td').eq(1).text() == 'Used') {
      // do some stuff/show a dialog
    } else {
      // do some other stuff/show a different dialog
    }
  });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.