How to make ng-select remove-and-read-only
Asked Answered
B

6

8

In my Angular application, I have an instance of the ng-select widget:

enter image description here

If you click on it, by default you can search for items and add more of them to the current selection:

enter image description here

I would like to change this default behaviour, in particular:

  • it shouldn not be possible to add new elements
  • clicking on it should not open the search bar or the items list
  • it should not show the arrow down icon (displayed by default on the right)
  • it should not show the X icon to remove all selection at once (displayed by default on the right)

So this is how I would like it to be:

enter image description here

Biotechnology answered 13/9, 2018 at 14:53 Comment(0)
B
11

In order to achieve this, we first need to create 3 css classes.

One to disable the arrow-down icon:

.ng-select.disable-arrow .ng-arrow-wrapper .ng-arrow {
  display: none;
}

One to disable the search/list dropdown:

.ng-select.disable-dropdown ng-dropdown-panel {
  display: none;
}

One to disable the clear-all X icon:

.ng-select.disable-clear-all .ng-clear-wrapper {
  display: none;
}

Then we add the ng-select element using the 3 css classes that we created, plus a few options:

<ng-select
  class="disable-arrow disable-dropdown disable-clear-all"
  [searchable]="false"
  [clearable]="true"
  [multiple]="true"
>
</ng-select>
Biotechnology answered 13/9, 2018 at 14:53 Comment(1)
Thanks for the answer, one minor thing: When this is selected now the corner radii change from rounded so I added this code to prevent that .ng-select.ng-select-opened.ng-select-bottom > .ng-select-container { border-bottom-right-radius: 4px !important; border-bottom-left-radius: 4px !important; }Oneiromancy
K
3

To disable the clear-all X icon add [clearable]="false" attribute to your ng-select tab ex.:

<ng-select class="disable-clear-all" [clearable]="false" [searchable]="false">
</ng-select>
Kamila answered 16/1, 2019 at 14:48 Comment(0)
W
2

If someone is looking to do that simply in 2023 you just have to add readonly to true to the ng-select element:

[readonly]="true"

cf documentation : https://github.com/ng-select/ng-select

Whereto answered 18/9, 2023 at 12:5 Comment(0)
H
0

To hide the clear and arrow icons:

.ng-clear-wrapper {display: none;}

.ng-arrow-wrapper {display: none;}
Halfhour answered 21/8, 2019 at 11:23 Comment(0)
O
0

Below is only what u need if u need it programmatically.

<ng-select
    [items]="people3"
    bindLabel="name"
    [disabled]="true"
    [multiple]="true"
    [(ngModel)]="selectedPeople3">
</ng-select>
Oman answered 22/1, 2021 at 15:11 Comment(0)
C
0

Nothing worked to me except this code

::ng-deep .ng-select .ng-arrow-wrapper {
    display: none !important;
  }
Camail answered 31/7 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.