val()
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,
undefinedis returned.multiGet()orfield().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()orfield().multiSet()to set individual values if required.
- The field value is set to the same value for all items being edited. Use
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:
| Name | Type | Optional | |
|---|---|---|---|
| 1 | fieldName | Yes | |
The field name(s) to obtain the values of. Multiple fields can be used by passing the value in as an array of field names. | |||
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:
| Name | Type | Optional | |
|---|---|---|---|
| 1 | fieldNames | No | |
The field name to set the value of. If given as an object each property name is the name of the field to set and the property value, the value to set for the field. | |||
| 2 | value |
| Yes |
The value to set the field to. The format of the value will depend upon the field type. Not required if the first parameter is given as an object. | |||
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.