问题描述
任何人都可以告诉我如何使用 fnServerData
? $(document).ready(function(){
$('#example')。dataTable
bProcessing:true,
bServerSide:true,
sAjaxSource:xhr.php,
fnServerData:function(sSource,aoData,fnCallback, oSettings){
oSettings.jqXHR = $ .ajax({
dataType:'json',
type:POST,
url:sSource,
data:aoData,
success:fnCallback
});
}
});
});
下面是我的Ajax调用,我想替换 ajax调用
与fnServerData。
$ .ajax({
type:'GET',
url:url,
jsonp:true,
contentType:application / json,
dataType:'jsonp',
success:function(data){
$ .each(data.value, function(i,item){
table.fnAddData(item);
});
},
错误:function(e){
console.log e.message);
}
});
什么是sSource,fnCallback和oSettings?任何人都可以告诉我如何使用 fnServerData
?
sSource,fnCallback和oSettings由Datatables生成。
sSource是您的ajax调用的URL。初始化datatable时,可以在sAjaxSource中指定。所以你应该传递你的url var作为sAjaxSource。
oSettings是由数据集js创建和维护的。它存储有关您的数据表状态的重要信息。详细的文档可以在这里:
然而,我认为您的成功功能是不必要的。您应该在初始化期间指定aoColumns作为选项,然后datatables将为您填充数据。
$(document).ready(function(){
$('#example')。dataTable
aoColumns:[
{mData:engine},
{mData:browser},
{mData:platform.inner },
{mData:platform.details.0},
{mData:platform.details.1}
]
}),
bProcessing:true,
bServerSide:true,
sAjaxSource:url,
fnServerData:function(sSource,aoData,fnCallback,oSettings){
oSettings.jqXHR = $ .ajax({
dataType:'json',
type:POST,
url:sSource,
data:aoData,
success:fnCallback,
error:function(e){
console.log(e.message);
}
});
}
});
});
有关aoColumns的更多信息,请访问:
另外,在数据表页面上查看示例。应该有任何你需要的例子:
问候,
Saz
Can anyone show me how to use fnServerData
?.
$(document).ready( function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "xhr.php",
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
oSettings.jqXHR = $.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
}
} );
} );
Below is my Ajax call, i want to replace the ajax call
with the fnServerData.
$.ajax({
type: 'GET',
url: url,
jsonp: true,
contentType: "application/json",
dataType: 'jsonp',
success: function (data) {
$.each(data.value, function(i,item){
table.fnAddData(item);
});
},
error: function (e) {
console.log(e.message);
}
});
http://datatables.net/ref#fnServerData
What is sSource, fnCallback and oSettings?. Can anyone show me how to use fnServerData
?.
sSource, fnCallback and oSettings are generated by Datatables.
sSource is the url for your ajax call. When you initialize the datatable you specify this in sAjaxSource. So you should pass in your url var as sAjaxSource.
oSettings is created and maintained by datatables js. It stores important information about the state of your datatable. Detailed documentation available here:https://datatables.net/docs/DataTables/1.9.0/DataTable.models.oSettings.html
I think however, your success function is unnecessary. You should specify aoColumns as an option during initialization and then datatables will populate the data for you.
$(document).ready( function() {
$('#example').dataTable( {
"aoColumns": [
{ "mData": "engine" },
{ "mData": "browser" },
{ "mData": "platform.inner" },
{ "mData": "platform.details.0" },
{ "mData": "platform.details.1" }
]
}),
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": url,
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
oSettings.jqXHR = $.ajax( {
"dataType": 'json',
"type": "POST",
"url": sSource,
"data": aoData,
"success": fnCallback,
"error": function (e) {
console.log(e.message);
}
});
}
});
});
More information on aoColumns here: http://www.datatables.net/usage/columnsAlso, have a looka the examples on the datatables page. There should be an example for anything you need:http://www.datatables.net/usage/columns
Regards,Saz
这篇关于如何使用fnServerData?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!