Dependant Select problems
Dependant Select problems
peterbrowne
Posts: 314Questions: 54Answers: 0
in Editor
I have a dependant select to populate the select learning_event.week_fk
var editor = new $.fn.dataTable.Editor( {
ajax: "program_data/learning_event_data.php",
table: "#learning_event_table",
template: '#learning_event_form',
fields: [ {
label: "Learning Event:",
name: "learning_event.learning_event_name"
}, {
label: "Outline:",
name: "learning_event.outline",
type: "ckeditor"
}, {
label: "Program Outcome:",
name: "program_outcome[].program_outcome_pk",
type: "select",
placeholder: 'No selection',
placeholderDisabled: false,
placeholderValue: 0,
multiple: true
}, {
label: "Unit Group:",
name: "learning_event.unit_group_fk",
type: "select",
placeholder: "Select Unit Group..."
}, {
label: "Week:",
name: "learning_event.week_fk",
type: "select",
placeholder: "Select Week..."
}, {
label: "Presentations:",
name: "presentation[].presentation_pk",
type: "select",
placeholder: 'No selection',
placeholderDisabled: false,
placeholderValue: 0,
multiple: true
}, {
label: "Conditions:",
name: "mcondition[].mcondition_pk",
type: "select",
placeholder: 'No selection',
placeholderDisabled: false,
placeholderValue: 0,
multiple: true
}, {
label: "Type:",
name: "learning_event_type[].learning_event_type_pk",
type: "select",
placeholder: 'No selection',
placeholderDisabled: false,
placeholderValue: 0,
multiple: true,
attr: {
size: 1
}
} ]
} );
editor.dependent('learning_event.week_fk', function(val, data, callback) {
editor.field('learning_event.unit_group_fk').input().on('change', function(e, d) {
//check if user or editor initiated update
if (d && d.editorSet) return;
$.ajax({
url: 'program_data/get_weeks.php',
data: {
"unit": editor.get('learning_event.unit_group_fk')
},
type: 'POST',
dataType: 'JSON',
success: function(data) {
callback(data);
}
});
});
});
var table = $( '#learning_event_table' ).DataTable( {
responsive: true,
"autoWidth": false,
"lengthMenu": [
[ 5, 10, 25, 50, -1 ],
[ 5, 10, 25, 50, "All" ]
],
columnDefs: [ {
targets: 1,
render: $.fn.dataTable.render.ellipsis( 150, true )
} ],
ajax: "program_data/learning_event_data.php",
dom: "Blfrtip",
columns: [ {
data: "learning_event.learning_event_name",
}, {
data: "learning_event.outline",
}, {
data: "program_outcome",
render: "[, ].program_outcome"
}, {
data: "unit_group.unit_group"
}, {
data: "week.week"
}, {
data: "presentation",
render: "[, ].presentation_name"
}, {
data: "mcondition",
render: "[, ].mcondition_name"
}, {
data: "learning_event_type",
render: "[, ].learning_event_type_name"
}, {
data: "learning_event.modified"
}, {
data: "learning_event.modified_by"
}, {
data: null,
className: "center",
defaultContent: '<a href="" class="editor_edit">Edit</a>'
} ],
select: {
style: 'os',
selector: 'td:first-child'
},
buttons: []
} );
more in next post...
This question has an accepted answers - jump to answer
Answers
The dependant is not getting populated using this in the get_weeks.php:
hard coding the value for $unit makes no difference. The above script reurns:
.
Hi,
Your
options
object contains the keyweek[].week_pk
, but there is no field in the Editor list of fields which matches that. Should it actually belearning_event.week_fk
?Allan
I'm not clear, sorry Allan. Can you include more of the code context so I can pinpoint?
There is in the Editor list:
The
week[].week_pk
is coming from the scriptget_weeks.php
as shown above, called in the dependant part for the editor.This should populate the dependant select in the editor with related records from table
week
:What am I doing wrong?
In the JSON return you use
{"options":{"week[].week_pk"
. But there is no field with that name. If you want to populate the list of options for thelearning_event.week_fk
field then update your PHP to be:Editor (client-side) will find a field with that name and then populate its options with that data.
Allan
Thanks Allan.
I now have the dependant getting the options OK. However the create and update is not happening, it just displays the progress indicator and doesn't do the insert or update.
This is the client page:
and the server side script:
Note that if I comment out the dependant section in the client script, then the update and create works fine. The problem then s of course that the weeks select just contains all records from table week.
Have you checked for errors in the browser's console?
Have you checked the XHR request and response, assuming that it happens before any errors.
Kevin
I have checked Network - XHR and it doesn't show any response or errors as the action isn't being performed.
So is the XHR request sent? If so then look at your server logs for errors.
Kevin
This is the editor showing the progress indicator in the top corner. This doesn't complete when dependant part of the script is active. It seems to hang. Commenting out that dependant script then the request completes.
It could be to do with the callback in the dependant script not being fired, there is no XHR.
This is the result from
get_weeks.php
to populate the dependant selectlearning_event.week_fk
To reiterate, the dependant gets populated fine, but with the dependant part of the client code below, editor does not fire the update or create, it hangs. If I comment out the code below, the update or create works.
I think I see the problem - you are adding a
change
event listener insidedependent()
and it is adding a new listener everytime dependent is triggered. What complicates this is that the callback doesn't execute until theunit_group_fk
field's value changes after the dependent action.So if dependent were to trigger, but the
unit_group_fk
field doesn't change value, then the callback will never happen, leaving error in an undefined state. That is what I think you are seeing here.We need to step back a little to see what your goal is here. I suspect this approach isn't quite the right way to go about it. Could you clarify what you want the interaction between
week_fk
andunit_group_fk
to be please?Thanks,
Allan
Thanks Allan.
I need to populate the select for
learning_event.week_fk
based on the value set in the select forlearning_event.unit_group_fk
.This should be for create and update.
For create, the user selects a value from
learning_event.unit_group_fk
, which then populateslearning_event.week_fk
.For update, it would be ideal if
learning_event.unit_group_fk
was automatically populated according to the value set inlearning_event.unit_group_fk
. Iflearning_event.unit_group_fk
is not set, then the user selects an option from that, andlearning_event.week_fk
is populated based on that selection.I did notice significant lag when populating the dependant, so your explanation of multiple listeners may be correct.
This example may help - it's disabling or enabling a field depending on whether it's an edit or a create. This is similar to your scenario, so you could set the value if it's an edit.
Colin
Thanks Colin, but that doesn't help with my dependant select issue...
It was meant to be in addition to Allan's statement, for how you want different behaviour for the create and the update - it was just some code to give you an idea of how that could be done.
Colin
Thanks for the clarification.
You want to use:
In that case. The first parameter of
dependent()
should be the field that the condition is dependent upon.You could could also have Editor make the Ajax call for you:
Then in
get_weeks.php
you would use$_POST['values']['learning_event']['unit_group_fk']
to get the value for the unit group and base the return on that.To set the options for
learning_event.week_fk
you then need to do:Allan
Thanks Allan, very helpful.
I tried:
with:
But it's not sending the value, at least not the
learning_event.unit_group_fk
value. Using the above and hardcoding a value in the query for$unit
in get_weeks.php works though.Using the following works, though I need to have the dependant select automatically populate based on the value of
learning_event.unit_group_fk
(on change and otherwise...):With
editor.dependent('learning_event.unit_group_fk', 'program_data/get_weeks.php');
and using:the following empty array is returned:
Hardcoding the value for
$unit
works fine...Any ideas why the following returns an empty array?
It suggests to me that the query isn't matching anything. If you
in your script, what does the return from the server show? It will be invalid JSON, but that doesn't matter, it is the return that is of interest.
Allan
Sorry Allan, where do I put
print_r($_POST);
? In get_weeks.php? If so, then how and where is thatprint_r
going to show when it is run fromedit_learning_event.php
?Yes. Basically anywhere in there. Then when the dependent action is triggered an Ajax request is made to
get_weeks.php
and the response will be in there. As I say, it will be invalid JSON, but for a quick debug test, that doesn't matter.This tech note explains how you can see the response from the server.
Allan
Thanks Allan. This is what I get:
The above was produced with
editor.dependent('learning_event.unit_group_fk', 'program_data/get_weeks_test.php');
inedit_learning_event.php
and
get_weeks.php
:Without print_r($_POST);