.ajax(),ajax

本文详细介绍了如何使用Ajax数据源在DataTables中动态加载表格内容。DataTables支持从远程文件、对象或自定义函数获取数据,并提供了丰富的选项进行定制,包括设置请求类型、处理发送到服务器的数据以及解析返回的数据。`ajax.dataSrc`参数可以改变默认的数据读取属性,而`ajax`作为函数使用时,允许完全控制Ajax请求,甚至可以使用其他数据获取方式,如Web存储或Firebase数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ajax

Since: DataTables 1.10

Load data for the table's content from an Ajax source.

Description

DataTables can obtain the data that it is to display in the table body from a number of sources, including from an Ajax data source, using this initialisation parameter. As with other dynamic data sources, arrays or objects can be used for the data source for each row, with columns.data employed to read from specific object properties.

The ajax property has three different modes of operation, depending on how it is defined. These are:

string - Set the URL from where the data should be loaded from.

object - Define properties for jQuery.ajax.

function - Custom data get function

Types

stringDescription:In its simplest form, ajax, when given as a string will simply load the data from the given remote file. Note that DataTables expects the table data to be an array of items in the data parameter of the object (use the ajax.dataSrc option of ajax as an object, if your data is formatted differently):

{

"data": [

// row 1 data source,

// row 2 data source,

// etc

]

}

DataTables can use objects or arrays in almost any format as the data source for each row through use of the columns.data option. The default is to use an array data source.

Simple example:

$('#example').dataTable( {

"ajax": "data.json"

} );

objectDescription:As an object, the ajax object is passed to jQuery.ajax allowing fine control of the Ajax request. DataTables has a number of default parameters which you can override using this option. Please refer to the jQuery documentation for a full description of the options available, although the following parameters provide additional options in DataTables or require special consideration:

data (ajax.data) - As with jQuery, data can be provided as an object, but as an extension, it can also be provided as a function to manipulate the data DataTables sends to the server. The function takes a single parameter, an object where the parameters are name / value pairs that DataTables has readied for sending. An object may be returned which will be use used as the data to send to the server (therefore, if you wish to use the DataTables set parameters, you must merge them in your function), or you can add the items to the object that was passed in and not return anything from the function. This supersedes fnServerParams from DataTables 1.9-.

dataSrc (ajax.dataSrc) - By default DataTables will look for the property data (or aaData for compatibility with DataTables 1.9-) when obtaining data from an Ajax source or for server-side processing - this parameter allows that property to be changed, through two different forms:

As a string - define the property from the object to read. Note that if your Ajax source simply returns an array of data to display, rather than an object, set this parameter to be an empty string. Additionally you can use Javascript dotted object notation to get a data source for multiple levels of object / array nesting.

As a function - As a function it takes a single parameter, the JSON returned from the server, which can be manipulated as required. The returned value from the function is what will be used by DataTables as the data source for the table.

This supersedes sAjaxDataProp from DataTables 1.9-.

success - Must not be overridden as it is used internally in DataTables. To manipulate / transform the data returned by the server use ajax.dataSrc (above), or use ajax as a function (below).

Simple example:

$('#example').dataTable( {

"ajax": {

"url": "data.json",

"type": "POST"

}

} );

function ajax( data, callback, settings )Description:As a function, making the Ajax call is left up to yourself allowing complete control of the Ajax request. Indeed, if desired, a method other than Ajax could be used to obtain the required data, such as Web storage or a Firebase database.

When the data has been obtained from the data source, the second parameter (callback here) should be called with a single parameter passed in - the data to use to draw the table.

Simple example:

$('#example').dataTable( {

"ajax": function (data, callback, settings) {

callback(

JSON.parse( localStorage.getItem('dataTablesData') )

);

}

} );

Note that as of DataTables 1.10.6 the xhr event will be triggered when a function is used for ajax (even if there is no Ajax request). Prior to 1.10.6 no event would have been triggered.Parameters:NameTypeOptional1dataNo

Data to send to the server

2callbackNo

Callback function that must be executed when the required data has been obtained. That data should be passed into the callback as the only parameter

DataTables settings object

Examples

Get JSON data from a file via Ajax.:$('#example').dataTable( {

"ajax": "data.json"

} );

Get JSON data from a file via Ajax, using dataSrc to change data to tableData (i.e. { tableData: [ ...data... ] }):$('#example').dataTable( {

"ajax": {

"url": "data.json",

"dataSrc": "tableData"

}

} );

Get JSON data from a file via Ajax, using dataSrc to read data from a plain array rather than an array in an object:$('#example').dataTable( {

"ajax": {

"url": "data.json",

"dataSrc": ""

}

} );

Send request as POST:$('#example').dataTable( {

"ajax": {

"url": "data.json",

"type": "POST"

}

} );

Add data to the request, returnng an object by extending the default data:$('#example').dataTable( {

"ajax": {

"url": "data.json",

"data": function ( d ) {

return $.extend( {}, d, {

"extra_search": $('#extra').val()

} );

}

}

} );

Add data to the request by manipulating the data object:$('#example').dataTable( {

"ajax": {

"url": "data.json",

"data": function ( d ) {

d.extra_search = $('#extra').val();

}

}

} );

Manipulate the data returned from the server - add a link to data (note this can, should, be done using render for the column - this is just a simple example of how the data can be manipulated).:$('#example').dataTable( {

"ajax": {

"url": "data.json",

"dataSrc": function ( json ) {

for ( var i=0, ien=json.length ; i

json[i][0] = '

callback(

JSON.parse( localStorage.getItem('dataTablesData') )

);

}

} );

Related

The following options are directly related and may also be useful in your application development.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值