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
添加回答
舉報