Auto select rows

Auto select rows

osanmartinosanmartin Posts: 15Questions: 6Answers: 0

Hi, i've data stored on my db, i need to select some rows on datatable when data is loading. I tried do this in event:
$('#mesast').on( 'draw.dt', function () {
var table = $('#mesast').DataTable();

                table
                    .column( 4 )
                    .data()
                    .each( function ( value, index ) {
                        if(value.length > 0){
                            // Seleccionar fila
                            $( table.row( index ).nodes() ).addClass( 'selected' );
                        }
                    } );
            } );

And rows are correctly selected, but when i click some selected row, this row is not unselected until "second click" and when y save datatable in my db, some rows previously selected on load data, aren't saved...maybe that my auto-select is only "visual" effect, but datatable internally doesn't

Best regards,
Oscar

Answers

  • allanallan Posts: 61,903Questions: 1Answers: 10,148 Site admin

    The row().select() method can be used to select a row.

    Allan

  • osanmartinosanmartin Posts: 15Questions: 6Answers: 0

    Thanks Allan for your quickly answer; only for test i tried:

    $('#mesast').on( 'draw.dt', function () {
    var table = $('#mesast').DataTable();

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

    but console return: "Uncaught TypeError: table.row(...).select is not a function"

    Thanks,

  • osanmartinosanmartin Posts: 15Questions: 6Answers: 0

    I'm Sorry Allan, I just include css and js required files and select() method work, but:

    var oTT = TableTools.fnGetInstance( 'mesast' );
    var aSelectedTrs = oTT.fnGetSelectedData();

    aSelectedTrs not return rows previously selected when i reload data, only returns rows selected by "click" or "select buttons". I use fnGetSelectedData() in order to get udated rows selected.

    Thanks in advance.

  • allanallan Posts: 61,903Questions: 1Answers: 10,148 Site admin

    I would suggest not mixing TableTools and Select. They are two difference pieces of software. I would also not suggest using TableTools - it is legacy and no longer being updated.

    Select is the way to do it. If you want to get the selected rows, that is documented here.

    Allan

  • osanmartinosanmartin Posts: 15Questions: 6Answers: 0

    Important detail...... now i'm not using TableTools, and all work fine..!, thank you Allan!!

    OSM

This discussion has been closed.