Datatables serverside. Send extra parameters asynchronously

穿精又带淫゛_ 提交于 2020-01-05 08:05:36

问题


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>

回答1:


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();
    });


来源:https://stackoverflow.com/questions/26824732/datatables-serverside-send-extra-parameters-asynchronously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!