field().def()

Since: Editor 1.3

Get / set the default value of the field.
Please note - this property requires the Editor extension for DataTables.

Description

The default value of a field is used by the create() command for each field in the form, and when editing a field (edit(), bubble() and inline() if the field's value at the time of editing is undefined). The form fields are automatically filled in with the default values in these circumstances.

The default value can be set when it is initially added to the form using the fields.def option or the def option of the field-options object given to add() when dynamically creating a field. This API method extends that ability by providing a method to set the default value at any point after a field has been added to the form.

Default values can be given as any Javascript data type (numbers, strings, objects, etc), including functions. If given in this form, the function will be evaluated when the default value is requested, and the value returned used. This can be useful, for example, to set the current date as the default in a date input field, since the function is evaluated when requested.

Types

field(…).def()

Get the defined default value for this field.

Returns:

Any: The default value

field().def( set )

Get the defined default value for this field.

Parameters:

Returns:

DataTable.Editor.Field: Field instance

Examples

Get the default value of a field:

var def = editor.field('first_name').def();
alert('first_name default is: ' + def);

Set the default value of a field:

editor.field('first_name').def('Allan');

Set the default as a function which will return the current date in YYYY-MM-DD format:

editor.field('update_time').def(function () {
	var d = new Date();
	var pad = function (i) {
		return i < 9 ? '0' + i : i;
	};

	return (
		d.getFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate())
	);
});