From 1dfa6c3bdf061e1b3824ef137a03c500c3a0ad48 Mon Sep 17 00:00:00 2001 From: ulion <ulion2002@gmail.com> Date: Wed, 11 Mar 2015 08:30:34 +0800 Subject: [PATCH 1/2] Enhance the select field with null value support --- .../decorators/bootstrap/select.html | 21 ++++++++++++++++++- src/services/schema-form.js | 15 +++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/directives/decorators/bootstrap/select.html b/src/directives/decorators/bootstrap/select.html index 05c45a5ae..c7bd41c32 100644 --- a/src/directives/decorators/bootstrap/select.html +++ b/src/directives/decorators/bootstrap/select.html @@ -1,4 +1,4 @@ -<div class="form-group {{form.htmlClass}} schema-form-select" +<div ng-if="form.allowAutoNullOption !== undefined" class="form-group {{form.htmlClass}} schema-form-select" ng-class="{'has-error': hasError(), 'has-success': hasSuccess(), 'has-feedback': form.feedback !== false}"> <label class="control-label" ng-show="showTitle()"> {{form.title}} @@ -11,6 +11,25 @@ schema-validate="form" ng-options="item.value as item.name group by item.group for item in form.titleMap" name="{{form.key.slice(-1)[0]}}"> + <option ng-if="form.allowAutoNullOption !== false" value="">{{form.allowAutoNullOption !== true ? form.allowAutoNullOption : ''}}</option> </select> <div class="help-block" sf-message="form.description"></div> </div> +<div ng-if="form.allowAutoNullOption === undefined" class="form-group {{form.htmlClass}} schema-form-select" + ng-class="{'has-error': hasError(), 'has-success': hasSuccess(), 'has-feedback': form.feedback !== false}"> + <label class="control-label" ng-show="showTitle()"> + {{form.title}} + </label> + <select ng-model="$$value$$" + ng-model-options="form.ngModelOptions" + ng-disabled="form.readonly" + sf-changed="form" + class="form-control {{form.fieldHtmlClass}}" + schema-validate="form" + ng-options="item.value as item.name for item in form.titleMap" + name="{{form.key.slice(-1)[0]}}"> + </select> + <div class="help-block" + ng-show="(hasError() && errorMessage(schemaError())) || form.description" + ng-bind-html="(hasError() && errorMessage(schemaError())) || form.description"></div> +</div> \ No newline at end of file diff --git a/src/services/schema-form.js b/src/services/schema-form.js index b1664f0e2..99774b04c 100644 --- a/src/services/schema-form.js +++ b/src/services/schema-form.js @@ -359,6 +359,21 @@ angular.module('schemaForm').provider('schemaForm', } } + if (obj.type === 'select' && obj.titleMap && obj.allowAutoNullOption !== false) { + // when allowNull === false, we leave titleMap as it is. + // else we filter out the null value and setup allowNull to the label + // so the select template will behaviour different according to it. + obj.titleMap = obj.titleMap.filter(function(item) { + if (item.value === null) { + // so we have null value option, setup the label and make it always selectable. + if (obj.allowAutoNullOption === undefined) + obj.allowAutoNullOption = item.name === null ? '' : item.name; + return false; + } + return true; + }); + } + // Are we inheriting readonly? if (readonly === true) { // Inheriting false is not cool. obj.readonly = true; From 34c9c29fb56cdd98f891b35fa000e4e3c6841d0f Mon Sep 17 00:00:00 2001 From: ulion <ulion2002@gmail.com> Date: Sat, 21 Mar 2015 06:42:55 +0800 Subject: [PATCH 2/2] Do not change titleMap order if null value in it. --- src/services/schema-form.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/services/schema-form.js b/src/services/schema-form.js index 99774b04c..c2b52bfaa 100644 --- a/src/services/schema-form.js +++ b/src/services/schema-form.js @@ -359,18 +359,16 @@ angular.module('schemaForm').provider('schemaForm', } } - if (obj.type === 'select' && obj.titleMap && obj.allowAutoNullOption !== false) { - // when allowNull === false, we leave titleMap as it is. - // else we filter out the null value and setup allowNull to the label - // so the select template will behaviour different according to it. - obj.titleMap = obj.titleMap.filter(function(item) { + if (obj.type === 'select' && obj.titleMap) { + // we check whether null value is in titleMap. if so, we fix possible null + // label and set allowAutoNullOption to false, then leave the titleMap as it is + obj.titleMap.forEach(function(item) { if (item.value === null) { - // so we have null value option, setup the label and make it always selectable. if (obj.allowAutoNullOption === undefined) - obj.allowAutoNullOption = item.name === null ? '' : item.name; - return false; + obj.allowAutoNullOption = false; + if (item.name === null) + item.name = ''; } - return true; }); }