Setting ajax.data dynamically oustide instance

Setting ajax.data dynamically oustide instance

kabezakabeza Posts: 14Questions: 5Answers: 0

I have the following var which initializes a Datatable with these settings

var tablaDatos = $('#tablaDatos').DataTable({
    "language": {
        "url": "spanish.json"
    },
    "processing": true,
    "serverSide": true,
    "serverMethod": "post",
    "ajax": {
        "url": '/data_json',
        "data": {
            "filtro_tipo": "",
            "filtro_herramienta": "",
            "filtro_destino": "",
        }
    },
    "bFilter": true,
    "bAutoWidth": false,
    "ordering": true,
});

I have a select with some static options
When user chooses an option (select.change) I want to filter the table with the selected option and reload the ajax query, etc.

$('#myselect').on('change', function() {
    console.log("Filter: " + this.value);
    tablaDatos.ajax.data = { "filtro_tipo" : this.value };
    console.log(tablaDatos);
    tablaDatos.ajax.reload();
    // tablaDatos.draw();
});

Is this the correct way to set ajax.data ?
Console.log shows the correct value, but it is not sending the ajax.data in the $_POST array ...

What could be wrong? Thanks

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @kabeza,

    I'm not sure that you can set ajax.data like that, I'd be very surprised if that works. The best bet would be to have ajax.data as a function (see the examples on that page) and set filtro_tipo (and the others) based on a property or variable.

    Hope that does the trick,

    Cheers,

    Colin

  • kabezakabeza Posts: 14Questions: 5Answers: 0

    Hi @colin
    Yes, that did the trick. Thanks!

    "ajax": {
        "url": '/datos_json',
        data: function(data) {
            data.param_dep = $('#seldep').val();
        }
    },
    
This discussion has been closed.