亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

數據表沒有被過濾/重新加載

數據表沒有被過濾/重新加載

溫溫醬 2023-08-18 17:16:03
我正在使用 JQuery Datatable 插件處理服務器端,當下拉列表更改時它不會刷新。我將通過數據表向我的 PHP 類發送一個值,該類從后端獲取記錄。那么場景是每當下拉列表更改時,表將根據所選值進行排序。我現在嘗試做的方式甚至在下拉列表更改時不會觸發。<script type="text/javascript">    //Initially gets the selected value of dropdown    var status= $("#orderStatus option:selected").text();    //DataTable Initialization    $(document).ready(function() {        var tableone = $('#example').DataTable( {            "processing":   true,            "serverSide":   true,            "paging"    :   true,            "searching" :   true,            "sDom": 'rtip',            "iDisplayLength"    :   100,            "processData": false,            "ajax": {                url  :"fetch.php",                type : "POST",                data : {                    status: status                }            }        } );        //Search field        $('#search').keyup(function(){            tableone.search($(this).val()).column(0).draw() ;        });    } );    //Whenever the value is changed, the table has to be sorted acc to its value.    $(document).on('change','#orderStatus',function(event){        var drpStats= $(this).val();        $.ajax({            url  :"filter.php",            type : "POST",            data : {                status : drpStats            },            success: function (data) {                $('#example').DataTable().ajax.reload();            }        });    });</script>我正在嘗試另一種方法,即下拉菜單在更改時被觸發,但不獲取值。那個也附上了。這樣我就可以獲得下拉列表的變化值,但數據表一旦更改就不會被過濾。可能是什么原因?在控制臺中,它返回 fetch.php 和 filter.php 的 JSONS。解決方案可能很簡單。但我不知道我在哪里犯了錯誤。如果需要更多信息,請發表評論?
查看完整描述

1 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

經過幾個小時的努力,我發現當初始化數據表時,應該直接從下拉列表中獲取傳遞的數據,而不是將變化的值分配給變量并傳遞該數據。


<script type="text/javascript">


    //Initially gets the selected value of dropdown

    var status= $("#orderStatus option:selected").text(); //This is unnecessary.


    //DataTable Initialization

    $(document).ready(function() {

        var tableone = $('#example').DataTable( {

            "processing":   true,

            "serverSide":   true,

            "paging"    :   true,

            "searching" :   true,

            "sDom": 'rtip',

            "iDisplayLength"    :   100,

            "processData": false,

            "ajax": {

                url  :"fetch.php",

                type : "POST",

                //It's changed here

                data : function(data){

                    var status = $('#orderStatus').val();

                    data.orderStatus = status;

                }

            }

        } );


        //Search field

        $('#search').keyup(function(){

            tableone.search($(this).val()).column(0).draw() ;

        });


       //Then Redraw the datatable when the dropdown is selected

            $('#orderStatus').change(function(){

            tableone.draw();

        });

    });

并在服務器端接收發布的下拉列表


$orderStatus = $_POST['orderStatus'];

參考:https ://makitweb.com/how-to-add-custom-filter-in-datatable-ajax-and-php/?unapproved=13009&moderation-hash=c02720a3cdf60b2886fed5a45824b850#comment-13009


查看完整回答
反對 回復 2023-08-18
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號