Mjoin with Dependent Select
Mjoin with Dependent Select
This is a rephrased question from here
I have a select list populated via an Mjoin (see code in following comment). The select is:
, {
label: "Learning Events:",
name: "learning_event[].learning_event_pk",
type: "select",
placeholder: "Select Learning Event...",
placeholderDisabled: false,
placeholderValue: 0,
multiple: true
}
This select has data that will need to be created, updated and deleted as per Editor functions and the Mjoin
.
Instead of showing all records for table learning_event
in the select above, I want to show only those records that relate to a selected option in another select unit_group.unit_group_pk
.
, {
label: "Unit Group:",
name: "unit_group.unit_group_pk",
type: "select",
placeholder: "Select Unit Group..."
}
The unit_group.unit_group_pk
select is populated by a separate AJAX function:
var unit_groups = [];
$.getJSON("program_data/get_unit_groups.php", function(data) {
var option = {};
$.each(data, function(i,e) {
option.label = e.text;
option.value = e.id;
unit_groups.push(option);
option = {};
});
}).done(function(){editor.field('unit_group.unit_group_pk').update(unit_groups);
});
I have tried using the following editor.dependant code
, but it causes problems with what seems to be an infinite loop causing refresh issues in the learning_event[].learning_event_pk
select and it won't do a create or an update.
editor.dependent('learning_event[].learning_event_pk', 'program_data/get_learning_events.php');
I assume I need the Mjoin to facilitate the DB transactions.
Any ideas on how this can be achieved?
Answers
This is the server-side code:
This is the full client side code:
is there any more information I can add to help get an answer?
The image below shows the two select lists. So, I just want to have the top select
unit_group.unit_group_pk
populate (or repopulate or filter) the bottom selectlearning_event[].learning_event_pk
.Given that the data in the two selects are related of course, which they are, what editor functionality can I use to achieve this??
You can use "dependent" and "update" if you are using "select" fields. Here is an example from my own coding. "planning_level_1" and "planning_level_2" are "select" fields which are interdependent. Same applies to "planning_level_3" that is dependent on both of the above.
This code is a bit longer than it needs to be because I added some code to optimize the number of field settings due to issues with Editor when to many field settings / updates occur.
Here are the Editor field definitions:
"planningLevelOptions" is a global variable that contains the default planning levels as an array of objects (label / value pairs).
Depending on the planning level you choose in level 1 you can only choose the remaining options in level 2. Same applies for level 3. I use "slice()" to make shallow copies of the initial options and "splice()" to delete options that have been chosen on the previous planning level(s) already. Then I use "update()" to update the respective "select" options.
Thanks. But I I was actually hoping for a simpler Editor functionality. I will give it a shot if inherent Editor functions don't exist.
Allan?
Hi,
Sorry for the delay. Traveling this week - back to normal on Tuesday! Have you read over this blog post on the topic?
I'll have a more detailed look at your code when I'm back
Allan
Yep, it's all about the same thing: You get options from somewhere (either ajax in your blog post or otherwise like in my example) and then you update the options "dependent" on your individual use case's conditions.
I think that is already rather simple. Depending on how complex your use case is and what you are doing on the server (in my case: nothing is done on the server) you have more or hardly any Javasript code.
Quoting your blog post, Allan:
That's the understatement of the year
Thanks @rf1234 and @allan. I have looked at the code provided from both suggestions, but just don't quite understand how to apply to my Editor code.
I guess the first thing to check is that the blog post does show the kind of thing you want?
If so, at which part do you get stuck with how to apply it to your own page?
Allan
I think with "all records" you mean "all options" to choose from.
If that is true you have pretty much exactly my use case. So you could use my code.
You seem to be loading the options from the server though: Hence loading the updated options of "learning event" from the server "dependent" on a change of selection of "unit group" using Allan's blog post example is probably the better option for you.
@rf1234 Possibly, although I did try that before and it had issues with endless loops and not doing an update/create etc.
I will give the blog answer another go...and post back.
I'm still not sure how to implement the blog code in my solution.
Is in that solution:
editor.dependent( 'continent', '/api/countries' );
referring to the following script?
I assume
'id as value', 'name as label'
need to be changed to the actual column names of the 'country' table.Also in my case, I would still need to populate the parent select separately, as there is no lookup table for that relationship.
So I have been using:
I have never used the blog post example. I always do it differently using my own proprietary ajax calls to retrieve the options and then use "update()" client side to update the options.
In order to make "update()" work without further modifications you need to return "label" / "value" pairs from the server. Like in this simple example:
"value AS label, id AS value" looks confusing but it works because the label is usually some kind of name which I called "value" in this case and the value is mostly an id.