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
mahamed raheemmahamed raheem 

conditional rendering in lwc

I have to display fields on condition-based

if type equals text then I have to display text fields

  <template for:each={questions} for:item="qs">
            <div key={qs}>
        <template if:true={qs.type='multiselect'}>
                           
            <lightning-dual-listbox name="duallist" label="multiselect" source-label="Available" selected-label="Selected"
            options={options}
            onchange={handleMultiselectChange}></lightning-dual-listbox>
        </template>
        <template if:true={qs.type='dropdown'}>           
            <lightning-combobox name="combobox" label="dropdown" value={value} placeholder="Select" options={options}
            onchange={handleDropdownChange} ></lightning-combobox>
        </template>   
            <template if:true={qs.type='checkbox'}>                
                <lightning-input type="checkbox" label="checkbox" name="checkbox"></lightning-input>
              </template>
              
              <template if:true={qs.type='text'}>
                <lightning-input type="text" label="text" name="text"></lightning-input>
              </template>
            </div>
            </template>>


Please help me on this
Best Answer chosen by mahamed raheem
David Zhu 🔥David Zhu 🔥
The multiselect boolean should be on record level instead of on page level.
i.e.
<template if:true={qs.multiselect}>

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Mahamed, 

Please see the following example to render DOM Elements Conditionally

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/create_conditional

Anudeep
David Zhu 🔥David Zhu 🔥
I don't believe expression function is supported in LWC html file.
<template if:true={qs.type='multiselect'}> would not work.

I would suggest add a few Booleans in Wapper class of "question".
boolean isMultiSelect;
boolean isText;   and etc.

then change your HTML to: 
<template if:true={isMultiSelect'}> , etc
mahamed raheemmahamed raheem
Hi David 

I have tried the same thing but my for:each iterations values are coming duplicate

   <template for:each={question} for:item="qs">
                    <div key={qs.id}>                 
                    <template if:true={multiselect}>
                        <lightning-dual-listbox name="duallist" label={qs.label} source-label="Available" selected-label="Selected"
                        options={options}
                        onchange={handleMultiselectChange}></lightning-dual-listbox>
                    </template>
            
                    <template if:true={dropdown}>           
                        <lightning-combobox name="combobox" label={qs.label} value={value} placeholder="Select" options={options}
                        onchange={handleDropdownChange} ></lightning-combobox>
                    </template>   
            
                        <template if:true={checkbox}>                
                            <lightning-input type="checkbox" label={qs.label} name="checkbox"></lightning-input>
                          </template>
                          
                          <template if:true={text}>
                            <lightning-input type="text" label={qs.label} name="text"></lightning-input>
                          </template>
                    </div>
                </template>

----Js---

  this.question=result;
  for (let i = 0; i < result.length; i++) {
                dataType.push(result[i].type);                 
            
            }
                if(dataType.includes("Multi Select")){
                    this.multiselect=true;                 
                }else {
                   this.multiselect=false;                  
                }
                if(dataType.includes("Dropdown")){
                    this.dropdown=true; 
                }else{
                    this.dropdown=false;
                }
                if(dataType.includes("Checkbox")){  
                    this.checkbox=true; 
                }else{
                    this.checkbox=false;
                }
                    if(dataType.includes("Text")){  
                        this.text=true; 
                }else{
                    this.text=false;
                }
               
        })
David Zhu 🔥David Zhu 🔥
The multiselect boolean should be on record level instead of on page level.
i.e.
<template if:true={qs.multiselect}>
This was selected as the best answer
mahamed raheemmahamed raheem
Thanks David its working