Update ajax data programatically for jQuery datatables

javascript jquery datatable datatables

193 просмотра

1 ответ

I have a jQuery datatable with an ajax datasource defined like so:

var selected_ids = [];

selected = $('#selected_members').dataTable( {
    "ajax": {
        'type': 'GET',
        'url': "{!! route('admin.members.included_datatables') !!}",
        'data': {
           'member_ids' : JSON.stringify(selected_ids)
        },
    },
    "processing": true,
    "serverSide": true,
    "columns": [
        null,
        null,
        null,
        null,
        null,
        {"searchable": false, "orderable": false}
    ]
});

$('#available_members').on('click', '.select', function(e) {
    e.preventDefault();

    member_id = $(this).data('member-id');
    selected_ids.push(member_id);

    selected.api().ajax.reload();
});

As you can see I am updating the contents of selected_ids manually and I want to then refresh the datatable. This is working except that when the datatable reloads and does the ajax call the member_ids parameter that it passes to the server is still empty. Do I have to update the data property of the ajax call manually before reloading the table and if so how do I do that?

Автор: geoffs3310 Источник Размещён: 08.11.2019 11:23

Ответы (1)


0 плюса

I fixed it I used a closure for the data attribute instead like so:

selected = $('#selected_members').dataTable( {
    "ajax": {
        'type': 'GET',
        'url': "{!! route('admin.members.included_datatables') !!}",
        'data': function ( d ) {
            d.member_ids = selected_ids;
            return d;
        }
    },
    "processing": true,
    "serverSide": true,
    "columns": [
        null,
        null,
        null,
        null,
        null,
        {"searchable": false, "orderable": false}
    ]
});
Автор: geoffs3310 Размещён: 20.08.2016 02:03
Вопросы из категории :
32x32