1

//html

            <lightning-combobox
                name="selectedYear"
                value={currentYear}
                placeholder="Select Year"
                options={options}
                onchange={handleChangeYear}>
            </lightning-combobox>

//js

@track currentYear = (new Date()).getFullYear();
@track options;

    handleChangeYear(event) {
        this.currentYear = event.detail.value;
    }

    getYears(){
        getYears()
            .then((returnedData) => {
                let years = [];
                returnedData.forEach((item) => {
                    years.push( {label : 'Regional Expenses ' + item.Year, value : item.Year })
                });
                years.sort((a, b) => a.value > b.value ? -1 : 1);
                this.options = years;
                this.currentYear = years[0].value;
            }).catch(err => console.log(err));
    }

//options:

[{"label":"Regional Expenses 2021","value":2021},{"label":"Regional Expenses 2020","value":2020},{"label":"Regional Expenses 2019","value":2019},{"label":"Regional Expenses 2018","value":2018}]

After loading the page, I get the default value selected:

enter image description here

But after selecting any other value , it is not saved and I only see the placeholder label...

enter image description here

enter image description here

1 Answer 1

5

The value of your options is a number, while event.detail.value holds a string, so after selecting an option this.currentYear will hold a wrong value, i.e. '2020' instead of 2020.

The unary plus operator is the fastest way to convert it to a number:

handleChangeYear(event) {
    this.currentYear = +event.detail.value;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .