What does dependent callback do?
What does dependent callback do?
Dear Datatables,
I have been reading the documentation in https://editor.datatables.net/reference/api/dependent() and looking at many forum posts about dependent. However, I am still unclear on what the callback in this function does?
editor.dependent( 'position', function ( val, data, callback, e ) {
$.ajax( {
url: '/update/position',
dataType: 'json',
success: function ( json ) {
callback( json );
}
} );
} );
I saw in datatables.editor.js that it is this function, but I cannot understand it. Any help would be appreciated.
var update = function (json) {
if (opts.preUpdate) {
opts.preUpdate(json);
}
// Field specific
$.each({
labels: 'label',
options: 'update',
values: 'val',
messages: 'message',
errors: 'error'
}, function (jsonProp, fieldFn) {
if (json[jsonProp]) {
$.each(json[jsonProp], function (field, val) {
that.field(field)[fieldFn](val);
});
}
});
// Form level
$.each(['hide', 'show', 'enable', 'disable'], function (i, key) {
if (json[key]) {
that[key](json[key], json.animate);
}
});
if (opts.postUpdate) {
opts.postUpdate(json);
}
field.processing(false);
};
Thanks,
Philip
Answers
With
e-dependent()
, it allows you to perform actions when a field changes. The example on the site adds or removes values from the form, and this example counts how many characters have been entered into the name field.Hope that helps,
Colin
Thanks Colin!
After seeing the second example you sent me, I finally see what the "Return options / JSON" subheading in the dependent() API documentation means. We can return an object in the function, which object contains options, messages, etc. The result will be the editor box shows messages or changes the select options for the fields we specify.
Now I understand that the callback takes in an object such as this:
callback( {
"options": {
"position": [
"Director",
"Senior VP",
"VP"
]
},
"messages": {
"fieldName1": "message 1",
"fieldName2": "message 2"
}
} );
After which it will update the editor box using including the select options for the field "position".
Excellent, glad all sorted!
Colin