0

How create/edit record with fields displayed (hide/show) depending on the values ​​of adjacent fields?

What are the best practice for that case? Thank you!

For example:

HTML

<template if:true={trip}>
                    <lightning-record-edit-form object-api-name="RUEV_Trip__c" record-id={tripId} onsuccess={handleSuccess}>
                        <lightning-messages></lightning-messages>

                        <lightning-input-field value={type} data-name="type" onchange={handleChange} field-name="Type_Of_Trip__c">
                        </lightning-input-field>

                        <template if:true={isForeignTrip}>
                            <lightning-input-field field-name="Purpose_of_travel__c">
                            </lightning-input-field>
                        </template>
                    </lightning-record-edit-form>
</template>

JS:

@track type;

handleChange(event) {
        const targetName = event.target.dataset.name;
        if (targetName === 'type') {
            this.type= event.target.value;
        }
}

But also if existing record @track trip loaded from Apex by @track tripId, it should pass value to local var:

@wire(getTrip, {
        recordId: '$tripId'
    })
    wiretGetTrip({
        data
    }) {
        if (data) {
            this.trip = data;
            this.type= this.trip.Type_Of_Trip__c;
    }
}

Example

  • what is your exact issue? – sanket kumar Feb 17 at 17:32
  • Hello! I just want to know is it normal practice to use that way or maybe there is some more legal workaround :)) thanks! – Ivan S. Feb 17 at 19:33
  • if you have LWC components nested in another, use parent to dispatch events to both. If you just add them onto the same Flexi Page, use pub-sub developer.salesforce.com/docs/component-library/documentation/… – zaitsman Feb 17 at 23:31
  • I mean which way to use to conditionally display record fields? – Ivan S. Feb 18 at 5:57
  • template if: true is a good way to render element conditionally as it will remove the element from DOM if the condition does not get satisfied. – sanket kumar Feb 18 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.