Dependent fields - show/hide + editor.field('').val()

Dependent fields - show/hide + editor.field('').val()

carrarachristophecarrarachristophe Posts: 39Questions: 10Answers: 0
edited August 31 in Editor

Hello,

Based on that example, I am hidding/showing some fields based on a radio button. It works fine.
But In addition to showing/hidding some fields, I need to set the value of an hidden field to 0, or 1.
How can I add:

editor.field( 'bibliotheque_bibliotheques.possession').val(1);

in case val !== '1'
and add

editor.field( 'bibliotheque_bibliotheques.possession').val(0);

in case val === '1'

editor.dependent( 'bibliotheque_bibliotheques.voeux', function ( val ) {
        return val === '1' ?
            { hide: ['bibliotheque_bibliotheques.date_achat', 'bibliotheque_bibliotheques.prix_achat', 'bibliotheque_bibliotheques.neuf'] } :
            { show: ['bibliotheque_bibliotheques.date_achat', 'bibliotheque_bibliotheques.prix_achat', 'bibliotheque_bibliotheques.neuf'] } 
    } );

I don't find any example of that.

Thanks and regards,
Christophe

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 14,531Questions: 1Answers: 2,484
    edited September 1 Answer ✓

    It would be easier to add that combined functionality into a function, something like:

    editor.dependent( 'bibliotheque_bibliotheques.voeux', function ( val, data, callback ) {
      if (val === '1') {
        editor.field( 'bibliotheque_bibliotheques.possession').val(0);
        editor.hide([ 'bibliotheque_bibliotheques.date_achat', 'bibliotheque_bibliotheques.neuf' ]);
      }
      else {
        editor.field( 'bibliotheque_bibliotheques.possession').val(1);
        editor.show([ 'bibliotheque_bibliotheques.date_achat', 'bibliotheque_bibliotheques.neuf' ]);
      }
    
      callback(true);
    });
    

    Colin

  • carrarachristophecarrarachristophe Posts: 39Questions: 10Answers: 0

    As simple as that...
    Thank you Colin, it worked like a charm.

Sign In or Register to comment.