Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiselect: selectedOptions within selectedItemsTemplate not cleared #15277

Open
capc0 opened this issue Apr 15, 2024 · 7 comments
Open

multiselect: selectedOptions within selectedItemsTemplate not cleared #15277

capc0 opened this issue Apr 15, 2024 · 7 comments
Labels
Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Milestone

Comments

@capc0
Copy link
Contributor

capc0 commented Apr 15, 2024

Describe the bug

When using a multiselect with selectedItemsTemplate template, the internal selectedOptions variable is not always cleared correctly. Therefore a previous value is still rendered within the multiselect.

Unfortunatly I am currently not able to reproduce this within a stackblitz issue template, but the problem is that the internal selectedOptions variable is not cleared when the modelValue and visibleOptions is set to an empty array [].

Because visibleOptions is empty here https://github.com/primefaces/primeng/blob/master/src/app/components/multiselect/multiselect.ts#L1122, the variable selectedOptions is not updated and still has dirty state.


As a workaround the following can be added:

else if (this.selectedOptions?.length > 0) {
                this.selectedOptions = [];
                this.cd.markForCheck();
}

I believe the bug was introduced with issue #14187

Environment

Latest

Reproducer

https://stackblitz.com/edit/github-8p6qdv?file=src%2Fapp%2Fapp.component.html

image

Angular version

17

PrimeNG version

17.13.0

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

20

Browser(s)

No response

Steps to reproduce the behavior

No response

Expected behavior

No response

@capc0 capc0 added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Apr 15, 2024
@Xriuk
Copy link

Xriuk commented Apr 17, 2024

+1

@Dharmang92
Copy link

Facing the same issue. Any update?

@capc0
Copy link
Contributor Author

capc0 commented May 14, 2024

As a workaround you can patch the multiselect constructor locally https://github.com/primefaces/primeng/blob/master/src/app/components/multiselect/multiselect.ts#L1182

from:

effect(() => {
    const modelValue = this.modelValue();

    const visibleOptions = this.visibleOptions();
    if (visibleOptions && ObjectUtils.isNotEmpty(visibleOptions)) {
        if (this.optionValue && this.optionLabel && modelValue) {
            this.selectedOptions = visibleOptions.filter((option) => modelValue.includes(option[this.optionLabel]) || modelValue.includes(option[this.optionValue]));
        } else {
            this.selectedOptions = modelValue;
        }
        this.cd.markForCheck();
    }
});

to:

effect(() => {
    const modelValue = this.modelValue();

    const visibleOptions = this.visibleOptions();
    if (visibleOptions && ObjectUtils.isNotEmpty(visibleOptions)) {
        if (this.optionValue && this.optionLabel && modelValue) {
            this.selectedOptions = visibleOptions.filter((option) => modelValue.includes(option[this.optionLabel]) || modelValue.includes(option[this.optionValue]));
        } else {
            this.selectedOptions = modelValue;
        }
        this.cd.markForCheck();
    }
    else if (this.selectedOptions?.length > 0) {
        this.selectedOptions = [];
        this.cd.markForCheck();
    }
});

@mehmetcetin01140 mehmetcetin01140 added Type: Bug Issue contains a bug related to a specific component. Something about the component is not working and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels May 20, 2024
@mehmetcetin01140 mehmetcetin01140 added this to the 17.17.1 milestone May 20, 2024
@aliaksandrZh
Copy link

As a workaround you can patch the multiselect constructor locally https://github.com/primefaces/primeng/blob/master/src/app/components/multiselect/multiselect.ts#L1182

from:

effect(() => {
    const modelValue = this.modelValue();

    const visibleOptions = this.visibleOptions();
    if (visibleOptions && ObjectUtils.isNotEmpty(visibleOptions)) {
        if (this.optionValue && this.optionLabel && modelValue) {
            this.selectedOptions = visibleOptions.filter((option) => modelValue.includes(option[this.optionLabel]) || modelValue.includes(option[this.optionValue]));
        } else {
            this.selectedOptions = modelValue;
        }
        this.cd.markForCheck();
    }
});

to:

effect(() => {
    const modelValue = this.modelValue();

    const visibleOptions = this.visibleOptions();
    if (visibleOptions && ObjectUtils.isNotEmpty(visibleOptions)) {
        if (this.optionValue && this.optionLabel && modelValue) {
            this.selectedOptions = visibleOptions.filter((option) => modelValue.includes(option[this.optionLabel]) || modelValue.includes(option[this.optionValue]));
        } else {
            this.selectedOptions = modelValue;
        }
        this.cd.markForCheck();
    }
    else if (this.selectedOptions?.length > 0) {
        this.selectedOptions = [];
        this.cd.markForCheck();
    }
});

In this approach we clear values shown here
Снимок экрана 2024-05-21 в 10 36 37,
However, the internal state of the multi-select still retains its values. Shouldn't we clear the value of this.modelValue as well?

@capc0
Copy link
Contributor Author

capc0 commented May 21, 2024

As far as I understand the code here, the effect runs whenever modelValue changes. The code here only is intended to update selectedOptions based on the current modelValue.

@aliaksandrZh
Copy link

Since we have the.visibleOptions() in effect, changes to the visibleOptions trigger that code as well. In the StackBlitz example, the clear() method assigns an empty array to the options, which is why the code runs in this case.
Am I wrong?
Снимок экрана 2024-05-21 в 11 10 00

As I can see, once we click on clear(), only visibleOptions is changed.

@cetincakiroglu cetincakiroglu modified the milestones: 17.18.0, 18.Future May 30, 2024
@Xriuk
Copy link

Xriuk commented Sep 10, 2024

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Projects
Status: No status
Development

No branches or pull requests

6 participants