val()

Since: Editor 1.3

Get or set the value for one or more fields.
Please note - this property requires the Editor extension for DataTables.

Description

It is often useful to be able to get and set the value of a field, or multiple fields, for example in client-side validation, or data manipulation. This method provides that ability for one or more fields in a form.

When used with multi-row editing, the values returned and set for fields have the following behaviour for this method:

  • As a getter, for each field:
    • If all items being edited have the same value for the field, that value is returned
    • If the items being edited have different values for the field, undefined is returned. multiGet() or field().multiGet() should be used to access individual values if this is the case.
  • As a setter, for each field:
    • The field value is set to the same value for all items being edited. Use multiSet() or field().multiSet() to set individual values if required.

In addition to this method, Editor provides a number of other methods that can be used to get and set field values - each with their own speciality. Please refer to the Related selection below for links to these methods.

Types

val( [ fieldNames ] )

Get the value for the given field(s)

Parameters:

Returns:

Any or object:
The value of the field if a string was used as the fieldName parameter, or an object, with the keys set to the field names and the value to the corresponding field's value, if fieldName was given as an array.

val( fieldNames [, value ] )

Set the value for one or more fields.

Parameters:

Returns:

DataTable.Editor: Editor instance

Examples

Client-side validation using preSubmit:

editor.on('preSubmit', function () {
	if (this.val('first_name') === '') {
		this.error('first_name', 'This field is required');
		return false;
	}

	return true;
});

Check a value from a row being edited and submit immediately if certain data is found.:

$('#myTable tbody').on('click', 'tr', function () {
	editor.edit(this, false);

	if (editor.val('access') === 'all') {
		editor.set('admin', 1).submit();
	}
});

Set the values of a few fields before then automatically submitting the form:

editor
	.create()
	.val('name', 'Test user')
	.val('access', 'Read only')
	.submit();

As above, but setting multiple fields at the same time using an object:

editor
	.create()
	.val({
		name: 'Test user',
		access: 'Read only'
	})
	.submit();

Related

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