Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit dd383b6

Browse files
author
SamGraber
committed
Added support for specifying a null option for the select dropdown. When a null option is specified, add a config object to the start of the options array. The code will recognize this object and use the nullOption string as its display name, and set ngModel to null when it's selected. This isn't a pretty solution, but it's the only thing I could come up with due to the fact that ui-select doesn't support null-options yet.
1 parent 8b51947 commit dd383b6

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<div ng-controller="TestController as test">
2222
Selection: {{test.selection}}
2323
<rl-select ng-model="test.selection" options="test.options" option-as="item"
24-
selector="'name'" label="Test selector"></rl-select>
24+
selector="'name'" label="Test selector" null-option="None"></rl-select>
2525
<rl-tabset>
2626
<rl-tab>
2727
<rl-tab-header>Header</rl-tab-header>

source/components/select/select.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as _ from 'lodash';
1010

1111
import { services } from 'typescript-angular-utilities';
1212
import __validation = services.validation;
13+
import __object = services.object;
1314

1415
import {
1516
IComponentValidator,
@@ -30,24 +31,34 @@ export class SelectController {
3031
validator: __validation.IValidationHandler;
3132
label: string;
3233
ngDisabled: boolean;
34+
nullOption: string;
3335

3436
ngModel: angular.INgModelController;
3537
selectValidator: IComponentValidator;
3638
loading: boolean;
3739

40+
private _nullOption: any = {
41+
__isNullOption: true,
42+
};
43+
3844
get selection(): any {
3945
return this.ngModel.$viewValue;
4046
}
4147

4248
set selection(value: any) {
43-
this.ngModel.$setViewValue(value);
49+
if (value.__isNullOption) {
50+
this.ngModel.$setViewValue(null);
51+
} else {
52+
this.ngModel.$setViewValue(value);
53+
}
4454
}
4555

46-
static $inject: string[] = ['$element', '$scope', '$q', componentValidatorFactoryName];
56+
static $inject: string[] = ['$element', '$scope', '$q', componentValidatorFactoryName, __object.serviceName];
4757
constructor($element: angular.IAugmentedJQuery
4858
, $scope: angular.IScope
4959
, private $q: angular.IQService
50-
, componentValidatorFactory: IComponentValidatorFactory) {
60+
, componentValidatorFactory: IComponentValidatorFactory
61+
, private object: __object.IObjectUtility) {
5162
this.ngModel = $element.controller('ngModel');
5263

5364
if (_.isUndefined(this.options)) {
@@ -56,6 +67,8 @@ export class SelectController {
5667
this.options = options;
5768
this.loading = false;
5869
});
70+
} else {
71+
this.options = this.configureOptions(this.options);
5972
}
6073

6174
if (!_.isUndefined(this.validator)) {
@@ -72,17 +85,31 @@ export class SelectController {
7285
return null;
7386
}
7487

88+
if (item.__isNullOption) {
89+
return this.nullOption;
90+
}
91+
7592
return _.isFunction(this.selector)
7693
? (<{ (item: any): string }>this.selector)(item)
7794
: item[<string>this.selector];
7895
}
7996

8097
loadItems(): angular.IPromise<any[]> {
98+
let promise: angular.IPromise<any[]>;
8199
if (_.isFunction(this.getOptions)) {
82-
return this.getOptions();
100+
promise = this.getOptions();
83101
} else {
84-
return this.$q.when(this.options);
102+
promise = this.$q.when(this.options);
85103
}
104+
return promise.then((options: any[]): any[] => { return this.configureOptions(options); });
105+
}
106+
107+
configureOptions(options: any[]): any[] {
108+
if (!this.object.isNullOrWhitespace(this.nullOption)) {
109+
options.unshift(this._nullOption);
110+
}
111+
112+
return options;
86113
}
87114
}
88115

@@ -101,6 +128,7 @@ export function select(): angular.IDirective {
101128
validator: '=',
102129
label: '@',
103130
ngDisabled: '=',
131+
nullOption: '@',
104132
},
105133
};
106134
}

0 commit comments

Comments
 (0)