1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
<?xml version="1.0" encoding="UTF-8" ?>
<dt-option group="data">
<name>ajax.data</name>
<summary>Add or modify data submitted to the server upon an Ajax request</summary>
<since>1.10</since>
<type type="object">
<description>
As an object, the `dt-init ajax.data` option is used to extend the data object that DataTables constructs internally to submit to the server. This provides an easy method of adding additional, static, parameters to the data to be sent to the server. For dynamically calculated values, use `dt-init ajax.data` as a function (see below).
</description>
</type>
<type type="function">
<signature>ajax.data( data, settings )</signature>
<parameter type="object" name="data">
Data that DataTables has constructed for the request. This will include server-side processing parameters if you are using the `dt-init serverSide` option.
</parameter>
<parameter type="object" name="settings" since="1.10.6">
DataTables settings object (`dt-type DataTables.Settings`).
</parameter>
<returns type="object|string|undefined">
If there is no return value from the function (i.e. `dt-type undefined`) then the original data object passed into the function by DataTables will be used for the request (the function may have manipulated its values).
If an object is returned, then that object will be used as the data for the request. It will not be merged with the original data object constructed by DataTables before being sent.
If a string is returned, this string it will be used in the Ajax request body rather than individual HTTP parameters being sent. This is particularly useful for sending JSON encoded data in the request body so the server can decode it directly, rather than individual HTTP parameters being sent. See example below for how to use `JSON.stringify()` to achieve this.
</returns>
<description>
As a function, the `dt-init ajax.data` option can be used to modify the data DataTables submits to the server upon an Ajax request, by manipulating the original data object DataTables constructs internally, or by replacing it completely.
This provides the ability to submit additional information to the server upon an Ajax request, with the function being executed upon each Ajax request, allowing values to be dynamically calculated. For example, a value could be read from a text input field to act as an additional search option.
</description>
</type>
<description>
When making an Ajax request to the server, DataTables will construct a data object internally, with the data it requires to be sent to the server for the request. What this data contains will depend upon the processing mode DataTables is operating in:
* For client-side processing no additional data is submitted to the server
* For server-side processing (`dt-init serverSide`) the draw request parameters are submitted - see the [server-side processing manual](//datatables.net/manual/server-side).
The `dt-init ajax.data` option provides the ability to add additional data to the request, or to modify the data object being submitted if required.
In principle it operates in exactly the same way as jQuery's `$.ajax.data` property, in that it can be given as an object with parameters and values to submit, but DataTables extends this by also providing it with the ability to be a function, to allow the data to be re-evaluated upon each Ajax request (see above).
</description>
<example title="Add an extra parameter (`user_id` in this case), of a static value to the data submitted"><![CDATA[
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"data": {
"user_id": 451
}
}
} );
]]></example>
<example title="Add data to the request by manipulating the data object (no return from the function)"><![CDATA[
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"data": function ( d ) {
d.extra_search = $('#extra').val();
}
}
} );
]]></example>
<example title="Add data to the request (returning an object)"><![CDATA[
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"data": function ( d ) {
return $.extend( {}, d, {
"extra_search": $('#extra').val()
} );
}
}
} );
]]></example>
<example title="Submit data as JSON in the request body"><![CDATA[
$('#example').dataTable( {
"ajax": {
"url": "data.json",
"contentType": "application/json",
"type": "POST",
"data": function ( d ) {
return JSON.stringify( d );
}
}
} );
]]></example>
<related type="option">ajax</related>
<related type="api">ajax.json()</related>
<related type="api">ajax.reload()</related>
<related type="api">ajax.url()</related>
</dt-option>
|