- <div style="width: 300px; margin-bottom: 20px;">
- <h3>Select a single city</h3>
- <ng-select [allowClear]="true"
- [items]="items"
- [disabled]="disabled"
- (data)="refreshValue($event)"
- (selected)="selected($event)"
- (removed)="removed($event)"
- (typed)="typed($event)"
- placeholder="No city selected">
- </ng-select>
- <p></p>
- <pre>{{value.text}}</pre>
- <div>
- <button type="button" class="btn btn-primary"
- [(ngModel)]="disabledV" btnCheckbox
- btnCheckboxTrue="1" btnCheckboxFalse="0">
- {{disabled === '1' ? 'Enable' : 'Disable'}}
- </button>
- </div>
- </div>
- import { Component } from '@angular/core';
- @Component({
- selector: 'single-demo',
- templateUrl: './single-demo.html'
- })
- export class SingleDemoComponent {
- public items:Array<string> = ['Amsterdam', 'Antwerp', 'Athens', 'Barcelona',
- 'Berlin', 'Birmingham', 'Bradford', 'Bremen', 'Brussels', 'Bucharest',
- 'Budapest', 'Cologne', 'Copenhagen', 'Dortmund', 'Dresden', 'Dublin',
- 'Düsseldorf', 'Essen', 'Frankfurt', 'Genoa', 'Glasgow', 'Gothenburg',
- 'Hamburg', 'Hannover', 'Helsinki', 'Kraków', 'Leeds', 'Leipzig', 'Lisbon',
- 'London', 'Madrid', 'Manchester', 'Marseille', 'Milan', 'Munich', 'Málaga',
- 'Naples', 'Palermo', 'Paris', 'Poznań', 'Prague', 'Riga', 'Rome',
- 'Rotterdam', 'Seville', 'Sheffield', 'Sofia', 'Stockholm', 'Stuttgart',
- 'The Hague', 'Turin', 'Valencia', 'Vienna', 'Vilnius', 'Warsaw', 'Wrocław',
- 'Zagreb', 'Zaragoza', 'Łódź'];
- private value:any = {};
- private _disabledV:string = '0';
- private disabled:boolean = false;
- private get disabledV():string {
- return this._disabledV;
- }
- private set disabledV(value:string) {
- this._disabledV = value;
- this.disabled = this._disabledV === '1';
- }
- public selected(value:any):void {
- console.log('Selected value is: ', value);
- }
- public removed(value:any):void {
- console.log('Removed value is: ', value);
- }
- public typed(value:any):void {
- console.log('New search input: ', value);
- }
- public refreshValue(value:any):void {
- this.value = value;
- }
- }
- <div style="width: 300px; margin-bottom: 20px;">
- <h3>Select multiple cities</h3>
- <ng-select [multiple]="true"
- [items]="items"
- [disabled]="disabled"
- (data)="refreshValue($event)"
- (selected)="selected($event)"
- (removed)="removed($event)"
- placeholder="No city selected"></ng-select>
- <pre>{{itemsToString(value)}}</pre>
- <div>
- <button type="button" class="btn btn-primary"
- [(ngModel)]="disabledV" btnCheckbox
- btnCheckboxTrue="1" btnCheckboxFalse="0">
- {{disabled === '1' ? 'Enable' : 'Disable'}}
- </button>
- </div>
- </div>
- import { Component } from '@angular/core';
- @Component({
- selector: 'multiple-demo',
- templateUrl: './multiple-demo.html'
- })
- export class MultipleDemoComponent {
- public items:Array<string> = ['Amsterdam', 'Antwerp', 'Athens', 'Barcelona',
- 'Berlin', 'Birmingham', 'Bradford', 'Bremen', 'Brussels', 'Bucharest',
- 'Budapest', 'Cologne', 'Copenhagen', 'Dortmund', 'Dresden', 'Dublin', 'Düsseldorf',
- 'Essen', 'Frankfurt', 'Genoa', 'Glasgow', 'Gothenburg', 'Hamburg', 'Hannover',
- 'Helsinki', 'Leeds', 'Leipzig', 'Lisbon', 'Łódź', 'London', 'Kraków', 'Madrid',
- 'Málaga', 'Manchester', 'Marseille', 'Milan', 'Munich', 'Naples', 'Palermo',
- 'Paris', 'Poznań', 'Prague', 'Riga', 'Rome', 'Rotterdam', 'Seville', 'Sheffield',
- 'Sofia', 'Stockholm', 'Stuttgart', 'The Hague', 'Turin', 'Valencia', 'Vienna',
- 'Vilnius', 'Warsaw', 'Wrocław', 'Zagreb', 'Zaragoza'];
- private value:any = ['Athens'];
- private _disabledV:string = '0';
- private disabled:boolean = false;
- private get disabledV():string {
- return this._disabledV;
- }
- private set disabledV(value:string) {
- this._disabledV = value;
- this.disabled = this._disabledV === '1';
- }
- public selected(value:any):void {
- console.log('Selected value is: ', value);
- }
- public removed(value:any):void {
- console.log('Removed value is: ', value);
- }
- public refreshValue(value:any):void {
- this.value = value;
- }
- public itemsToString(value:Array<any> = []):string {
- return value
- .map((item:any) => {
- return item.text;
- }).join(',');
- }
- }
- <div class="selectivity-input example-input"
- style="width: 300px; margin-bottom: 20px;">
- <h3>Select a city by country</h3>
- <ng-select [allowClear]="true"
- [items]="items"
- [disabled]="disabled"
- (data)="refreshValue($event)"
- (selected)="selected($event)"
- (removed)="removed($event)"
- placeholder="No city selected"></ng-select>
- <p></p>
- <pre>{{value.text}}</pre>
- <div>
- <button type="button" class="btn btn-primary"
- [(ngModel)]="disabledV" btnCheckbox
- btnCheckboxTrue="1" btnCheckboxFalse="0">
- {{disabled === '1' ? 'Enable' : 'Disable'}}
- </button>
- </div>
- </div>
- import { Component } from '@angular/core';
- @Component({
- selector: 'children-demo',
- templateUrl: './children-demo.html'
- })
- export class ChildrenDemoComponent {
- public items:Array<any> = [
- {
- text: 'Austria',
- children: [
- {id: 54, text: 'Vienna'}
- ]
- },
- {
- text: 'Belgium',
- children: [
- {id: 2, text: 'Antwerp'},
- {id: 9, text: 'Brussels'}
- ]
- },
- {
- text: 'Bulgaria',
- children: [
- {id: 48, text: 'Sofia'}
- ]
- },
- {
- text: 'Croatia',
- children: [
- {id: 58, text: 'Zagreb'}
- ]
- },
- {
- text: 'Czech Republic',
- children: [
- {id: 42, text: 'Prague'}
- ]
- },
- {
- text: 'Denmark',
- children: [
- {id: 13, text: 'Copenhagen'}
- ]
- },
- {
- text: 'England',
- children: [
- {id: 6, text: 'Birmingham'},
- {id: 7, text: 'Bradford'},
- {id: 26, text: 'Leeds'},
- {id: 30, text: 'London'},
- {id: 34, text: 'Manchester'},
- {id: 47, text: 'Sheffield'}
- ]
- },
- {
- text: 'Finland',
- children: [
- {id: 25, text: 'Helsinki'}
- ]
- },
- {
- text: 'France',
- children: [
- {id: 35, text: 'Marseille'},
- {id: 40, text: 'Paris'}
- ]
- },
- {
- text: 'Germany',
- children: [
- {id: 5, text: 'Berlin'},
- {id: 8, text: 'Bremen'},
- {id: 12, text: 'Cologne'},
- {id: 14, text: 'Dortmund'},
- {id: 15, text: 'Dresden'},
- {id: 17, text: 'Düsseldorf'},
- {id: 18, text: 'Essen'},
- {id: 19, text: 'Frankfurt'},
- {id: 23, text: 'Hamburg'},
- {id: 24, text: 'Hannover'},
- {id: 27, text: 'Leipzig'},
- {id: 37, text: 'Munich'},
- {id: 50, text: 'Stuttgart'}
- ]
- },
- {
- text: 'Greece',
- children: [
- {id: 3, text: 'Athens'}
- ]
- },
- {
- text: 'Hungary',
- children: [
- {id: 11, text: 'Budapest'}
- ]
- },
- {
- text: 'Ireland',
- children: [
- {id: 16, text: 'Dublin'}
- ]
- },
- {
- text: 'Italy',
- children: [
- {id: 20, text: 'Genoa'},
- {id: 36, text: 'Milan'},
- {id: 38, text: 'Naples'},
- {id: 39, text: 'Palermo'},
- {id: 44, text: 'Rome'},
- {id: 52, text: 'Turin'}
- ]
- },
- {
- text: 'Latvia',
- children: [
- {id: 43, text: 'Riga'}
- ]
- },
- {
- text: 'Lithuania',
- children: [
- {id: 55, text: 'Vilnius'}
- ]
- },
- {
- text: 'Netherlands',
- children: [
- {id: 1, text: 'Amsterdam'},
- {id: 45, text: 'Rotterdam'},
- {id: 51, text: 'The Hague'}
- ]
- },
- {
- text: 'Poland',
- children: [
- {id: 29, text: 'Łódź'},
- {id: 31, text: 'Kraków'},
- {id: 41, text: 'Poznań'},
- {id: 56, text: 'Warsaw'},
- {id: 57, text: 'Wrocław'}
- ]
- },
- {
- text: 'Portugal',
- children: [
- {id: 28, text: 'Lisbon'}
- ]
- },
- {
- text: 'Romania',
- children: [
- {id: 10, text: 'Bucharest'}
- ]
- },
- {
- text: 'Scotland',
- children: [
- {id: 21, text: 'Glasgow'}
- ]
- },
- {
- text: 'Spain',
- children: [
- {id: 4, text: 'Barcelona'},
- {id: 32, text: 'Madrid'},
- {id: 33, text: 'Málaga'},
- {id: 46, text: 'Seville'},
- {id: 53, text: 'Valencia'},
- {id: 59, text: 'Zaragoza'}
- ]
- },
- {
- text: 'Sweden',
- children: [
- {id: 22, text: 'Gothenburg'},
- {id: 49, text: 'Stockholm'}
- ]
- }
- ];
- private value:any = {};
- private _disabledV:string = '0';
- private disabled:boolean = false;
- private get disabledV():string {
- return this._disabledV;
- }
- private set disabledV(value:string) {
- this._disabledV = value;
- this.disabled = this._disabledV === '1';
- }
- public selected(value:any):void {
- console.log('Selected value is: ', value);
- }
- public removed(value:any):void {
- console.log('Removed value is: ', value);
- }
- public refreshValue(value:any):void {
- this.value = value;
- }
- }
- <div style="width: 300px; margin-bottom: 20px;">
- <h3>Select a color</h3>
- <ng-select [allowClear]="true"
- [items]="items"
- [disabled]="disabled"
- (data)="refreshValue($event)"
- (selected)="selected($event)"
- (removed)="removed($event)"
- (typed)="typed($event)"
- placeholder="No color selected">
- </ng-select>
- <p></p>
- <pre>{{value.text}}</pre>
- <div>
- <button type="button" class="btn btn-primary"
- [(ngModel)]="disabledV" btnCheckbox
- btnCheckboxTrue="1" btnCheckboxFalse="0">
- {{disabled === '1' ? 'Enable' : 'Disable'}}
- </button>
- </div>
- </div>
- import { Component, OnInit, ViewEncapsulation } from '@angular/core';
- const COLORS = [
- {'name': 'Blue 10', 'hex': '#C0E6FF'},
- {'name': 'Blue 20', 'hex': '#7CC7FF'},
- {'name': 'Blue 30', 'hex': '#5AAAFA'},
- {'name': 'Blue 40', 'hex': '#5596E6'},
- {'name': 'Blue 50', 'hex': '#4178BE'},
- {'name': 'Blue 60', 'hex': '#325C80'},
- {'name': 'Blue 70', 'hex': '#264A60'},
- {'name': 'Blue 80', 'hex': '#1D3649'},
- {'name': 'Blue 90', 'hex': '#152935'},
- {'name': 'Blue 100', 'hex': '#010205'},
- {'name': 'Green 10', 'hex': '#C8F08F'},
- {'name': 'Green 20', 'hex': '#B4E051'},
- {'name': 'Green 30', 'hex': '#8CD211'},
- {'name': 'Green 40', 'hex': '#5AA700'},
- {'name': 'Green 50', 'hex': '#4B8400'},
- {'name': 'Green 60', 'hex': '#2D660A'},
- {'name': 'Green 70', 'hex': '#144D14'},
- {'name': 'Green 80', 'hex': '#0A3C02'},
- {'name': 'Green 90', 'hex': '#0C2808'},
- {'name': 'Green 100', 'hex': '#010200'},
- {'name': 'Red 10', 'hex': '#FFD2DD'},
- {'name': 'Red 20', 'hex': '#FFA5B4'},
- {'name': 'Red 30', 'hex': '#FF7D87'},
- {'name': 'Red 40', 'hex': '#FF5050'},
- {'name': 'Red 50', 'hex': '#E71D32'},
- {'name': 'Red 60', 'hex': '#AD1625'},
- {'name': 'Red 70', 'hex': '#8C101C'},
- {'name': 'Red 80', 'hex': '#6E0A1E'},
- {'name': 'Red 90', 'hex': '#4C0A17'},
- {'name': 'Red 100', 'hex': '#040001'},
- {'name': 'Yellow 10', 'hex': '#FDE876'},
- {'name': 'Yellow 20', 'hex': '#FDD600'},
- {'name': 'Yellow 30', 'hex': '#EFC100'},
- {'name': 'Yellow 40', 'hex': '#BE9B00'},
- {'name': 'Yellow 50', 'hex': '#8C7300'},
- {'name': 'Yellow 60', 'hex': '#735F00'},
- {'name': 'Yellow 70', 'hex': '#574A00'},
- {'name': 'Yellow 80', 'hex': '#3C3200'},
- {'name': 'Yellow 90', 'hex': '#281E00'},
- {'name': 'Yellow 100', 'hex': '#020100'}
- ];
- @Component({
- selector: 'rich-demo',
- templateUrl: './rich-demo.html',
- styles: [`colorbox,.colorbox { display:inline-block; height:14px; width:14px;margin-right:4px; border:1px solid #000;}`],
- encapsulation: ViewEncapsulation.None // Enable dynamic HTML styles
- })
- export class RichDemoComponent implements OnInit {
- private value:any = {};
- private _disabledV:string = '0';
- private disabled:boolean = false;
- private items:Array<any> = [];
- public ngOnInit():any {
- COLORS.forEach((color:{name:string, hex:string}) => {
- this.items.push({
- id: color.hex,
- text: `<colorbox style='background-color:${color.hex};'></colorbox>${color.name} (${color.hex})`
- });
- });
- }
- private get disabledV():string {
- return this._disabledV;
- }
- private set disabledV(value:string) {
- this._disabledV = value;
- this.disabled = this._disabledV === '1';
- }
- public selected(value:any):void {
- console.log('Selected value is: ', value);
- }
- public removed(value:any):void {
- console.log('Removed value is: ', value);
- }
- public typed(value:any):void {
- console.log('New search input: ', value);
- }
- public refreshValue(value:any):void {
- this.value = value;
- }
- }
import {SelectModule} from 'ng2-select';
// class Select
@Component({
selector: 'ng-select',
properties: [
'allowClear',
'placeholder',
'items',
'multiple',
'showSearchInputInDropdown']
})
items
- (Array<any>
) - Array of items from which to select. 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 children
property to any item, whose value should be another array of items. Items that have children may omit having an ID. If items
are specified, all items are expected to be available locally and all selection operations operate on this local array only. If omitted, items are not available locally, and the query
option should be provided to fetch data.active
(?Array<any>
) - Initial selection data to set. This should be an object with id
and text
properties in the case of input type 'Single', or an array of such objects otherwise. This option is mutually exclusive with value.allowClear
(?boolean=false
) (not yet supported) - Set to true
to allow the selection to be cleared. This option only applies to single-value inputs.placeholder
(?string=''
) - Placeholder text to display when the element has no focus and selected items.disabled
(?boolean=false
) - When true
, it specifies that the component should be disabled.multiple
- (?boolean=false
) - Mode of this component. If set true
user can select more than one option. This option only applies to single-value inputs, as multiple-value inputs don't have the search input in the dropdown to begin with.data
- it fires during all events of this component; returns Array<any>
- current selected dataselected
- it fires after a new option selected; returns object with id
and text
properties that describes a new option.removed
- it fires after an option removed; returns object with id
and text
properties that describes a removed option.typed
- it fires after changing of search input; returns string
with that value.