function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Thirumagal SiluvaithasanThirumagal Siluvaithasan 

Using arithmetic comparison in if:true statement in Lightning web component

Hi Team,

I have for-each loop in my LWC component. And I want to check below condition inside  the loop.
 
<template for:each={object.MonthListBefore} for:item="month" >
      <template if:true={month == 'Jan'}> 
       <li key={object.Id} class="year">{object.currentYear}</li>       
</template>
</template>



But this is not working.  I am not sure how could I use the arithmetic comparison in HTML file of LWC. Could anyone please help me to resolve this?
Best Answer chosen by Thirumagal Siluvaithasan
Ravi Dutt SharmaRavi Dutt Sharma
I think it is not possible to do a check in template if:true. What you can do over here is that in your object, as you have several properties like MonthListBefore, Id, and currentYear, you can have another property called showOnUI (or any name you want). You calculate this property before populating on the object (either in your JS controller or Apex). Then you use this property in template if:true

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

you can use something below, 
 
<template for:each={object.MonthListBefore} for:item="month" >
      <template if:true={isJanmonth}> 
       <li key={object.Id} class="year">{object.currentYear}</li>       
</template>
</template>

in your JS controller, 
 
get isJanmonth() {
        return this.selectVal === "Jan";
    }

Hope this will give you some idea. 

Thanks
karthik




 
Thirumagal SiluvaithasanThirumagal Siluvaithasan
Hi karthikeyan ,

Here I have to check month == 'Jan'.   "month" is coming from for each loop. It is dynamic.
 
<template for:each={object.MonthListBefore} for:item="month" >


 
Ravi Dutt SharmaRavi Dutt Sharma
I think it is not possible to do a check in template if:true. What you can do over here is that in your object, as you have several properties like MonthListBefore, Id, and currentYear, you can have another property called showOnUI (or any name you want). You calculate this property before populating on the object (either in your JS controller or Apex). Then you use this property in template if:true
This was selected as the best answer
Thirumagal SiluvaithasanThirumagal Siluvaithasan
@ Ravi Dutt Sharma.
Thank you. I will try and let you know
Thirumagal SiluvaithasanThirumagal Siluvaithasan
Hi Ravi Dutt Sharma,

Thank You. Your solution is working fine !
Ravi Dutt SharmaRavi Dutt Sharma
I am glad it worked for you.