Dependent fields with two or more criteria

Dependent fields with two or more criteria

musicawaysmusicaways Posts: 5Questions: 3Answers: 0

Hello guys.
I would like to modify the fields in a form (show / hide) according to the value selected in a select field that has three values.
This process has to be done with three different criteria depending on the selected value.
Return the code I used, can you help me to figure out where I am wrong?
Thanks a lot.

   editor.dependent( 'options', function ( val ) {
    return val === 'Simple' ?
        { hide: ['1', '2', '3', '4', '5','6','7','8'] } :
        { show: [] };
    return val === 'Simple2' ?
        { hide: ['5', '6', '7', '8'] } :
        { show: ['1', '2', '3', '4'] };
    return val === 'Simple3' ?
        { hide: ['1', '2', '3', '4'] } :
        { show: ['5', '6', '7', '8'] };
} );

This question has an accepted answers - jump to answer

Answers

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28
    edited June 2017

    Lines 5-10 are never reached. Lines 2 says 'if val is 'Simple' do line 3, otherwise do line 4, it then returns the val so it can never reach llines 5+. 'Simple2' and 'Simple3' they would be caught by line 4 because they don't match the first test and evaluate to the last clause of the ternary operator.
    You'll need to change your logic so you can check for your other cases before returning.

  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14
    Answer ✓

    Hi musicaways, maybe like this

    editor.dependent( 'options', function ( val ) {
    
    var fields;
    
    if(val === "Simple"){
      fields =  { hide: ['1', '2', '3', '4', '5','6','7','8'],  show: [] };
      return fields
    }
    else if(val === "Simple2"){
     fields =   { hide: ['5', '6', '7', '8'], show: ['1', '2', '3', '4'] };
     return fields
    }
    else if(val === "Simple3"){
     fields =  { hide: ['1', '2', '3', '4'],  show: ['5', '6', '7', '8'] };
     return fields
    }
    
    });
    

    Shay

  • musicawaysmusicaways Posts: 5Questions: 3Answers: 0

    Thank you very much Rob Brun, works great!

  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14

    Hi musicways, I am glad that solution worked for you. If you would be so kind please mark my answer :-D

  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin

    It wasn't asked as a question originally, but rather as a discussion, so it can't be marked with an accepted answer I'm afraid.

    Allan

  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14

    Oh, I see :-(

  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin

    OOO - I've just discovered I can change them! I've just done that and marked your reply as the answer.

    Allan

  • Rob BrunRob Brun Posts: 56Questions: 2Answers: 14

    Radical! thank you Allan :-D

    Shay

This discussion has been closed.