"Dependent" not working with a field that is an array
"Dependent" not working with a field that is an array
I noticed that "dependent" wasn't triggered by a change in a field that contains an array of ids (department ids that the user selects in a selectize field).
I have a use case that documents may not be uploaded if the user selects a department that doesn't allow this. Hence I wanted to do an ajax call using "dependent" for this. This didn't work:
editor
.dependent('ctr_govdept[].id', function (val, data, callback) {
... do something
})
So I ended up rolling my own "poor man's" version of "dependent" and "undependent" like this:
editor
.on('open', function (e, mode, action) {
var self = this;
ctrGovdeptIds = '';
$( self.field('ctr_govdept[].id').node() ).change( function() {
if ( self.val("ctr_govdept[].id").toString() !== ctrGovdeptIds ) {
ctrGovdeptIds = self.val("ctr_govdept[].id").toString();
$.ajax({
type: "POST",
url: 'actions.php?action=checkDocUploadPermission',
data: {
ctrGovdeptIds: ctrGovdeptIds //comma separated string
},
success: function (blockUpload) {
if ( blockUpload <= 0 ) {
$( self.field('file[].id').node() ).removeClass("hidden");
} else {
self.set( {'file[].id': []} );
$( self.field('file[].id').node() ).addClass("hidden");
}
}
});
}
});
$( self.field('ctr_govdept[].id').node() ).change();
})
.on('close', function() {
$( this.field('ctr_govdept[].id').node() ).off( 'change' );
})
I also noticed that this didn't work either:
self.set( {'file[].id': []} )
.hide(['file[].id']);
I had to replace it with the code above.
Any ideas why "dependent" doesn't work with fields containing an array? Or is that only me having the issue?
This question has an accepted answers - jump to answer
Answers
dependent()
listens for thechange
event from whatever elementfield().input()
replies with. So if the field doesn't triggerchange
, or there are no elements that it can listen on, then unfortunately it just won't work.You can use the
event
option of the third argument passed todependent()
to change the event being listened for if you want. However, in this case I wonder if it is because Selectize doesn't trigger change on thefield().input()
return.Interesting that the chain of
set('...', []).hide()
doesn't work. Looking at the code, I can't immediately see why that would be. In the short term, you could reverse the calls so you callhide
beforeset
in the chain perhaps?Allan
I don't think you are right regarding selectize, but I probably caused it to break myself ... see below.
I am glad I found the work around relatively quickly. I will just leave it as it is. I checked all of my "dependents" - and they were never with an array of values only with fields that contain individual values. Hence I suspected the array is the problem.
This here works like a charm - and it is also a selectize field (but may contain only one value):
So this is the selectize field that works with dependent:
And this is the one that doesn't work with dependent:
I suspect that the following might be the cause of it?! On "initCreate" and "initEdit" I remove and add the field dynamically. Maybe that causes "dependent" not to work properly?! What do you think, Allan?
The code below is needed to dynamically change the field options. Unfortunately Editor only allows to retrieve one set of field options for all records, it does not allow to retrieve individual field options for each record read from the database. Then there is another problem: Using a "select" field you can dynamically change the field options, but not with "selectize": The only way to achieve this is to delete the field and add it with the new options retrieved from the server using a proprietary ajax call.
Here is the code for this:
Calling "hide" before "set" doesn't make a difference. I suspect the problem arises because the file upload consists of multiple elements?! Otherwise I never had any problems with "set" and "hide".
Yup - that would do it.
dependent()
binds its event listener to whateverfield().input()
returns, as I noted above, so if you were to delete the field (and thus also the node) then the event listener is gone. If you add a new field, then it wouldn't automatically be assigned any existing event listeners - you would need to rundependent()
again.Allan
Thanks for clarifying, Allan!