Sunday, April 22, 2018

Weirdness with Chained Selects in the Admin

I have two chained selects in an inline form in my admin page. I wrote a short javascript script to pull the data from the database for the second select when a selection is made in the first select. It all seems to work - the scrip fires when a selection is made in the first select, the correct values are loaded into the second select, and in the browser I can make a selection in the second select. 

When I try to save the page, the values in the second select magically disappear, and I get a validation error on the page. 

If I load the page, run my script and select values in both selects, and then look at the source for the page, I see that the first select has the correct value selected, but the second select is empty. 

I am not sure id I am missing something in my js code to make the changes to the second select permanent, or if django does something to the data when the form is saved?

This is my java script:

var $ = django.jQuery;

$(function() {
    var t = $("#id_documentmetadatavalue_set-0-metadata");
    t.change(function() {
        if (t.val() !== '') {

            console.log("t.change occured");

            var selected = [];
            $("#id_documentmetadatavalue_set-0-metadata :selected").each(function(i, sel) {
                selected.push($(sel).val());
            });

            console.log("the selected values: " + selected);
            $.ajax({
                url: '/ajax/metadatavalue_query/' + t.val(),
                dataType: 'json',
                success: function(data) {
                    //       data = $.parseJSON(JSON.stringify(data));
                    var options;
                    options = '<option value>---------</option>';
                    $.each(data, function(index, item) {
                        options += '<option value="' + item.id + '">' + item.value + '</option>';
                    });

                    console.log("the options are: " + options);

                    $("#id_documentmetadatavalue_set-0-metadataValue").html(options);
                    $("#id_documentmetadatavalue_set-0-metadataValue").val($("#id_documentmetadatavalue_set-0-metadataValue option:first").val());
                    $("#id_documentmetadatavalue_set-0-metadataValue").selectmenu('refresh');
                }
            });
        }
    });
});

Thanks for any insights you may have.

Mark

PS I know there are chained selects apps for django out there. For this small application I didn't want to add another app to my project, and I thought I would learn something by "rolling my own". Perhaps that was a mistake, in hindsight!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEqej2Ng0RYz-M5gaXKQcH77NTZDrjSmoXU%2BgXL8Zu%2BbjvZWaQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment