ajax

Since: Editor 1.0

Ajax configuration for form data submission.
Please note - this property requires the Editor extension for DataTables.

Description

Editor will read the data that it is to edit directly from a DataTable, or HTML elements if used in standalone mode, but that edited data needs to be sent somewhere so it can be saved for later retrieval. This option defines the actions Editor will take for submitting the data to be stored when submit() is called.

ajax can take a number of forms, documented below, to make it easy to configure where and how Editor will send the data for your specific use cases. It can even be given as a function to allow complete flexibility. It is very much based on the DataTables ajax option and the two are very similar in operation.

URL parameters

The URL given as the Ajax submit address using this option (either as a string, or in the url option of an object) can contain the following options which will be processed and automatically replaced by Editor:

  • {id} - Will be replaced by the row id that is being edited or removed (as defined by idSrc). If multiple rows are being operated on, the ids will be joined as a comma separated list. This can be particularly useful for REST services - for example /staff/{id} might be submitted as /staff/4 for staff entity 4 on edit / remove. Note: the {id} is a literal string and will be automatically replaced by Editor. See the examples below to see how it might be used. Note: Editor v1 used _id_ for the replacement of the row id - this will still work in Editor v2, but the new form of {id} is recommended.

ajax.replacements can be used to define your own replacements in the URL when data is submitted.

Server response

The data submitted by Editor and that expected in return is documented in the manual. The Ajax options discussed on this page are basically a transport layer to facilitate that data interchange.

The request to the server will be considered to be successful as long as valid JSON is returned (regardless of status code), while any response with invalid JSON will fall into Editor's error handler. This is particularly useful when interfacing Editor with an REST service where response codes can be used to convey useful information - for example a submission which contains invalid data could have a 400 response code and valid JSON noting which field was in error, and what the error was. Editor will correctly handle such a case.

Backwards compatibility notes:

  • The functionality of this option in Editor 1.3 replaces the deprecated ajaxUrl. More specifically, with the addition of the ability of this option to be given as a string in 1.3, ajaxUrl is redundant.
  • URLs given in this option (either as a string, or in the url option of an object) can be prefixed with an HTTP method followed by a space and then the URL (for example PUT /data). This is provided for full compatibility with the old ajaxUrl option, but it is recommended in 1.3+ to use an object structure with the type option set (see examples below).

Types

string

Since: 1.3

In its simplest form, the ajax option can be given as a simple string, pointing to the URL where Editor should submit data from the form when the submit() method is called.

The Ajax request that is performed is performed through jQuery's $.ajax method, with Editor's default Ajax options which are:

  • type: POST
  • dataType: json

To send data with different Ajax options, use this option as an object: see below.

Simple example:

new $.fn.dataTable.Editor( {
    ajax:   "../php/staff.php",
    ...
} );

object

As an object, the ajax object is passed to $.ajax allowing fine control of the Ajax request and the ability to override the default Editor Ajax options. Please refer to the jQuery documentation for a full description of the options available.

Like in DataTables, the ajax.data option available extends jQuery's default data option by providing the ability to supply it as a function. This can allow the data that is submitted to the server to be manipulated quite easily. Please see the ajax.data documentation for further details.

Prior to 1.6 the success and error options should not be overridden as these are used by Editor. Editor 1.6 will now recognise your methods and execute them after its own functions.

Simple example:

new $.fn.dataTable.Editor( {
    ajax: {
        url: "../php/staff.php",
        type: "PUT"
    },
    ...
} );

object

Since: 1.3

ajax can be given in two different forms as an object. The first, documented above provides the ability to give specific jQuery Ajax options, but it is also possible to give different options for each of the CRUD methods that Editor performs. This can be useful, for example, if you need to submit data using a different HTTP method depending upon the request type, as is common in a REST interface.

When used in this form, the object must contain the parameters:

  • create - Create a record
  • edit - Edit one or more records
  • remove - Delete one or more records
  • upload - File upload (for use with upload). Requires 1.6.3 or newer.

Each of these options can be given individually as a string, an object of Ajax configuration options as described above, or as a function for a custom Ajax call, as described below.

Simple example:

new $.fn.dataTable.Editor( {
    ajax: {
        create: '/api/staff', // default method is POST
        edit: {
            type: 'PUT',
            url:  '/api/staff'
        },
        remove: {
            type: 'DELETE',
            url:  '/api/staff'
        }
    },
    ...
} );

ajax( method, url, data, success, error )

The final form in which e-ajax can be given is as a function. This provides complete control over how Editor will send the submitted data and where it sends it to. This can be a custom $.ajax call, you could use localStorage, a Firebase connection or anything else.

As of Editor 2.1 this function is executed in the scope of the host Editor instance (i.e use this to access the Editor instance).

Parameters:

Examples

As a string - all actions are submitted to this URI as POST requests:

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

As an object - using GET rather than POST:

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

As an object - each action is submitted to a different URI as POST requests:

var editor = new DataTable.Editor({
	ajax: {
		create: '/api/user/create',
		edit: '/api/user/_id_/edit',
		remove: '/api/user/_id_/delete'
	},
	table: '#myTable'
});

As an object - with different HTTP methods for each action:

var editor = new DataTable.Editor({
	ajax: {
		create: {
			type: 'POST',
			url: '/rest/user/create'
		},
		edit: {
			type: 'PUT',
			url: '/rest/user/edit/_id_'
		},
		remove: {
			type: 'DELETE',
			url: '/rest/user/delete'
		}
	},
	table: '#myTable'
});

As a function - Making a custom $.ajax call:

var editor = new DataTable.Editor({
	table: '#myTable',
	ajax: function (method, url, data, success, error) {
		$.ajax({
			type: method,
			url: url,
			data: data,
			dataType: 'json',
			success: function (json) {
				success(json);
			},
			error: function (xhr, error, thrown) {
				error(xhr, error, thrown);
			}
		});
	}
});

Related

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