ajax.replacements

Since: Editor 2.0

Specify replacement parameters for the URL per submission.
Please note - this property requires the Editor extension for DataTables.

Description

You may need to dynamically modify the URL used for form submission based on the content of the data being edited. This is particularly common in REST APIs where you might need to modify the URL to contain certain keys.

This option provides the ability to list a set of keys and their replacement functions. The value returns for each key is the value that will be used in place of the key in the URL string.

Please note that in the URL the key should be denoted surrounded by braces - e.g.: {person}.

As of Editor 3, the replacement options of this parameter will also apply to the Ajax actions of upload and uploadMany field types, as well as the main ajax option.

Type

object

A list of keys which will be replaced in the URL whenever Editor sends a submission to the server. The value of each property in the object must be a function or false. The replacement function is run when the Ajax object is prepared for sending to the server and is passed in the following information:

  1. The replacement key name
  2. Ids being edited / deleted (comma separated string)
  3. Form action (create, edit or remove).
  4. Form data (see ajax.data). Note that the replacement functions happen before the ajax.data modifications.

Execution scope of the function is the Editor instance that is calling it.

The value returned from the function is the value that will be used in the URL, replacing the original key (and its surrounding braces).

As of Editor 3, if the value for a key is false no replacement is made. Note that id is a special case - normally Editor will replace _id_ and {id} in the URL with the id of the item being edited. This default behaviour is cancelled if there is a id option in ajax.replacements, which can be false to simply stop the replacement.

Examples

Static value replacement:

var editor = new DataTable.Editor({
	ajax: {
		url: '/api/{controller}/person',
		replacements: {
			controller: function (key, id, action, data) {
				return '12';
			}
		}
	},
	table: '#myTable',
	fields: [
		// ...
	]
});

Dynamic replacement based on form data:

var editor = new DataTable.Editor({
	ajax: {
		url: '/api/{controller}/person',
		replacements: {
			controller: function (key, id, action, data) {
				return this.field('controller').val();
			}
		}
	},
	table: '#myTable',
	fields: [
		// ...
	]
});

Cancel replacement of _id_ (Editor 3+):

var editor = new DataTable.Editor({
	ajax: {
		url: '/api/person_id_edit',
		replacements: {
			id: false
		}
	},
	table: '#myTable',
	fields: [
		// ...
	]
});

Related

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