Handling fields (multiple selections) with values that depend on a value in a previous field - Django

1

I want to make a form where the values of a particular field depend on a selected value in a previous field. The idea is that both fields must be of multiple selection.

I was working with django-smart-selects , but I had these two drawbacks described here :

Investigating in some issues and on the web, there is someone who has the same problem as me.

And in some community groups, users have said that the django-smart-selects application probably does not support some functionalities and they even recommend doing validations or checking the content of the fields via JS or manually.

I wanted to know if you have had experiences with this concern that I have and if suddenly they resolved it.

They know some third-party application in which the values of a particular field depend on a value selected in a previous field. The idea is that both fields can have multiple selection values.

Any guidance I will be grateful for.

    
asked by bgarcial 02.03.2016 в 21:11
source

1 answer

1

First of all I have never used Django but it seems a challenge in any Framework or Library. In my experience, whenever you want to do very complex things with a form, it is best to manipulate it with Javascript. You can use only Jquery or combine it with your preferred framework to handle events.

I show you an example of a select when you change the value using the Jquery .change () event.

link

I will use the class sr-only of Bootstrap that when applied to an element, will take away the visibility ( visibility: hidden ).

link

Suppose you have a select with three values and two fields that depend on it. If the value is ValueDelSelect1 , then you sample FieldQueDepende1 and ensure that FieldQueDepende2 is hidden. If the value is ValueDelSelect1 then you do the opposite. If the third value of the Select is an arbitrary one then you remove the two dependent fields from the view. The same concept applies to Radios or groups of Checkboxes.

$('#IDdelSelect').change(function(){
       if( $(this).val() == 'ValorDelSelect1'){
           $('#campoQueDepende1').removeClass('sr-only');
           $('#campoQueDepende2').addClass('sr-only');
       }else if( $(this).val() == 'ValorDelSelect2'){
           $('#campoQueDepende1').removeClass('sr-only');
           $('#campoQueDepende2').addClass('sr-only');
       }else{
           $('#campoQueDepende1').addClass('sr-only');
           $('#campoQueDepende2').addClass('sr-only');
       }

});

If you use a Javascript library like Backbone you can use something very similar using Views and Events when the Input value changes. This will be customized for each Form since only with Jquery your JS scripts can become difficult to maintain.

I hope I help you.

    
answered by 03.03.2016 в 06:15