Browse Source

Dropdown: Adds strict type checking as an option for form selection, allowing for better handling of data-value=0, value='' and other ambiguous values

pull/701/merge
jlukic 11 years ago
parent
commit
565e0a075a
2 changed files with 22 additions and 9 deletions
  1. 6
      src/modules/behavior/form.js
  2. 25
      src/modules/dropdown.js

6
src/modules/behavior/form.js

@ -107,12 +107,10 @@ $.fn.form = function(fields, parameters) {
; ;
$field $field
.each(function() { .each(function() {
var
var
type = $(this).prop('type'), type = $(this).prop('type'),
inputEvent = module.get.changeEvent(type) inputEvent = module.get.changeEvent(type)
; ;
if(settings.inline == true) {
}
$(this) $(this)
.on(inputEvent + eventNamespace, module.event.field.change) .on(inputEvent + eventNamespace, module.event.field.change)
; ;
@ -201,7 +199,7 @@ $.fn.form = function(fields, parameters) {
get: { get: {
changeEvent: function(type) { changeEvent: function(type) {
if(type == 'checkbox' || type == 'radio') {
if(type == 'checkbox' || type == 'radio' || type == 'hidden') {
return 'change'; return 'change';
} }
else { else {

25
src/modules/dropdown.js

@ -334,7 +334,7 @@ $.fn.dropdown = function(parameters) {
: $module.data(metadata.value) : $module.data(metadata.value)
; ;
}, },
item: function(value) {
item: function(value, strict) {
var var
$selectedItem = false $selectedItem = false
; ;
@ -344,6 +344,10 @@ $.fn.dropdown = function(parameters) {
? module.get.value() ? module.get.value()
: module.get.text() : module.get.text()
; ;
strict = (value === '')
? true
: strict || false
;
if(value !== undefined) { if(value !== undefined) {
$item $item
.each(function() { .each(function() {
@ -358,11 +362,22 @@ $.fn.dropdown = function(parameters) {
? optionText.toLowerCase() ? optionText.toLowerCase()
: optionText : optionText
; ;
if( optionValue == value ) {
$selectedItem = $(this);
if(strict) {
module.debug('Ambiguous dropdown value using strict type check', value);
if( optionValue === value ) {
$selectedItem = $(this);
}
else if( !$selectedItem && optionText === value ) {
$selectedItem = $(this);
}
} }
else if( !$selectedItem && optionText == value ) {
$selectedItem = $(this);
else {
if( optionValue == value ) {
$selectedItem = $(this);
}
else if( !$selectedItem && optionText == value ) {
$selectedItem = $(this);
}
} }
}) })
; ;

Loading…
Cancel
Save