2

i writing simple contexMenu for datatable.i'm using datatables class for create list of data. i want to find first cell of table on right click on each that. HTML: jquery:

    $("#showTopics tbody").bind("contextmenu",function(event) {
        var aata = $(this).children('tr').children('td').eq(0).text();
        alert(aata);
    return false;
});

HTML

<table id='showTopics' style='line-height:18px;'>
    <thead>
        <tr>
            <th style='width:30%;text-align:right;'>X"</th>
            <th style='width:7%;'>a</th>
            <th style='width:12%;'>b</th>
            <th style='width:11%;'>c</th>
            <th style='width:9%;'>d</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

generated tbody with datatables:

below result after generated with datatables and putting between of tbody.

row_selected seting with jquery after using contex menu and clicking on tr.

how can i find that? for example after clicking on first row i must be have 7 and after clicking on second of tr 8.

sorry for my english

<tr class="odd row_selected">
  <td class=" sorting_1">7</td>
  <td class="">0000-00-00</td>
  <td class="">0</td>
  <td class="">a</td>
  <td class="">aa</td>
</tr>
<tr class="even">
  <td class=" sorting_1">8</td>
  <td class="">0000-00-00</td>
  <td class="">0</td>
  <td class="">b</td>
  <td class="">bb</td>
</tr>
2
  • Do you want to show a <td>-cell's context, when you click with right mouse button on it? Commented May 16, 2013 at 8:52
  • 1
    @Stano, yes, i want to get that(first cell of clicked tr). Commented May 16, 2013 at 8:54

2 Answers 2

0

You should change your code to the following. Instead of binding context menu on tbody, bind it to each tr

$("#showTopics tbody tr").bind("contextmenu", function (event) {
    var aata = $(this).find("td:eq(0)").text();
    alert(aata);
    return false;
});
Sign up to request clarification or add additional context in comments.

Comments

0

Update: the problem can be solved by attaching the handler with 'on' method as shown in this SO answer or in this example.

$(document).ready(function () {
    $('#showTopics').dataTable();
    $('#showTopics tbody').on('contextmenu', 'tr', function() {
        var firstcell = $(this).children('td:first');
        alert(firstcell.text());
        return false;
    });
});

fiddle

1 Comment

tbody is generated with datatables and your code does not work for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.