Angular4_支持多选,分组,自动完成,过滤,带图标,清理输入框 可配置的select
效果图
Documentation
Usage
- Install ngx-select-ex through npm package manager using the following command:
npm i ngx-select-ex --save
For usage with Angular 4 install using the following command:
npm i ngx-select-ex@ng4 --save
- Add NgxSelectModule into your AppModule class. app.module.ts would look like this:
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {AppComponent} from './app.component';
import { NgxSelectModule } from 'ngx-select-ex';
@NgModule({
imports: [BrowserModule, NgxSelectModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {
}
If you want to change the default options then use next code:
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {AppComponent} from './app.component';
import { NgxSelectModule, INgxSelectOptions } from 'ngx-select-ex';
const CustomSelectOptions: INgxSelectOptions = { // Check the interface for more options
optionValueField: 'id',
optionTextField: 'name'
};
@NgModule({
imports: [BrowserModule, NgxSelectModule.forRoot(CustomSelectOptions)],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {
}
- Include Bootstrap styles. For example add to your index.html
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
- Add the tag
<ngx-select>
<ngx-select [items]="items" [(ngModel)]="itemId">
- More information regarding of using ngx-select-ex is located in demo.
API
Any item can be disabled
for prevent selection. For disable an item add the property disabled
Input | Type | Default | Description |
[items] | any[] |
| Items array. Should be an array of objects with |
optionValueField | string |
| Provide an opportunity to change the name an |
optionTextField | string |
| Provide an opportunity to change the name a |
optGroupLabelField | string |
| Provide an opportunity to change the name a |
optGroupOptionsField | string |
| Provide an opportunity to change the name of an |
[multiple] | boolean |
| Mode of this component. If set |
[allowClear] | boolean |
| Set to |
[placeholder] | string |
| Set to |
[noAutoComplete] | boolean |
| Set to |
[disabled] | boolean |
| When |
[defaultValue] | any[] |
| Use to set default value |
autoSelectSingleOption | boolean |
| Auto select a non disabled single option |
autoClearSearch | boolean |
| Auto clear a search text after select an option. Has effect for |
noResultsFound | string |
| The default text showed when a search has no results |
size |
|
| Adding bootstrap classes: form-control-sm, input-sm, form-control-lg input-lg, btn-sm, btn-lg |
searchCallback |
|
| The callback function for custom filtering the select list |
Output | Description |
(typed) | Fired on changing search input. Returns |
(focus) | Fired on select focus |
(blur) | Fired on select blur |
(open) | Fired on select dropdown open |
(close) | Fired on select dropdown close |
(select) | Fired on an item selected by user. Returns value of the selected item. |
(remove) | Fired on an item removed by user. Returns value of the removed item. |
(navigated) | Fired on navigate by the dropdown list. Returns: |
(selectionChanges) | Fired on change selected options. Returns: |
Warning! Although the component contains the select
and the remove
events, the better solution is using valueChanges
of the FormControl
.
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
@Component({
selector: 'app-example',
template: `<ngx-select [items]="['111', '222']" [formControl]="selectControl"></ngx-select>`
})
class ExampleComponent {
public selectControl = new FormControl();
constructor() {
this.selectControl.valueChanges.subscribe(value => console.log(value));
}
}
Styles and customization
Currently, the component contains CSS classes named within BEM Methodology. As well it contains the "Bootstrap classes". Recommended use BEM classes for style customization.
List of styles for customization:
ngx-select
ngx-select_multiple
ngx-select__disabled
ngx-select__selected
ngx-select__toggle
ngx-select__placeholder
ngx-select__selected-single
ngx-select__selected-plural
ngx-select__allow-clear
ngx-select__toggle-buttons
ngx-select__toggle-caret
ngx-select__clear
ngx-select__clear-icon
ngx-select__search
ngx-select__choices
ngx-select__item-group
ngx-select__item
ngx-select__item_disabled
ngx-select__item_active
Templates
For extended rendering customisation you are can use the ng-template
:
<ngx-select [items]="items" optionValueField="hex" optionTextField="name" [(ngModel)]="ngxValue">
<ng-template ngx-select-option-selected let-option let-text="text">
<span class="color-box" [style]="style('background-color:' + option.value)"></span>
<span [innerHtml]="text"></span>
({{option.data.hex}})
</ng-template>
<ng-template ngx-select-option let-option let-text="text">
<span class="color-box" [style]="style('background-color:' + option.value)"></span>
<span [innerHtml]="text"></span>
({{option.data.hex}})
</ng-template>
<ng-template ngx-select-option-not-found>
Nothing found
</ng-template>
</ngx-select>
Also, you are can mix directives for reducing template:
<ngx-select [items]="items" optionValueField="hex" optionTextField="name" [(ngModel)]="ngxValue">
<ng-template ngx-select-option-selected ngx-select-option let-option let-text="text">
<span class="color-box" [style]="style('background-color:' + option.value)"></span>
<span [innerHtml]="text"></span>
({{option.data.hex}})
</ng-template>
<ng-template ngx-select-option-not-found>
Not found <button (click)="addItem()">(+) Add new item</button>
</ng-template>
</ngx-select>
Description details of the directives:
ngx-select-option-selected
- - Customization rendering selected options. Representing variables:
option
- (implicit) - object of type
INgxSelectOption
- .
text
- - The text defined by the property
optionTextField
- .
index
ngx-select-option
- - Customization rendering options in the dropdown menu. Representing variables:
option
- (implicit) - object of type
INgxSelectOption
- .
text
- - The highlighted text defined by the property
optionTextField
- . It is highlighted in the search.
index
subIndex
ngx-select-option-not-found