JQuery DataTables plug-in method for obtaining data from the server side

Source: Internet
Author: User
Tags json

Sajaxsource parameter, the value is the URL. The table sends an AJAX request to fetch the data from the server side. The data returned on the server side should be a JSON string that can be converted to a JSON object. This string must be in strict accordance with JSON format requirements. Otherwise there will be an error. The data object's key for this object should be "Aadata", for example:

JS Code:

{

"Aadata":

{

"ColumnA": "Valuea",

"COLUMNB": "Valueb",

...

}

}

The Bserverside parameter, set to True, indicates that data is processed using the server side. When sorting, it is directly to the background to query the data, directly displayed, will not be in the front of the sorting operation.

The Fnserverdata parameter, which is used to customize the function, instead of the default function of the DataTables plug-in to query data from the server side. The default functions are as follows:

Original reference from Webmaster network: http://www.software8.co/wzjs/jquery/3314.html

JS Code:

/**

* @param {string} ssource HTTP source to obtain the "data from" (Sajaxsource)

* @param {array} aodata A key/value pair object containing the data to send

* to the server

* @param {function} Fncallback to is called on completion of the

* process that would draw the data on the page.

* @param {Object} osettings DataTables Settings object

*/

"Fnserverdata": Function (sURL, Aodata, Fncallback, osettings) {

OSETTINGS.JQXHR = $.ajax ({

"url": sURL,

"Data": Aodata,

' Success ': function (JSON) {

if (json.serror) {

Osettings.oapi._fnlog (osettings, 0, Json.serror);

}

$ (osettings.oinstance). Trigger (' xhr ', [Osettings, JSON]);

Fncallback (JSON);

},

"DataType": "JSON",

"Cache": false,

' Type ': Osettings.sservermethod,

"Error": Function (XHR, error, thrown) {

if (Error = = "ParserError") {

Osettings.oapi._fnlog (osettings, 0, "DataTables warning:json data from" + "server could is not parsed. This is caused by a JSON formatting error. ");

}

}

});

},

We can use this parameter to customize the AJAX request, or we can manipulate the acquired data. For example:

Server-side returns the data object of the table, without using "aadata" as the key of the data, we can add "aadata" key to the data in our defined callback function.

Fnserverparams parameter, used to send additional data to the server. For example:

JS Code:

$ (' #example '). DataTable ({

"Bprocessing": true,

"Bserverside": true,

"Sajaxsource": "scripts/server_processing.php",

' Fnserverparams ': function (aodata) {

Aodata.push ({"Name": "More_data", "Value": "My_value"});

}

});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.