Load ajax data with custom parameters

Load ajax data with custom parameters

viandanteviandante Posts: 22Questions: 10Answers: 1
edited September 2014 in Free community support

Hi to all!

In my datatable i pass few custom parameters (ex: values of input fields).
But if i change the value of the input fields, and reload the table, the new values are not taken...


<script type="text/javascript">
tblPreventivi = $('#dt_basic').DataTable({
    "processing": true,
    "serverSide": true,
    "order": [[0, "desc"]],
    "ajax": {
        url: "?modulo=Preventivo&action=getPreventiviNew&ajxRq=1",
        data: {
            "dtStart": $("#dtStartAlternate").val(),
            "dtEnd": $("#dtEndAlternate").val(),
            "provincia": $("#selProvincia").val(),
            "inviati": $("input[name=preventiviinviati]:checked").val()
        }
    },
    "columns": [
        {data: {
                _: "id.display",
                sort: "id.tosort"
            }},
        {data: {
                _: "datapreventivo.display",
                sort: "datapreventivo.timestamp"
            }},
        {data: {
                _: "datamodifica.display",
                sort: "datamodifica.timestamp"
            }},
        {data: "nome"},
        {data: "email"},
        {data: "telefono"},
        {data: "citta"},
        {data: "provincia"},
        {data: "opzioni"},
    ],
    "sDom": "<'dt-toolbar'<'col-xs-12 col-sm-3 col-sm-offset-9'f>>r" +
            "t" +
            "<'dt-toolbar-footer'<'col-sm-3 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-9'>p>",
    "autoWidth": true,
    "preDrawCallback": function() {
        // Initialize the responsive datatables helper once.
        if (!responsiveHelper_dt_basic) {
            responsiveHelper_dt_basic = new ResponsiveDatatablesHelper($('#dt_basic'), breakpointDefinition);
        }
    },
    "columnDefs": [
        {"orderable": false, "targets": 8},
        {"orderable": false, "targets": 1}
    ],
    "rowCallback": function(nRow) {
        responsiveHelper_dt_basic.createExpandIcon(nRow);
    },
    "drawCallback": function(oSettings) {
        responsiveHelper_dt_basic.respond();
    },
    "language": {
        "url": "accessories/DT-IT.txt"
    }
});
</script>

The initial value of the dtStart, dtEnd etc.. parameters is "", and it's ok, but when i change the value:


<script type="text/javascript">
$(document).on("change", ".datepicker", function(event) {   
    tblPreventivi.ajax.reload();
});
</script>

the new value are not taken...

How i can do this?

Datatables 1.10

Thanks in advance.

Massimo

This question has an accepted answers - jump to answer

Answers

  • sumit25sumit25 Posts: 2Questions: 1Answers: 0

    try this

      ajax: {
            url: "?modulo=Preventivo&action=getPreventiviNew&ajxRq=1",
           "data": function ( d ) {
               return $.extend( {}, d, {
                 "dtStart": $("#dtStartAlternate").val(),
                 "dtEnd": $("#dtEndAlternate").val(),
                 "provincia": $("#selProvincia").val(),
                 "inviati": $("input[name=preventiviinviati]:checked").val()
               } );
            }
        },
    

    http://datatables.net/reference/option/ajax.data

    i apologize for the formatting, but the link gives you a more readable example.

  • viandanteviandante Posts: 22Questions: 10Answers: 1
    edited September 2014

    I found the solution, a function for the data...

    
    ...
    ajax: {
    url: "...",
    data: function(data) {
         data.dtStart = $("#dtStartAlternate").val();
         data.dtEnd = $("#dtEndAlternate").val();
         data.provincia = $("select#selProvincia option:selected").val();
         data.tipo = $("select#selSettore option:selected").val();
         data.inviati = $("input[name=preventiviinviati]:checked").val();
      }
    }
    
    

    But this solution don't work with the pipeline example...

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Answer ✓

    Not sure why @sumit25's answer was rejected - it is exactly what I would suggest myself and is correct.

    Regarding pipelining - if you want to use it with pipelining you would need to modify the pipelining code a little to add your data to each request.

    Allan

  • viandanteviandante Posts: 22Questions: 10Answers: 1

    Sorry, i read a wrong answer from sumit25... it's the right answer...

This discussion has been closed.