Skip to content

Commit

Permalink
feat: selectable group (#325)
Browse files Browse the repository at this point in the history
fixes #282
  • Loading branch information
anjmao authored Mar 7, 2018
1 parent e358247 commit 32455b8
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 242 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ map: {
| clearAllText | `string` | `Clear all` | no | Set custom text for clear all icon title |
| dropdownPosition | `bottom`,`top`,`auto` | `bottom` | no | Set the dropdown position on open |
| [groupBy] | `string` | null | no | Allow to group items by key |
| [selectableGroup] | `boolean` | false | no | Allow to select group when groupBy is used |
| [items] | `Array<NgOption>` | `[]` | yes | Items array |
| loading | `boolean` | `-` | no | You can set the loading state from the outside (e.g. async items loading) |
| loadingText | `string` | `Loading...` | no | Set custom text when for loading items |
Expand Down
30 changes: 18 additions & 12 deletions demo/app/examples/groups.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,8 @@ import { Component, ChangeDetectionStrategy } from '@angular/core';
<p>
ng-select supports grouping flat array of objects by providing <b>groupBy</b> input
</p>
---js
const data = [{ name: 'Adam', email: '[email protected]', age: 12, country: 'United States' },
{ name: 'Samantha', email: '[email protected]', age: 30, country: 'United States' },
{ name: 'Amalie', email: '[email protected]', age: 12, country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21, country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21, country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30, country: 'Ecuador' },
{ name: 'Natasha', email: '[email protected]', age: 54, country: 'Ecuador' },
{ name: 'Nicole', email: '[email protected]', age: 43, country: 'Colombia' },
{ name: 'Michael', email: '[email protected]', age: 15, country: 'Colombia' },
{ name: 'Nicolás', email: '[email protected]', age: 43, country: 'Colombia' }];
---
<label>Default</label>
---html,true
<ng-select [items]="accounts"
bindLabel="name"
Expand All @@ -32,6 +21,20 @@ import { Component, ChangeDetectionStrategy } from '@angular/core';
<p>
<small>Selected: {{selectedAccount | json}}</small>
</p>
<hr />
<label>With selectable groups</label>
---html,true
<ng-select [items]="accounts2"
bindLabel="name"
groupBy="country"
[selectableGroup]="true"
[(ngModel)]="selectedAccount2">
</ng-select>
---
<p>
<small>Selected: {{selectedAccount2 | json}}</small>
</p>
`
})
export class SelectGroupsComponent {
Expand All @@ -49,6 +52,9 @@ export class SelectGroupsComponent {
{ name: 'Nicolás', email: '[email protected]', age: 43, country: 'Colombia' }
];

accounts2 = this.accounts.slice();
selectedAccount2 = this.accounts2[1];

ngOnInit() {

}
Expand Down
3 changes: 2 additions & 1 deletion demo/app/examples/reactive-forms.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NgOption } from '@ng-select/ng-select';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { DataService } from '../shared/data.service';
import { NgSelectComponent } from '../../../src/ng-select/ng-select.component';
import { delay } from 'rxjs/operators';

@Component({
selector: 'reactive-forms',
Expand Down Expand Up @@ -256,7 +257,7 @@ export class ReactiveFormsComponent {
}

private loadAlbums() {
this.dataService.getAlbums().subscribe(albums => {
this.dataService.getAlbums().pipe(delay(500)).subscribe(albums => {
this.allAlbums = albums;
this.albums = [...this.allAlbums];
this.selectFirstAlbum();
Expand Down
4 changes: 1 addition & 3 deletions demo/app/examples/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ export class SelectSearchComponent {

searchTerm = new EventEmitter<string>();
peopleTypeahead = new EventEmitter<string>();
selectedPersons = [{
name: 'Karyn Wright'
}];
selectedPersons = [{name: 'Karyn Wright'}, {name: 'Other'}];
selectedPerson: any;
selectedCustom: any;

Expand Down
9 changes: 7 additions & 2 deletions src/ng-select/items-list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NgOption } from './ng-select.types';
import * as searchHelper from './search-helper';
import { NgSelectComponent } from './ng-select.component';
import { isObject } from './utils';

export class ItemsList {

Expand Down Expand Up @@ -157,7 +158,7 @@ export class ItemsList {
}

resolveNested(option: any, key: string): any {
if (!(typeof option === 'object')) {
if (!isObject(option)) {
return option;
}
if (key.indexOf('.') === -1) {
Expand Down Expand Up @@ -185,6 +186,10 @@ export class ItemsList {
};
}

updateSelectedItem(item: NgOption, index: number) {
this._selected[index] = item;
}

private _getNextItemIndex(steps: number) {
if (steps > 0) {
return (this._markedIndex === this._filteredItems.length - 1) ? 0 : (this._markedIndex + 1);
Expand Down Expand Up @@ -224,7 +229,7 @@ export class ItemsList {
label: key,
hasChildren: true,
index: i,
disabled: true
disabled: !this._ngSelect.selectableGroup
};
parent.value = {};
parent.value[this._ngSelect.groupBy] = key;
Expand Down
Loading

0 comments on commit 32455b8

Please sign in to comment.