What am I doing wrong

What am I doing wrong

nessinitsnessinits Posts: 86Questions: 27Answers: 0

I try to select the first row of the Datatable after init. It displays the right id with alert();, but doesn't select te first row as it suposed to do:

                $('#wl_persons')
                    .on( 'init.dt', function () {
                        var table = $('#wl_persons').DataTable();
                        alert(table.data()[0]['wl_persons']['persons_id']);
                        table.row(0).node().click();
                    } )
                    .dataTable();

This question has accepted answers - jump to:

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Node gives you a dom element, not a jqury element so click will not workj

    try $( table.row(0).node()).trigger("click");

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Additionally you need to modify your selector slightly: ':eq(0)' - i.e.

    $( table.row(':eq(0)').node()).trigger("click");
    

    That way the first row in the table will be selected regardless of sorting. Using 0 alone will select the first row that was read into the table.

    Allan

  • nessinitsnessinits Posts: 86Questions: 27Answers: 0

    It does something, but not completely what I expected.

    I doesn't automatically make the first row "selected". Even when I add an on click event on the <tr> it selects the row as it should following the css, but it doesn't fire the regular events. For example the status of the datatable doesn't display any selected rows (1 row selected).

    What am I doing wrong?

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    Don't do it with an event handler.
    From what I understand, you are trying to select the first row so do this http://jsbin.com/kowejop/edit?html,css,js,output

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin
    Answer ✓

    Agreed - row().select() is a better way to select the row.

    Allan

This discussion has been closed.