Option 1- 使用 preXhr.dt 事件。
table = $('#example')
.on('preXhr.dt', function ( e, settings, data ) {
data.whateveryouwant = $("#someidhere").val()
data.anotherexample = "kittens"
} )
// then just setup your datatable as normal
.DataTable({
ajax:{
url: "/url/getTableData",
type: "GET" // This is the default value, could also be POST
},
sortClasses: false,
paging: false,
scrollY: 300,
columns: cols
});
看这里http://datatables.net/reference/event/
选项 2(首选)- 使用 ajax.data 函数。
table = $('#example').DataTable({
ajax:{
url: "/url/getTableData", // Change this URL to where your json data comes from
type: "GET", // This is the default value, could also be POST, or anything you want.
data: function(d) {
d.whateveryouwant = $("#someidhere").val()
d.anotherexample = "kittens"
}
},
sortClasses: false,
paging: false,
scrollY: 300,
columns: cols
});
两个选项都会产生相同的结果。您的服务器不会知道其中的区别。额外的数据将添加到每个table.ajax.reload()
。额外的数据将是:
whateveryouwant
的 与 的值#someidhere
元素,以及
anotherexample
与价值"kittens"
我更喜欢Option 2,因为更明显的是,每个请求都会添加额外的数据。我认为第一个选项有点偷偷摸摸,对于阅读您的代码的其他人来说并不那么明显。