<!-- fetchPicklistValues.html -->
<template>
<lightning-card title="WireGetPicklistValues" icon-name="custom:custom67">
<template if:true={picklistValues.data}>
<div class="slds-m-around_medium">
<template for:each={picklistValues.data.values} for:item="item">
<lightning-input
key={item.value}
label={item.label}
data-value={item.value}
type="checkbox"
onchange={handleCheckboxChange}
></lightning-input>
</template>
</div>
</template>
</lightning-card>
</template>
// fetchPicklistValues.js
import { LightningElement, wire } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import TYPE_FIELD from '@salesforce/schema/Account.Type';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
export default class FetchPicklistValues extends LightningElement {
@wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
objectInfo;
@wire(getPicklistValues, {
recordTypeId: '$objectInfo.data.defaultRecordTypeId',
fieldApiName: TYPE_FIELD
})
picklistValues;
handleCheckboxChange(event) {
console.log(event.target.dataset.value);
console.log(event.target.checked);
}
}
As per documentation, use
getPicklistValues
wire adapter (from lightning/uiObjectInfoApi module) to fetch picklist values for a specified field.Parameters:
recordTypeId
—(Required) The ID of the record type. Use the Object Info defaultRecordTypeId property, which is returned from getObjectInfo or getRecordUi.fieldApiName
—(Required) The API name of the picklist field on a supported object.propertyOrFunction
—A private property or function that receives the stream of data from the wire service. If a property is decorated with @wire, the results are returned to the property’s data property or error property. If a function is decorated with @wire, the results are returned in an object with a data property and an error property.