Select a row from JS

Select a row from JS

ostmalostmal Posts: 102Questions: 33Answers: 0

I would like to select table row from JS. I do it like here:
https://www.datatables.net/reference/api/row().select()

var table = $('#myTable').DataTable();
table.row(':eq(0)', { page: 'current' }).select();

But I can't do anything. The row in the table is not selected!

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    Did you add the select library?

    Your example code works here:
    http://live.datatables.net/mixisovi/1/edit

    Kevin

  • ostmalostmal Posts: 102Questions: 33Answers: 0
    edited August 2021

    I did everything as it should, as in the example that you provided. It doesn't work (
    My test example:
    http://www.a0250268.xsph.ru/index.php/edit-err

    (function($){
    $(document).ready(function() {
        var Editor = new $.fn.dataTable.Editor( {
            ajax: '/abc_crm/dt/my/edit_err/edit_err.php',
            table: '#edit_err',
            fields: [
                {
                    label: "note:",
                    name: "note",
                    type:  "textarea"
                }
            ],
        } );
    
        var table = $('#edit_err').DataTable( {
            dom: 'rtBp', // Убрал "f" - фильтрациюь
            ajax: {
                url: '/abc_crm/dt/my/edit_err/edit_err.php',
                data: { okr : "58" }
            },
            columns: [
                { data: 'ul' },
                { data: 'note' }
            ],
            buttons: [
                { extend: 'create', editor: Editor },
                { extend: 'edit',   editor: Editor},
                { extend: 'remove', editor: Editor }
            ]
    
        } );
    
        table.row(':eq(0)', { page: 'current' }).select();
    
    } );
    }(jQuery));
    
  • allanallan Posts: 61,443Questions: 1Answers: 10,053 Site admin
    Answer ✓

    Since you are Ajax loading the data you need to wait for the data to actually be loaded. At the moment, there is no data in the table when your row().select() call runs.

    You can use initComplete to address that - e.g. in this case:

    initComplete: function () {
      table.row(':eq(0)', { page: 'current' }).select();
    }
    

    Allan

  • ostmalostmal Posts: 102Questions: 33Answers: 0

    Allan, everything works fine! Thank you very much!

Sign In or Register to comment.