Skip to content

Commit 1dfa6c3

Browse files
committed
Enhance the select field with null value support
1 parent 446d846 commit 1dfa6c3

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/directives/decorators/bootstrap/select.html

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="form-group {{form.htmlClass}} schema-form-select"
1+
<div ng-if="form.allowAutoNullOption !== undefined" class="form-group {{form.htmlClass}} schema-form-select"
22
ng-class="{'has-error': hasError(), 'has-success': hasSuccess(), 'has-feedback': form.feedback !== false}">
33
<label class="control-label" ng-show="showTitle()">
44
{{form.title}}
@@ -11,6 +11,25 @@
1111
schema-validate="form"
1212
ng-options="item.value as item.name group by item.group for item in form.titleMap"
1313
name="{{form.key.slice(-1)[0]}}">
14+
<option ng-if="form.allowAutoNullOption !== false" value="">{{form.allowAutoNullOption !== true ? form.allowAutoNullOption : ''}}</option>
1415
</select>
1516
<div class="help-block" sf-message="form.description"></div>
1617
</div>
18+
<div ng-if="form.allowAutoNullOption === undefined" class="form-group {{form.htmlClass}} schema-form-select"
19+
ng-class="{'has-error': hasError(), 'has-success': hasSuccess(), 'has-feedback': form.feedback !== false}">
20+
<label class="control-label" ng-show="showTitle()">
21+
{{form.title}}
22+
</label>
23+
<select ng-model="$$value$$"
24+
ng-model-options="form.ngModelOptions"
25+
ng-disabled="form.readonly"
26+
sf-changed="form"
27+
class="form-control {{form.fieldHtmlClass}}"
28+
schema-validate="form"
29+
ng-options="item.value as item.name for item in form.titleMap"
30+
name="{{form.key.slice(-1)[0]}}">
31+
</select>
32+
<div class="help-block"
33+
ng-show="(hasError() && errorMessage(schemaError())) || form.description"
34+
ng-bind-html="(hasError() && errorMessage(schemaError())) || form.description"></div>
35+
</div>

src/services/schema-form.js

+15
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,21 @@ angular.module('schemaForm').provider('schemaForm',
359359
}
360360
}
361361

362+
if (obj.type === 'select' && obj.titleMap && obj.allowAutoNullOption !== false) {
363+
// when allowNull === false, we leave titleMap as it is.
364+
// else we filter out the null value and setup allowNull to the label
365+
// so the select template will behaviour different according to it.
366+
obj.titleMap = obj.titleMap.filter(function(item) {
367+
if (item.value === null) {
368+
// so we have null value option, setup the label and make it always selectable.
369+
if (obj.allowAutoNullOption === undefined)
370+
obj.allowAutoNullOption = item.name === null ? '' : item.name;
371+
return false;
372+
}
373+
return true;
374+
});
375+
}
376+
362377
// Are we inheriting readonly?
363378
if (readonly === true) { // Inheriting false is not cool.
364379
obj.readonly = true;

0 commit comments

Comments
 (0)