diff --git a/CHANGELOG.md b/CHANGELOG.md index 47e0b9f..d1ec573 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # jQuery Dependent Selects Change Log +## Version 1.2.2 + +- Allow placeholder value for sub selects through a passed array of strings +- Fixes a display bug when form displays a hirer hierarchy choice (attr -> prop) +- Fixes that undefined caused an exception to exit when initially displaying the selection value + + ## Version 1.2.1 - Fixes a display bug with placeholder selects and labels not displaying at the correct times diff --git a/README.md b/README.md index 2d61e08..f768fe9 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ $('.example-class').dependentSelects({ placeholderOption: '', // String or array of strings: The text used for the sub select boxes' placeholder option. // If an array, the first 'sub' level will be the first array item, you should manually create // the top level's placeholder in the HTML. + placeholderValue: '', // String or array of strings: The value used for the sub select boxes' placeholder option. placeholderSelect: false, // Array of strings: The text used for placeholder select boxes for sub levels. class: false, // String: Add an extra class to all sub selects labels: false // Array of strings: The text used for the sub select boxes' labels. Label element is diff --git a/jquery.dependent-selects.coffee b/jquery.dependent-selects.coffee index db83526..90a3d0a 100644 --- a/jquery.dependent-selects.coffee +++ b/jquery.dependent-selects.coffee @@ -1,6 +1,7 @@ ### -# jQuery Dependent Selects v1.2.2 +# jQuery Dependent Selects v1.2.3 # Copyright 2012 Mark J Smith, Simpleweb +# Licenced under MIT # Details on http://github.com/simpleweb/jquery-dependent-selects ### @@ -10,6 +11,7 @@ options = $.extend({ 'separator': ' > ' 'placeholderOption': '' + 'placeholderValue': '' 'placeholderSelect': false 'class': false 'labels': false @@ -68,7 +70,19 @@ text = placeholder[placeholder.length-1] else text = placeholder - $("") + + def_val = options.placeholderValue + if typeof def_val == 'object' + if def_val[depth] + val = def_val[depth] + else + val = def_val[def_val.length-1] + else + val = def_val + if val != null && val != '' + val = ' value="' + val + '"'; + + $("#{text}") labelAtDepth = (depth, $select) -> depth-- @@ -128,7 +142,7 @@ clearAllSelectsByParent = ($parent) -> $(".dependent-sub[data-dependent-id='#{$parent.attr('data-dependent-id')}']").each -> if parseInt($(@).attr('data-dependent-depth')) > parseInt($parent.attr('data-dependent-depth')) - $(@).find('option:first').attr('selected', 'selected') + $(@).find('option:first').prop('selected', true) hideSelect $(@) createNewSelect = (name, $select, depth) -> @@ -211,13 +225,14 @@ for i in [(parseInt $selected_select.attr('data-dependent-depth'))..0] $current_select.find('option').each -> if $(@).html() == current_option_text - $(@).attr('selected', 'selected') + $(@).prop('selected', true) else - $(@).removeAttr('selected') - + $(@).prop('selected', false) + showSelect $current_select - current_option_text = splitName($current_select.attr('data-dependent-path')).slice(-1)[0]; - $current_select = findSelectParent($current_select) + if typeof($current_select.attr('data-dependent-path')) != "undefined" + current_option_text = splitName($current_select.attr('data-dependent-path')).slice(-1)[0]; + $current_select = findSelectParent($current_select) $selected_select.trigger('change') diff --git a/jquery.dependent-selects.js b/jquery.dependent-selects.js index a41a67f..c51bbf2 100644 --- a/jquery.dependent-selects.js +++ b/jquery.dependent-selects.js @@ -1,8 +1,8 @@ -// Generated by CoffeeScript 1.9.1 - +// Generated by CoffeeScript 1.9.2 /* - * jQuery Dependent Selects v1.2.2 + * jQuery Dependent Selects v1.2.3 * Copyright 2012 Mark J Smith, Simpleweb + * Licenced under MIT * Details on http://github.com/simpleweb/jquery-dependent-selects */ (function($) { @@ -14,6 +14,7 @@ options = $.extend({ 'separator': ' > ', 'placeholderOption': '', + 'placeholderValue': '', 'placeholderSelect': false, 'class': false, 'labels': false @@ -71,7 +72,7 @@ } }; placeholderOptionAtDepth = function(depth) { - var placeholder, text; + var def_val, placeholder, text, val; depth--; placeholder = options.placeholderOption; if (typeof placeholder === 'object') { @@ -83,7 +84,20 @@ } else { text = placeholder; } - return $(""); + def_val = options.placeholderValue; + if (typeof def_val === 'object') { + if (def_val[depth]) { + val = def_val[depth]; + } else { + val = def_val[def_val.length - 1]; + } + } else { + val = def_val; + } + if (val !== null && val !== '') { + val = ' value="' + val + '"'; + } + return $("" + text + ""); }; labelAtDepth = function(depth, $select) { var labels; @@ -156,7 +170,7 @@ clearAllSelectsByParent = function($parent) { return $(".dependent-sub[data-dependent-id='" + ($parent.attr('data-dependent-id')) + "']").each(function() { if (parseInt($(this).attr('data-dependent-depth')) > parseInt($parent.attr('data-dependent-depth'))) { - $(this).find('option:first').attr('selected', 'selected'); + $(this).find('option:first').prop('selected', true); return hideSelect($(this)); } }); @@ -239,14 +253,16 @@ for (i = j = ref = parseInt($selected_select.attr('data-dependent-depth')); ref <= 0 ? j <= 0 : j >= 0; i = ref <= 0 ? ++j : --j) { $current_select.find('option').each(function() { if ($(this).html() === current_option_text) { - return $(this).attr('selected', 'selected'); + return $(this).prop('selected', true); } else { - return $(this).removeAttr('selected'); + return $(this).prop('selected', false); } }); showSelect($current_select); - current_option_text = splitName($current_select.attr('data-dependent-path')).slice(-1)[0]; - $current_select = findSelectParent($current_select); + if (typeof ($current_select.attr('data-dependent-path')) !== "undefined") { + current_option_text = splitName($current_select.attr('data-dependent-path')).slice(-1)[0]; + $current_select = findSelectParent($current_select); + } } return $selected_select.trigger('change'); }