dependent ... set value of a different field

dependent ... set value of a different field

wowzaaawowzaaa Posts: 3Questions: 3Answers: 0

so i am searching the web and docs for 3h now an i cannot believe i cannot find a single example of updating column b when column a changes ... maybe i have a problem but i consider this one of the most common use cases

so

editor.dependent('a', (e, data, callback) => { ???? });

anyone care to enlighten me?
thanks

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 632Questions: 37Answers: 100
    Answer ✓

    This should help you out:

    offerCredEditor
        .dependent( 'offer.instrument', function ( val, data, callback ) {
            var self = offerCredEditor;
            setEditorOfferFields(self);
            setEditorDerivativeOptions
                    ( self.field('offer.derivative'),
                      self.val('offer.type'), val, 'create' );                
        })
        .dependent( 'offer.type', function ( val, data, callback ) {
            var self = offerCredEditor;
            setEditorOfferFields(self);
            setEditorDerivativeOptions
                    ( self.field('offer.derivative'),
                      val, self.val('offer.instrument'), 'create' );
        });
        
    function setEditorOfferFields(that) {
        //select fields can only be set to a valid option in JS - otherwise setFormatter in PHP
        if ( that.val('offer.instrument') !== 'Z') { //if it is not a naked derivative
            if ( that.val('offer.type') >= 'V') { // if it is a variable instrument
                that.hide( ['offer.term'] )
                    .show( ['offer.ref_rate', 'offer.ref_rate_period',
                            'offer.negative_allowed', 'offer.spread'] );   
            } else {
                that.set( { 'offer.spread': 0 } )
                    .hide( ['offer.ref_rate', 'offer.ref_rate_period',
                            'offer.negative_allowed', 'offer.spread'] )
                    .show( ['offer.term'] );   
            }
        } else { //if it is a naked derivative show everything
            that.show( ['offer.ref_rate', 'offer.ref_rate_period',
                        'offer.negative_allowed', 'offer.spread', 'offer.term'] );   
        }
        if ( that.val('offer.instrument') === 'W' ) { //if it is deposit!
            that.hide( ['offer.repayment_method'] );                        
        } else {
            that.show( ['offer.repayment_method'] );   
        }
        if ( that.val('offer.instrument') === 'Z' ) { //if it is a derivative only offer
            that.set( { 'offer.initial_effective_rate': 0 } )
            that.hide( ['offer.type', 'offer.initial_effective_rate'] );
            that.field('offer.ref_rate').update([ { label: "-", value: "" } ], false)
                                        .update(refRateOptions, true);
            that.field('offer.ref_rate_period').update([ { label: "-", value: "" } ], false)
                                               .update(refRatePeriodOptions, true);
        } else {
            that.show( ['offer.type', 'offer.initial_effective_rate'] );
            that.field('offer.ref_rate').update(refRateOptions, false);
            that.field('offer.ref_rate_period').update(refRatePeriodOptions, false);
        }
    }
    
    function setEditorDerivativeOptions(derivative, type, instrument, action) {
        //derivative = entire field; type = value of field; instrument = value of field
        var derivativeValue = derivative.val();
        //if naked derivative type is set to spaces
        if (instrument === 'Z') {
            type = '  ';
        }
        
        //if called from rfp or offer action is always set 'create' because it makes
        //no difference and changes are allowed there unlike contracts and proposals    
        if ( action === 'edit' && derivativeValue >= 'A' ) {
            //in case there already is a derivative you can only change to multiple derivatives
            //because you shouldn't delete a component only add components
            //for deposits that have a derivative: no changes allowed at all because they can't 
            //have multiple derivatives
            var i = 0;
            while (derivativeOptions[i]) {
                if (derivativeOptions[i].value === derivativeValue) {
                    derivative.update([ derivativeOptions[i] ], false);
                }
                i++;
            }
            if ( derivativeValue !== 'X5' &&        //no change allowed if
                 instrument !== 'W'             )  { //multiple already or deposit
                 derivative.update(derivativeOptionsMultipleOnly, true);
            }    
        } else { //action is edit but no derivative or action is create    
            if ( type === 'F' ) { //if it is fixed rate
                derivative.update(derivativeOptionsNoCapFloorCollar, false);
            } else {
                if ( type.substring(0,1) === 'V' ) { //if it is variable rate
                    if (type >= 'V1') { //if it includes cap floor collar
                        derivative.update(derivativeOptionsNoCapFloorCollarFRA, false);                       
                    } else { //if it does not include a derivative
                        derivative.update(derivativeOptionsNoFRA, false);
                    }
                } else { //type neither F nor V.. ==> type is blank
                    if (instrument === 'Z') {
                        derivative.update(derivativeOptionsNakedDerivative, false);
                    } else {
                        derivative.update(derivativeOptionsNoSwapsNoMultiple, false);
                    }
                }
            }
            //for non-deposits also swaps and mutliple derivatives possible
            if (instrument !== 'W') {
                derivative.update(derivativeOptionsSwapsAndMultiple, true);
            }
        }
    }
    

    You can also have dependent within another event like this:

    contractGovEditor
        .dependent('contract.expired', function ( val, data, callback ) {
            if (val == '1') {
                contractGovEditor
                        .show( ['contract.exp_date'] );
                if ( contractGovEditor.val('contract.exp_date') <= '' ) {
                    contractGovEditor
                        .field('contract.exp_date').set(yesterday);
                }
            } else {
                contractGovEditor
                        .set( { 'contract.exp_date': '' } )
                        .hide( ['contract.exp_date'] );
            }
        })
        .on('open', function(e, mode, action) {
            var self = this;
            $('.DTE_Form_Info').addClass("text-warning");
            if ( self.val('contract.serial') <= '0' ) {
                self.message( function () {
                    return 'If you save changes a contract serial number\n\
                            will be generated and saved!';
                })
            }
            if (action === 'edit') {
                self.disable( [ 'contract.govdept_id',
                                'contract.instrument',
                                'contract.type'        ] );
            } else { // action not = edit
                self.enable ( [ 'contract.govdept_id',
                                'contract.instrument',
                                'contract.type'        ] );
            }
            self.dependent( 'contract.type', function ( val, data, callback ) {
                setEditorContractFields(self);
                setEditorUnderlyingField(self);
                setEditorDerivativeOptions
                    ( self.field('contract.derivative'),
                      val, self.val('contract.instrument'), action );
            });
            self.dependent( 'contract.instrument', function ( val, data, callback ) {
                //for non-deposits also swaps and mutliple derivatives possible
                setEditorContractFields(self);
                setEditorUnderlyingField(self);
                setEditorDerivativeOptions
                    ( self.field('contract.derivative'),
                      self.val('contract.type'), val, action );
            });
        });
    
  • mfmf Posts: 35Questions: 8Answers: 0

    Thanks for sharing your code rf1234, verry helpfull

Sign In or Register to comment.