1

I know that we can't write a specific condition in <template if:true={specificcondition}> LWC HTML file.

However what is the best way to implement next HTML code :

<template for:each={accounts} for:item="account">
    <template if:true="{account.Payment_Status__c = "Completed" }">
        <!-- some HTML code -->
    </template>
    <template if:true="{account.Payment_Status__c = "Not Received" }">
        <!-- some HTML code -->
    </template>
    <template if:true="{account.Payment_Status__c = "Blank" }">
        <!-- some HTML code -->
    </template>

</template>

JS file :

@wire(getAccountList)
setAccountList({ data, error }) {
    if (data) {
        this.accounts = data;
    } else if (error) {
        console.error(error);
    }
}

I know that the above code will not work.

But what is the best way to get the working approach for this ?

2
  • Could you please share the JS code how you get the accounts list, Maybe you should go through this list in JS file and create specific variables for each condition. Commented Sep 4, 2022 at 19:11
  • 1
    @VitaliiDehnerys I added JS code to my question.
    – user121838
    Commented Sep 4, 2022 at 19:39

2 Answers 2

1

You can go through the accounts list in your JS file and create specific variables for each condition.

So the JS code would look like this :

@wire(getAccountList)
setAccountList({ data, error }) {
    if (data) { 
        let temporaryList = data;
        temporaryList.forEach( (account) => {
            account.completedPaymentStatus   = account.Payment_Status__c === "Completed";
            account.notReceivedPaymentStatus = account.Payment_Status__c === "Not Received";
            account.blankPaymentStatus       = account.Payment_Status__c === "Blank";
        })
        this.accounts = temporaryList;
    } else if (error) {
        console.error(error);
    }
}

And the HTML code would look like this :

<template for:each={accounts} for:item="account">
    <template if:true={account.completedPaymentStatus}>
        <!-- some HTML code -->
    </template>
    <template if:true={account.notReceivedPaymentStatus}>
        <!-- some HTML code -->
    </template>
    <template if:true={account.blankPaymentStatus}>
        <!-- some HTML code -->
    </template>
</template>
0
3

I would go with LWC Templates - without additional loops and ifs. Quite easy to extend about new payment status.

// as it is now.
@wire(getAccountList)
setAccountList({ data, error }) {
    if (data) {
        this.accounts = data;
    } else if (error) {
        console.error(error);
    }
}
<template for:each={accounts} for:item="account">
    <c-account-payment-status account={account}></c-account-payment-status>
</template>
//accountPaymentStatus.js
import { LightningElement, api } from 'lwc';

import completed from './templates/completed.html';
import notReceived from './templates/notReceived.html';
import blank from './templates/blank.html';

const STATUS_TO_TEMPLATE = {
    'Completed': completed,
    'Not Received': notReceived,
    'Blank': blank
};

export default class SpaPages extends LightningElement {
    @api account;

    render() {
        return STATUS_TO_TEMPLATE[this.account?.Payment_Status__c] || STATUS_TO_TEMPLATE['Blank'];
    }
}
//accountPaymentStatus/template/completed.html
<template>
      <!-- Completed HTML code -->
</template>
//accountPaymentStatus/template/notReceived.html
<template>
      <!-- Not Received HTML code -->
</template>
//accountPaymentStatus/template/blank.html
<template>
      <!-- Blank HTML code -->
</template>

References:

3
  • 1
    Well, sometimes the Shadow DOM can get in the way of this design, but a nice callout to using a rarely used feature to separate the design from the logic.
    – sfdcfox
    Commented Sep 4, 2022 at 20:39
  • Thanks for your answer and it is really great approach. However what if I need to execute more than one condition (for example three ), it will just became really complex.
    – user121838
    Commented Sep 4, 2022 at 21:15
  • Great! It depends on what exactly you have to do. You can separate different conditions to separated components. Commented Sep 4, 2022 at 21:23

You must log in to answer this question.