Datatables serverside. Send extra parameters asynchronously
Asked Answered
O

1

1

I'm using Datatables with server-side processing. I'm able to send extra parameters to the server but they are sent only when the table is loaded for the first time or when the table is reloaded due to filtering, ordering, etc. I want the extra parameters to be sent to the server every time I select a value from the select field. How can I achieve this behavior?. Thanks in advance.

This is my datatables script

<script>
$(document).ready(function() {
    $('#tabla').dataTable( {
        "sDom": '<"top"l>rt<"bottom"pi><"clear">',
        "processing": true,
        "serverSide": true,
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "sAjaxSource": "server_side3.php?action=table_data",
        "bDeferRender": true,
        "aLengthMenu": [10, 25, 40],
        "contentType": "application/json; charset=utf-8",
        "dataType": "json",

        "fnServerParams": function ( aoData ) {
            aoData.push( { "name": "year", "value": $( "#year option:selected" ).text() } );
        },
        language: {
        url: '//cdn.datatables.net/plug-ins/380cb78f450/i18n/Spanish.json'
    }

    } ).columnFilter();

} );
</script>

I also tried with this piece of code:

  "fnServerData": function ( sSource, aoData, fnCallback ) {
            /* Add some extra data to the sender */
            aoData.push( { "name": "year", "value": $( "#year option:selected" ).text() } );
            $.getJSON( sSource, aoData, function (json) {
            fnCallback(json)
            } );

And my html

<select id="year"> 
        <?php
        echo '<option value="'.date(Y).' selected">'.date(Y).'</option>';
        for ($i=2005; $i < date(Y) ; $i++) { 
            echo '<option value="'.$i.'">'.$i.'</option>';
        }

        ?>
</select>
Orgel answered 9/11, 2014 at 3:39 Comment(1)
I think you want to use fnServerData. Look at my answer here: #21704898Dyanna
K
4

You're almost there - you just need to add a call to fnDraw() when a value in the selectlist is selected:

$('#year').change(function (e) {
        $('table#tabla').dataTable().fnDraw();
    });
Kerril answered 10/11, 2014 at 10:11 Comment(1)
May I say a few things... Thank you! You are COOL!, You saved my day and I hope grate things come to you. THANK YOU AGAIN.Orgel

© 2022 - 2024 — McMap. All rights reserved.