ajax.data

Since: Editor 1.3

Add or modify data submitted to the server upon an Ajax request.
Please note - this property requires the Editor extension for DataTables.

Description

When submitting data to the server (submit()), Editor will construct a data object internally, with the data required to be sent to the server for the request. This data is in the structure described in the Editor client / server communication documentation.

This method provides the ability to modify that data, including adding additional data 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 Editor extends this by also providing it with the ability to be a function, to allow the data to be re-evaluated and modified upon each Ajax request (see above).

Types

object

As an object, the ajax.data option is used to extend the data object that Editor 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 ajax.data as a function (see below).

ajax.data( data )

As a function, the ajax.data option can be used to modify the data Editor submits to the server upon an Ajax request, by manipulating the original data object Editor 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.

Parameters:

Returns:

object or string or undefined:
If there is no return value from the function (i.e. undefined) then the original data object passed into the function by Editor 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 Editor.

        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.

Examples

Add an extra parameter (user_id in this case), of a static value to the data submitted:

var editor = new DataTable.Editor({
	ajax: {
		url: 'php/staff.php',
		data: {
			user_id: 451
		}
	},
	table: '#myTable'
});

Add data to the request by manipulating the data object (no return from the function):

var editor = new DataTable.Editor({
	ajax: {
		url: 'php/staff.php',
		data: function (d) {
			d.user_id = $('#user_id').val();
		}
	},
	table: '#myTable'
});

Add data to the request (returning an object):

var editor = new DataTable.Editor({
	ajax: {
		url: 'php/staff.php',
		data: function (d) {
			return $.extend({}, d, {
				user_id: $('#user_id').val()
			});
		}
	},
	table: '#myTable'
});

Submit data as JSON in the request body:

var editor = new DataTable.Editor({
	ajax: {
		url: 'php/staff.php',
		contentType: 'application/json',
		data: function (d) {
			return JSON.stringify(d);
		}
	},
	table: '#myTable'
});

Related

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