Cell click problem

Cell click problem

richard90richard90 Posts: 5Questions: 0Answers: 0
edited August 2012 in General
Hi, First of all this is an amazing plugin and it's been great working with it, but I have just come into a problem that has left me scratching my head.

I need to be able to click a cell of the table and depending on which cell is clicked, a different modal will appear. However, the code I have been using will only work on the first row, even when pagination is turned off.

An example of my code:

[code]
$('#detailsTable tbody tr td:eq(3)').live('click', function(){
var data = oTable.fnGetData(this);
$("#modalHeader").append("Shipping Information");
$("#modalBody").append("From our records, it seems that " + data + " item from the quantity that you have ordered have left our stores and are currently on the way to you.");
$("#modal-window").modal('show');
});
[/code]

I have tried using delegate and also on but I can only get it to even come close to working when using live.

Any help or suggestions are greatly appreciated

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Its only getting the 4th TD matched - not the 4th TD from every row.

    There are a few ways of doing it in jQuery:

    [code]
    $('#detailsTable tbody tr').find('td:eq(3)')...
    [/code]

    or perhaps better for a live event handler:

    [code]
    $('#detailsTable tbody tr>td:nth-child(3)')...
    [/code]

    Allan
  • richard90richard90 Posts: 5Questions: 0Answers: 0
    Thanks for the quick response. I have amended the code to what you have suggested for a live event handler but it doesn't work on any row now. I am also not getting any errors either. I am also quite new to jQuery so sorry for being a "newbie"
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Try this: http://live.datatables.net/uwexaw/edit#source .

    Note that I've used jQuery 1.7's `on` rather than `live` as `live` is deprecated.

    Allan
  • richard90richard90 Posts: 5Questions: 0Answers: 0
    I've just tried that, changed from .live to .on but for some reason it still only works on the first row of data, and when I try to use nth-child(3) instead of eq(3), it doesn't even work on the first row.
  • richard90richard90 Posts: 5Questions: 0Answers: 0
    The table is initialised and filled using the JSON data that's returned, using the success of a .ajax call, could this be a problem?
  • richard90richard90 Posts: 5Questions: 0Answers: 0
    I solved the problem, turns out that I was calling .on in a wrong way. Thanks for your help and thanks again for the amazing plugin. Keep up the good work!
This discussion has been closed.