首页 > 其他分享 >Angular4_支持多选,分组,自动完成,带图标,清理输入框可配置的select

Angular4_支持多选,分组,自动完成,带图标,清理输入框可配置的select

时间:2023-04-30 21:03:27浏览次数:50  
标签:__ Fired item text 输入框 Angular4 import select


Angular4_支持多选,分组,自动完成,过滤,带图标,清理输入框 可配置的select

效果图

Angular4_支持多选,分组,自动完成,带图标,清理输入框可配置的select_ci

Documentation

Usage

  1. 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
  1. 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 {
 }
  1. 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">
  1. Add the tag 

<ngx-select>

<ngx-select [items]="items" [(ngModel)]="itemId">
  1. 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 id and text properties. As convenience, you may also pass an array of strings, in which case the same string is used for both the ID and the text. Items may be nested by adding a options

optionValueField

string

'id'

Provide an opportunity to change the name an id property of objects in the items

optionTextField

string

'text'

Provide an opportunity to change the name a text property of objects in the items

optGroupLabelField

string

'label'

Provide an opportunity to change the name a label property of objects with an options property in the items

optGroupOptionsField

string

'options'

Provide an opportunity to change the name of an options property of objects in the items

[multiple]

boolean

false

Mode of this component. If set true

[allowClear]

boolean

false

Set to true

[placeholder]

string

''

Set to true

[noAutoComplete]

boolean

false

Set to true Set to true

[disabled]

boolean

false

When true, it specifies that the component should be disabled

[defaultValue]

any[]

[]

Use to set default value

autoSelectSingleOption

boolean

false

Auto select a non disabled single option

autoClearSearch

boolean

false

Auto clear a search text after select an option. Has effect for multiple = true

noResultsFound

string

'No results found'

The default text showed when a search has no results

size

'small'/'default'/'large'

'default'

Adding bootstrap classes: form-control-sm, input-sm, form-control-lg input-lg, btn-sm, btn-lg

searchCallback

(search: string, item: INgxSelectOption) => boolean

null

The callback function for custom filtering the select list

Output

Description

(typed)

Fired on changing search input. Returns string

(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: INgxOptionNavigated.

(selectionChanges)

Fired on change selected options. Returns: INgxSelectOption[].

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-selectngx-select_multiplengx-select__disabledngx-select__selectedngx-select__togglengx-select__placeholderngx-select__selected-singlengx-select__selected-pluralngx-select__allow-clearngx-select__toggle-buttonsngx-select__toggle-caretngx-select__clearngx-select__clear-iconngx-select__searchngx-select__choicesngx-select__item-groupngx-select__itemngx-select__item_disabledngx-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

  1.  - Customization rendering selected options. Representing variables:

option

  •  (implicit) - object of type 

INgxSelectOption

  • .

text

  •  - The text defined by the property 

optionTextField

  • .

indexngx-select-option

  1.  - 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.

indexsubIndexngx-select-option-not-found




标签:__,Fired,item,text,输入框,Angular4,import,select
From: https://blog.51cto.com/u_15976398/6238249

相关文章