P
P
Pan Propan2015-11-06 14:21:31
JavaScript
Pan Propan, 2015-11-06 14:21:31

How to get data from Datatables?

Hello, friends. Please help me with the problem, otherwise I'm already confused.
There is some data which I display in DataTables. 1fa13cdf7b4c417ab276a57df609f081.png

The library has good documentation , which describes how to select data.
I actually implemented it with such a script.

$(document).ready(function() {
    // Получаем выбранные строки
    var table = $('#table_id').DataTable();
    $.fn.dataTable.ext.buttons.reload = {
    text: 'Загрузить',
    action: function alertCountMass() {
    var mass = {};    
    var mass = table.rows( { selected: true } ).data();
    var str = JSON.stringify(mass);
    alert(str); //
}
    };

    // Подключаем кнопки

    
    new $.fn.dataTable.Buttons( table, {
    buttons: [
        { extend: 'copy', className: 'btn btn-info' },
        { extend: 'excel', className: 'btn btn-info' },
        { extend: 'pdf', className: 'btn btn-info' },
        { extend: 'reload', className: 'btn btn-success' }
    ]
    } );

    table.buttons().container()
        .appendTo( $('.col-sm-6:eq(0)', table.table().container() ) );
    // Подключаем кнопки    

    $('#table_id').on( 'click', 'tbody tr', function () {
    if ( table.row( this, { selected: true } ).any() ) {
        table.row( this ).deselect();
    }
    else {
        table.row( this ).select();
    }

    } );

} );


I know for sure that after I selected some rows, the array
var mass = table.rows( { selected: true } ).data(); contains data since the check mass.lenght(); returns the number of selected items.

Further, as you can see from the script, I try to convert the data to json, but nothing works for me.
var mass = {};    
    var mass = table.rows( { selected: true } ).data();
    var str = JSON.stringify(mass);
    alert(str);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
coderisimo, 2015-11-06
@mgis

what do you write in the console if you write like this:
var mass = table.rows( { selected: true } ).data();
console log(mass );
console.log(mass(0).data());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question