Skip to main content

Hi,

I am trying to create a lightening web component combobox but it's not working. Please check the code

Javascript:

import { LightningElement, api, wire, track } from 'lwc';

import methodName from '@salesforce/apex/MoveAttachment.methodName';

export default class attachmentMove extends LightningElement {

    @track record;

    @track result;

    @track error;

    @wire(methodName)sobjectName;

    wiredsobjectName({error, data}){

        if(data){

            if(data.length>0){

            this.arrays.array.forEach(element => {

            this.record.push ({label: record[i].Id, value: record[i].Name });

            console.log('check=====>', this.result);

            });

        }

            this.result = record;

            this.error = undefined;

           

        } else if (error){

            this.error = error;

            this.result = undefined;

        }

    }

    get options() {

        console.log('option--->', this.sobjectName.data);

        return this.sobjectName.data;

    }

    get values(){

        return this.result;

    }

    handleChange(event) {

        this.value = event.detail.value;

    }

}

/*

    */

 

HTML:

<template>

    <lightning-card title="Combo Box_Type Values" icon-name="custom:custom67">

    <template  if:true={sobjectName.data}>

    <lightning-combobox

            name="List of Object"

            label="List of Object"

            value={result}

            placeholder="Select Object"

            options = {options}

            onchange={handleChange} >

        </lightning-combobox>

    <p>Selected value is: {options}</p>

</template>

</lightning-card>

</template>

If use get option then it shows blank value in dropdown, if i use this.result. then it does not work. say this.result is undefined and also this.result. Please help me to fix it.

Thanks and regards

Nishant Shrivastava
답변 3개
  1. 2022년 6월 13일 오전 6:49
    Hi Nishat,

    Please use below code:-

     

    import { LightningElement, api, wire, track } from 'lwc';

    import methodName from '@salesforce/apex/MoveAttachment.methodName';

    export default class attachmentMove extends LightningElement {

    @track TypeOptions;

    @wire(methodName)

    wiredsobjectName({error, data}){

    let options = [];

    if (data) {

    try {

    for (var key in data) {

    // Here key will have index of list of records starting from 0,1,2,....

    options.push({ label: data[key].Name, value: data[key].Id });

    // Here Name and Id are fields from sObject list.

    }

    this.TypeOptions = options;

    } catch (error) {

    console.error('check error here', error);

    }

    } else if (error) {

    console.error('check error here', error);

    }

    }

    get optionList() {

            return this.TypeOptions.length()>0 ? true : false ;

        }

    handleChange(event) {

    this.value = event.detail.value;

    }

    }

     

    <template>

    <lightning-card title="Combo Box_Type Values" icon-name="custom:custom67">

    <template if:true={optionList}>

    <lightning-combobox

    name="List of Object"

    label="List of Object"

    value={value}

    placeholder="Select Object"

    options = {TypeOptions}

    onchange={handleChange} >

    </lightning-combobox>

    <p>Selected value is: {TypeOptions}</p>

    </template>

    </lightning-card>

    </template>

    if you need any assistanse, Please let me know!!

    Kindly mark my solution as the best answer if it helps you.

    Thanks

    Mukesh

     
0/9000