jQuery 如何动态设置dataTable的Ajax URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32049439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set dynamically the Ajax URL of a dataTable?
提问by pheromix
I'm using jQuery DataTables, my JavaScript code is shown below:
我正在使用 jQuery DataTables,我的 JavaScript 代码如下所示:
$(document).ready(function() {
var tbl = $('#table_tabl').DataTable({
responsive: true,
"oLanguage": {
"sUrl": "<?php echo RP_LANG ?>fr_FR.txt",
},
"processing": true,
"serverSide": true,
ajax: "<?php echo RP_SSP ?>server_processing_reservTables.php", // I want to add a parmeter to it dynamically when a select element is selected
"aoColumnDefs": [{
"aTargets": [3],
"mData": 3,
"mRender": function(data, type, full) {
return '<div style="text-align:center;"><a href="RestaurantReservation/reserverTable/' + data + '" title="Réserver"><span class="mif-lock icon"></span></a></div>';
}
}],
"aLengthMenu": [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, "Tout"]
]
});
});
I want to filter this dataTable according to the selected value of a select element :
我想根据选择元素的选定值过滤此数据表:
$("#select_id").on("change", function(){
// set the ajax option value of the dataTable here according to the select's value
});
How to set the ajax
option's value of the dataTable
in the on_change
event of the select
element based on the select's selected item ?
如何设置ajax
的选项的值dataTable
在on_change
该事件select
根据所选项目选择的元素?
采纳答案by pheromix
I found it :
我找到了 :
$("#salle_code").on("change", function(){
tbl.ajax.url("<?php echo RP_SSP ?>server_processing_reservTables.php?salle_code="+$(this).val()).load();
});
回答by Gyrocode.com
SOLUTION 1
解决方案 1
Use ajax.url()
API method to set the URL that DataTables uses to Ajax fetch data.
使用ajax.url()
API 方法设置 DataTables 用于 Ajax 获取数据的 URL。
$("#select_id").on("change", function(){
// set the ajax option value of the dataTable here according to the select's value
$('#table_tabl').DataTable()
.ajax.url(
"<?php echo RP_SSP ?>server_processing_reservTables.php?param="
+ encodeURIComponent(this.value)
)
.load();
});
SOLUTION 2
解决方案 2
Use ajax.data
option to add or modify data submitted to the server upon an Ajax request.
使用ajax.data
选项添加或修改根据 Ajax 请求提交给服务器的数据。
var tbl = $('#table_tabl').DataTable({
// ... skipped other parameters ...
ajax: {
url: "<?php echo RP_SSP ?>server_processing_reservTables.php",
data: function(d){
d.param = $('#select_id').val();
}
}
});
回答by Bugfixer
Datatable Version : 1.10.0-beta.1
Using fnDraw
to Redraw the table.
数据表版本:1.10.0-beta.1
使用fnDraw
重绘表。
Sample code for using fndraw
使用 fndraw 的示例代码
$(document).ready(function() {
var oTable = $('#example').dataTable();
// Re-draw the table - you wouldn't want to do it here, but it's an example :-)
oTable.fnDraw();
} );
$(document).ready(function() {
var tbl = $('#table_tabl').DataTable({
responsive: true,
"oLanguage": {
"sUrl": "<?php echo RP_LANG ?>fr_FR.txt",
},
"processing": true,
"serverSide": true,
"sAjaxSource": "<?php echo RP_SSP ?>server_processing_reservTables.php", // I want to add a parmeter to it dynamically when a select element is selected
"aoColumnDefs": [{
"aTargets": [3],
"mData": 3,
"mRender": function(data, type, full) {
return '<div style="text-align:center;"><a href="RestaurantReservation/reserverTable/' + data + '" title="Réserver"><span class="mif-lock icon"></span></a></div>';
}
}],
"aLengthMenu": [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, "Tout"]
]
});
$("#select_id").change(function () {
var end = this.value;
var NTypSource = '<?php echo RP_SSP ?>server_processing_reservTables?type='+end+'';
var oSettings = tbl.fnSettings();
oSettings.sAjaxSource = NTypSource;
tbl.fnDraw();
});
});