select2 dependencies...
select2 dependencies...
I trying to get my head around the dependent method and setting a value for a select2-field. I have a field defined like this:
{
"label": "TPM",
"name": "TPM",
"type": "select2",
opts: {
//def: 'BR',
ajax: {
dataType: "json",
url: '/Registraties/getMethodes',
data: function (params) {
var query = {
tyb: editor.val('TYB'),
}
return query;
},
processResults: function (data) {
return {
results: data
};
}
}
}
}
This gets its data using an ajax call and passes the contents of the TYB-field to the request. Works fine.
Then I have a dependency defined like this:
editor.dependent('TYB', function (val, data, callback) {
$.ajax({
url: '/Registraties/TypeBehandelingUpdated',
type: 'post',
dataType: 'json',
data: {
"TYB": val,
"PRODUCTID": data.values.PRODUCTID,
"TPS": data.values.TPS,
},
success: function (json) {
callback(json); // callback naar de editor zodat die de velden kan bijwerken
}
});
});
So, when the TYB field is changed, I want to make some changes to the form. I do an ajax call, and on the serverside I decide which fields I need to enable or disable, and if I want to set a value for something. This returns something like the following:
{
"show":[
"TPM",
"HPG",
],
"hide":[
"BWL",
],
"values":{
"TPM":"BR",
},
"labels":{
"HVH":"Hoeveelheid:"
}
}
The problem here is the value for the TPM field. In the network-tab in the debugging console I see a call "http://localhost:53686/Registraties/getMethodes?initialValue=true&value=%22BR%22", and the result is: "{"id": "BR", "text": "BRANDEN" }". Looks good to me, but the TPM field stays empty.
Btw: when I try to set a default value for that field with the option " def: 'BR' ", then I see the same ajax call and then the field is set correctly.
I have a .Net background, and I try to get my head around all this jquery-magic, but this is too complicated for me. If anyone with some more experience would like to help me figure this out, that would be greatly appreciated! :-)
Answers
Agreed - that does look correct. Are you able to give me a link to your page please (I see you are using localhost above, but can you publish it somewhere)? That would let me trace through the live code to see what is going on. It looks like the option might not be getting set for Select2 for some reason.
Thanks,
Allan
Sorry I didn't see your reply sooner - I didn't get an email-notification.. ?
As with a lot of other topics here, this is not easily available from the outside. Maybe I can try to find a workaroud or a simulation somehow.. I'll try this next week, and will let you know.
Ugh.. seems like I have made some changes here and there, and I can't reproduce it anymore. I hate when this happens! I'll get back to you when it happens again.. thanks anyway!