UNPKG

26.6 kBMarkdownView Raw
1 [![npm version](https://badge.fury.io/js/%40ng-select%2Fng-select.svg)](https://badge.fury.io/js/%40ng-select%2Fng-select)
2[![Coverage Status][coveralls-image]][coveralls-url]
3[![gzip bundle size](http://img.badgesize.io/https://unpkg.com/@ng-select/ng-select@latest/bundles/ng-select-ng-select.umd.min.js?compression=gzip&style=flat-square)][ng-select-url]
4[![ng-select channel on discord](https://img.shields.io/discord/873021904708059177.svg?style=flat-square)](https://discord.gg/ETyJTvKK)
5
6[coveralls-image]: https://coveralls.io/repos/github/ng-select/ng-select/badge.svg?branch=master
7[coveralls-url]: https://coveralls.io/github/ng-select/ng-select?branch=master
8[ng-select-url]: https://unpkg.com/@ng-select/ng-select@latest
9
10# Angular ng-select - Lightweight all in one UI Select, Multiselect and Autocomplete
11See [Demo](https://ng-select.github.io/ng-select) page.
12
13---
14
15## Versions
16
17| Angular | ng-select |
18|------------------|:---------:|
19| >=19.0.0 <20.0.0 | v14.x |
20| >=18.0.0 <19.0.0 | v13.x |
21| >=17.0.0 <18.0.0 | v12.x |
22| >=16.0.0 <17.0.0 | v11.x |
23| >=15.0.0 <16.0.0 | v10.x |
24| >=14.0.0 <15.0.0 | v9.x |
25| >=13.0.0 <14.0.0 | v8.x |
26| >=12.0.0 <13.0.0 | v7.x |
27| >=11.0.0 <12.0.0 | v6.x |
28| >=10.0.0 <11.0.0 | v5.x |
29| >=9.0.0 <10.0.0 | v4.x |
30| >=8.0.0 <9.0.0 | v3.x |
31| >=6.0.0 <8.0.0 | v2.x |
32| v5.x.x | v1.x |
33
34---
35## Browser Support
36`ng-select` supports all browsers supported by Angular. For current list, see https://angular.io/guide/browser-support#browser-support. This includes the following specific versions:
37```angular2html
38Chrome 2 most recent versions
39Firefox latest and extended support release (ESR)
40Edge 2 most recent major versions
41Safari 2 most recent major versions
42iOS 2 most recent major versions
43Android 2 most recent major versions
44```
45Table of contents
46=================
47
48 * [Features](#features)
49 * [Getting started](#getting-started)
50 * [API](#api)
51 * [Change detection](#change-detection)
52 * [Custom styles](#custom-styles)
53 * [Validation state](#validation-state)
54 * [Contributing](#contributing)
55 * [Development](#development)
56 * [Inspiration](#inspiration)
57
58## Features
59- [x] Custom binding to property or object
60- [x] Custom option, label, header and footer templates
61- [x] Virtual Scroll support with large data sets (>5000 items).
62- [x] Infinite scroll
63- [x] Keyboard navigation
64- [x] Multiselect
65- [x] Flexible autocomplete with client/server filtering
66- [x] Custom search
67- [x] Custom tags
68- [x] Append to
69- [x] Group items
70- [x] Output events
71- [x] Accessibility
72- [x] Good base functionality test coverage
73- [x] Themes
74
75## Warning
76Library is under active development and may have API breaking changes for subsequent major versions after 1.0.0.
77
78## Getting started
79### Step 1: Install `ng-select`:
80
81#### NPM
82```shell
83npm install --save @ng-select/ng-select
84```
85#### YARN
86```shell
87yarn add @ng-select/ng-select
88```
89### Step 2:
90
91#### Standalone: Import NgSelectComponent and other necessary directives directly:
92```typescript
93import { NgLabelTemplateDirective, NgOptionTemplateDirective, NgSelectComponent } from '@ng-select/ng-select';
94import { FormsModule } from '@angular/forms';
95
96@Component({
97 selector: 'example',
98 standalone: true,
99 template: './example.component.html',
100 styleUrl: './example.component.scss',
101 imports: [
102 NgLabelTemplateDirective,
103 NgOptionTemplateDirective,
104 NgSelectComponent,
105 ],
106})
107export class ExampleComponent {}
108```
109
110#### NgModule: Import the NgSelectModule and angular FormsModule module:
111```typescript
112import { NgSelectModule } from '@ng-select/ng-select';
113import { FormsModule } from '@angular/forms';
114
115@NgModule({
116 declarations: [AppComponent],
117 imports: [NgSelectModule, FormsModule],
118 bootstrap: [AppComponent]
119})
120export class AppModule {}
121```
122
123### Step 3: Include a theme:
124To allow customization and theming, `ng-select` bundle includes only generic styles that are necessary for correct layout and positioning. To get full look of the control, include one of the themes in your application. If you're using the Angular CLI, you can add this to your `styles.scss` or include it in `.angular-cli.json` (Angular v5 and below) or `angular.json` (Angular v6 onwards).
125
126```scss
127@import "~@ng-select/ng-select/themes/default.theme.css";
128// ... or
129@import "~@ng-select/ng-select/themes/material.theme.css";
130
131```
132
133
134### Step 4 (Optional): Configuration
135
136You can also set global configuration and localization messages by injecting NgSelectConfig service,
137typically in your root component, and customize the values of its properties in order to provide default values.
138
139```js
140 constructor(private config: NgSelectConfig) {
141 this.config.notFoundText = 'Custom not found';
142 this.config.appendTo = 'body';
143 // set the bindValue to global config when you use the same
144 // bindValue in most of the place.
145 // You can also override bindValue for the specified template
146 // by defining `bindValue` as property
147 // Eg : <ng-select bindValue="some-new-value"></ng-select>
148 this.config.bindValue = 'value';
149 }
150```
151
152### Usage
153Define options in your consuming component:
154```js
155@Component({...})
156export class ExampleComponent {
157
158 selectedCar: number;
159
160 cars = [
161 { id: 1, name: 'Volvo' },
162 { id: 2, name: 'Saab' },
163 { id: 3, name: 'Opel' },
164 { id: 4, name: 'Audi' },
165 ];
166}
167```
168In template use `ng-select` component with your options
169
170```html
171<!--Using ng-option and for loop-->
172<ng-select [(ngModel)]="selectedCar">
173 @for (car of cars; track car.id) {
174 <ng-option [value]="car.id">{{car.name}}</ng-option>
175 }
176</ng-select>
177
178<!--Using items input-->
179<ng-select [items]="cars"
180 bindLabel="name"
181 bindValue="id"
182 [(ngModel)]="selectedCar">
183</ng-select>
184```
185For more detailed examples see [Demo](https://ng-select.github.io/ng-select#/data-sources) page
186
187### SystemJS
188If you are using SystemJS, you should also adjust your configuration to point to the UMD bundle.
189
190In your systemjs config file, `map` needs to tell the System loader where to look for `ng-select`:
191```js
192map: {
193 '@ng-select/ng-select': 'node_modules/@ng-select/ng-select/bundles/ng-select.umd.js',
194}
195```
196
197## API
198### Inputs
199| Input | Type | Default | Required | Description |
200|-----------------------------|------------------------------------------------------|---------------------| ------------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
201| [addTag] | `boolean \| ((term: string) => any \| Promise<any>)` | `false` | no | Allows to create custom options. |
202| addTagText | `string` | `Add item` | no | Set custom text when using tagging |
203| appearance | `string` | `underline` | no | Allows to select dropdown appearance. Set to `outline` to add border instead of underline (applies only to Material theme) |
204| appendTo | `string` | null | no | Append dropdown to body or any other element using css selector. For correct positioning `body` should have `position:relative` |
205| bufferAmount | `number` | 4 | no | Used in virtual scrolling, the `bufferAmount` property controls the number of items preloaded in the background to ensure smoother and more seamless scrolling. |
206| bindValue | `string` | `-` | no | Object property to use for selected model. By default binds to whole object. |
207| bindLabel | `string` | `label` | no | Object property to use for label. Default `label` |
208| [closeOnSelect] | `boolean` | true | no | Whether to close the menu when a value is selected |
209| clearAllText | `string` | `Clear all` | no | Set custom text for clear all icon title |
210| [clearable] | `boolean` | `true` | no | Allow to clear selected value. Default `true` |
211| [clearOnBackspace] | `boolean` | `true` | no | Clear selected values one by one when clicking backspace. Default `true` |
212| [compareWith] | `(a: any, b: any) => boolean` | `(a, b) => a === b` | no | A function to compare the option values with the selected values. The first argument is a value from an option. The second is a value from the selection(model). A boolean should be returned. |
213| dropdownPosition | `bottom` \| `top` \| `auto` | `auto` | no | Set the dropdown position on open |
214| [fixedPlaceholder] | `boolean` | `false` | no | Set placeholder visible even when an item is selected |
215| [groupBy] | `string` \| `Function` | null | no | Allow to group items by key or function expression |
216| [groupValue] | `(groupKey: string, children: any[]) => Object` | - | no | Function expression to provide group value |
217| [selectableGroup] | `boolean` | false | no | Allow to select group when groupBy is used |
218| [selectableGroupAsModel] | `boolean` | true | no | Indicates whether to select all children or group itself |
219| [items] | `Array<any>` | `[]` | yes | Items array |
220| [loading] | `boolean` | `-` | no | You can set the loading state from the outside (e.g. async items loading) |
221| loadingText | `string` | `Loading...` | no | Set custom text when for loading items |
222| labelForId | `string` | `-` | no | Id to associate control with label. |
223| [markFirst] | `boolean` | `true` | no | Marks first item as focused when opening/filtering. |
224| [isOpen] | `boolean` | `-` | no | Allows manual control of dropdown opening and closing. `true` - won't close. `false` - won't open. |
225| maxSelectedItems | `number` | none | no | When multiple = true, allows to set a limit number of selection. |
226| [hideSelected] | `boolean` | `false` | no | Allows to hide selected items. |
227| [multiple] | `boolean` | `false` | no | Allows to select multiple items. |
228| notFoundText | `string` | `No items found` | no | Set custom text when filter returns empty result |
229| placeholder | `string` | `-` | no | Placeholder text. |
230| [searchable] | `boolean` | `true` | no | Allow to search for value. Default `true` |
231| [readonly] | `boolean` | `false` | no | Set ng-select as readonly. Mostly used with reactive forms. |
232| [searchFn] | `(term: string, item: any) => boolean` | `null` | no | Allow to filter by custom search function |
233| [searchWhileComposing] | `boolean` | `true` | no | Whether items should be filtered while composition started |
234| [trackByFn] | `(item: any) => any` | `null` | no | Provide custom trackBy function |
235| [clearSearchOnAdd] | `boolean` | `true` | no | Clears search input when item is selected. Default `true`. Default `false` when **closeOnSelect** is `false` |
236| [deselectOnClick] | `boolean` | `false` | no | Deselects a selected item when it is clicked in the dropdown. Default `false`. Default `true` when **multiple** is `true` |
237| [editableSearchTerm] | `boolean` | `false` | no | Allow to edit search query if option selected. Default `false`. Works only if multiple is `false`. |
238| [selectOnTab] | `boolean` | `false` | no | Select marked dropdown item using tab. Default `false` |
239| [openOnEnter] | `boolean` | `true` | no | Open dropdown using enter. Default `true` |
240| [typeahead] | `Subject` | `-` | no | Custom autocomplete or advanced filter. |
241| [minTermLength] | `number` | `0` | no | Minimum term length to start a search. Should be used with `typeahead` |
242| typeToSearchText | `string` | `Type to search` | no | Set custom text when using Typeahead |
243| [virtualScroll] | `boolean` | false | no | Enable virtual scroll for better performance when rendering a lot of data |
244| [inputAttrs] | `{ [key: string]: string }` | `-` | no | Pass custom attributes to underlying `input` element |
245| [tabIndex] | `number` | `-` | no | Set tabindex on ng-select |
246| [preventToggleOnRightClick] | `boolean` | `false` | no | Prevent opening of ng-select on right mouse click |
247| [keyDownFn] | `($event: KeyboardEvent) => bool` | `true` | no | Provide custom keyDown function. Executed before default handler. Return false to suppress execution of default key down handlers |
248
249### Outputs
250
251| Output | Description |
252| ------------- | ------------- |
253| (add) | Fired when item is added while `[multiple]="true"`. Outputs added item |
254| (blur) | Fired on select blur |
255| (change) | Fired on model change. Outputs whole model |
256| (close) | Fired on select dropdown close |
257| (clear) | Fired on clear icon click |
258| (focus) | Fired on select focus |
259| (search) | Fired while typing search term. Outputs search term with filtered items |
260| (open) | Fired on select dropdown open |
261| (remove) | Fired when item is removed while `[multiple]="true"` |
262| (scroll) | Fired when scrolled. Provides the start and end index of the currently available items. Can be used for loading more items in chunks before the user has scrolled all the way to the bottom of the list. |
263| (scrollToEnd) | Fired when scrolled to the end of items. Can be used for loading more items in chunks. |
264
265
266### Methods
267 Name | Description |
268| ------------- | ------------- |
269| open | Opens the select dropdown panel |
270| close | Closes the select dropdown panel |
271| focus | Focuses the select element |
272| blur | Blurs the select element |
273
274### Other
275 Name | Type | Description |
276| ------------- | ------------- | ------------- |
277| [ngOptionHighlight] | directive | Highlights search term in option. Accepts search term. Should be used on option element. [README](https://github.com/ng-select/ng-select/blob/master/src/ng-option-highlight/README.md) |
278| NgSelectConfig | configuration | Configuration provider for the NgSelect component. You can inject this service and provide application wide configuration. |
279| SELECTION_MODEL_FACTORY | service | DI token for SelectionModel implementation. You can provide custom implementation changing selection behaviour. |
280
281## Custom selection logic
282Ng-select allows to provide custom selection implementation using `SELECTION_MODEL_FACTORY`. To override [default](https://github.com/ng-select/ng-select/blob/master/src/ng-select/lib/selection-model.ts) logic provide your factory method in your angular module.
283
284```javascript
285// app.module.ts
286providers: [
287 { provide: SELECTION_MODEL_FACTORY, useValue: <SelectionModelFactory>CustomSelectionFactory }
288]
289
290// selection-model.ts
291export function CustomSelectionFactory() {
292 return new CustomSelectionModel();
293}
294
295export class CustomSelectionModel implements SelectionModel {
296 ...
297}
298```
299
300## Change Detection
301Ng-select component implements `OnPush` change detection which means the dirty checking checks for immutable
302data types. That means if you do object mutations like:
303
304```javascript
305this.items.push({id: 1, name: 'New item'})
306```
307
308Component will not detect a change. Instead you need to do:
309
310```javascript
311this.items = [...this.items, {id: 1, name: 'New item'}];
312```
313
314This will cause the component to detect the change and update. Some might have concerns that
315this is a pricey operation, however, it is much more performant than running `ngDoCheck` and
316constantly diffing the array.
317
318## Custom styles
319If you are not happy with default styles you can easily override them with increased selector specificity or creating your own theme. This applies if you are using no `ViewEncapsulation` or adding styles to global stylesheet. E.g.
320
321```html
322<ng-select class="custom"></ng-select>
323```
324
325```css
326.ng-select.custom {
327 border:0px;
328 min-height: 0px;
329 border-radius: 0;
330}
331.ng-select.custom .ng-select-container {
332 min-height: 0px;
333 border-radius: 0;
334}
335```
336
337If you are using `ViewEncapsulation`, you could use special `::ng-deep` selector which will prevent scoping for nested selectors altough this is more of a workaround and we recommend using solution described above.
338
339```css
340.ng-select.custom ::ng-deep .ng-select-container {
341 min-height: 0px;
342 border-radius: 0;
343}
344```
345WARNING: Keep in mind that ng-deep is deprecated and there is no alternative to it yet. See [Here](https://github.com/angular/angular/issues/17867).
346
347### Validation state
348By default when you use reactive forms validators or template driven forms validators css class `ng-invalid` will be applied on ng-select. You can show errors state by adding custom css style
349
350```css
351ng-select.ng-invalid.ng-touched .ng-select-container {
352 border-color: #dc3545;
353 box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 0 3px #fde6e8;
354}
355```
356
357## Contributing
358
359Contributions are welcome. You can start by looking at [issues](https://github.com/ng-select/ng-select/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22) with label *Help wanted* or creating new Issue with proposal or bug report.
360Note that we are using https://conventionalcommits.org/ commits format.
361
362## Development
363
364Perform the _clone-to-launch_ steps with these terminal commands.
365
366### Run demo page in watch mode
367```
368git clone https://github.com/ng-select/ng-select
369cd ng-select
370yarn
371yarn run start
372```
373### Testing
374```
375yarn run test
376or
377yarn run test:watch
378```
379
380### Release
381
382To release to npm just run `./release.sh`, of course if you have permissions ;)
383
384## Inspiration
385This component is inspired by [React select](https://github.com/JedWatson/react-select) and [Virtual scroll](https://github.com/rintoj/angular2-virtual-scroll). Check theirs amazing work and components :)
386