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

Add feature: Disable single option within ng-select #1046

Closed
gongAll opened this issue Feb 6, 2019 · 7 comments
Closed

Add feature: Disable single option within ng-select #1046

gongAll opened this issue Feb 6, 2019 · 7 comments

Comments

@gongAll
Copy link

gongAll commented Feb 6, 2019

I have an ng-select element where I would like to be able to disable the selection of a given option, whenever I want.

<ng-select [items]="comboboxElements"
           [bindValue]="comboboxElements['value']"
           dropdownPosition="bottom"
           virtualScroll="true"
           [ngModel]="active"
           (ngModelChange)="activeChange.emit($event)"
           (change)="onChange($event)">

    <ng-template ng-label-tmp
                 let-item="item">
        {{item.labelKey | translate}}
    </ng-template>

    <ng-template ng-option-tmp
                 let-item="item">
        {{ item.labelKey | translate }}
    </ng-template>
</ng-select>

I believe this feature would make sense to be available inside the ng-option-tmp, as so:

    <ng-template ng-option-tmp
                 [disabled]="item.disabled"
                 let-item="item">
        {{ item.labelKey | translate }}
    </ng-template>
@gongAll
Copy link
Author

gongAll commented Feb 8, 2019

Sorry for the late update.

For whoever may concern, all I had to do on the .ts file was:

this.comboboxElements[0].disabled = true;

I believe this property should be documented, as I believe it is not very clear it exists.

@gongAll gongAll closed this as completed Feb 8, 2019
@vladyour
Copy link

Hi @gongAll,
Are you sure if it works?
When I code <ng-template ng-option-tmp [disabled]="itemIsDisabled(item)" ..., I see error in console:
Uncaught Error: Template parse errors: Can't bind to 'disabled' since it isn't a known property of 'ng-template'.

@gongAll
Copy link
Author

gongAll commented Mar 22, 2019

Instead, add a 'disabled: boolean' attribute to the object you're using to fill the table, as such:
html

<ng-select [items]="someRandomItems">
</ng-select>

ts

...
public someRandomItems: SomeRandomModel[] = [];
...
export class SomeRandomModel {
  ... your attributes ...
  public disabled = false;
}

@vladyour
Copy link

Wow! It works! Thank you!

@FarizF
Copy link

FarizF commented Sep 18, 2020

Is this also possible with the 2.20.0 version of ng-select? I'm trying to assign the attribute to the ng-option element/component, but can't get the option to be disabled. I've also tried adding the attribute to the model, without success.

@loverzpark
Copy link

[disable] is not working for directive ng-option-tmp

@gongAll
Copy link
Author

gongAll commented Apr 7, 2021

@loverzpark maybe read the entire conversation? It's not a bindable attribute, it's an attribute inside each element of the list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants