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

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

carrarachristophecarrarachristophe Posts: 100Questions: 24Answers: 2
edited August 2022 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: 15,177Questions: 1Answers: 2,590
    edited September 2022 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: 100Questions: 24Answers: 2

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

  • carrarachristophecarrarachristophe Posts: 100Questions: 24Answers: 2

    Hello,
    I refer to the above.
    I was wondering how I could replace

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

    by something like:

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

    How can I change the value of the field by computing the value of another one?

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    field().val() can be both a getter and a setter, so you could have:

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

    That should do the trick!

    Colin

  • carrarachristophecarrarachristophe Posts: 100Questions: 24Answers: 2
    edited February 12

    Hi Colin,
    Thank you for your prompt feed-back.

    I am getting:
    [object Object]1

    When I change the code to:

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

    I am getting (in case we have for example 'bibliotheque_bibliotheques.voeux'=12345):
    123451

    So I changed to:

    editor.field('bibliotheque_bibliotheques.possession').val(
      parseFloat(editor.field('bibliotheque_bibliotheques.voeux').val())+1);
    )
    

    I don't know if there is a cleanest way to do it

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Looks good to me :)

    Allan

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    Sorry, @carrarachristophe , yep, I said what to do, then promptly mistyped it, omitting the val(). Glad all sorted,

    Colin

  • carrarachristophecarrarachristophe Posts: 100Questions: 24Answers: 2

    No worries.
    You pointed me to the right direction.
    Take care.

Sign In or Register to comment.