diff --git a/README.md b/README.md index 4a32bd8..4c398a2 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,12 @@ View the [plugin documentation](http://plugins.krajee.com/dependent-dropdown) an ``` {group-name: {id: , name: }} ``` + + When no data is available for a specific situation (like an backend error), you can send a custom emptyMsg + + ``` + {emptyMsg: ''} + ``` ## Installation @@ -350,4 +356,4 @@ $('#input-id').depdrop('init'); ## License -**dependent-dropdown** is released under the BSD 3-Clause License. See the bundled `LICENSE.md` for details. \ No newline at end of file +**dependent-dropdown** is released under the BSD 3-Clause License. See the bundled `LICENSE.md` for details. diff --git a/js/dependent-dropdown.js b/js/dependent-dropdown.js index e1624ce..d84503b 100644 --- a/js/dependent-dropdown.js +++ b/js/dependent-dropdown.js @@ -120,20 +120,22 @@ }, success: function (data) { selected = isEmpty(data.selected) ? (self.initVal === false ? null : self.initVal): data.selected; - if (isEmpty(data)) { - addOption($el, '', vNullMsg, ''); + if (!isEmpty(data.emptyMsg)) { + vNullMsg = data.emptyMsg; + } + if (isEmpty(data) || isEmpty(data.output) || Object.keys(data.output).length == 0) { + $el.html(''); + addOption($el, 0, vNullMsg, ''); } else { $el.html(self.getSelect(data.output, vDefault, selected)); if ($el.find('optgroup').length > 0) { $el.find('option[value=""]').attr('disabled', 'disabled'); } - if (data.output) { - if (selected && $.isArray(selected) && $el.attr('multiple')) { - $el.val(selected); - } - $el.removeAttr('disabled'); + if (selected && $.isArray(selected) && $el.attr('multiple')) { + $el.val(selected); } + $el.removeAttr('disabled'); } optCount = $el.find('option').length; if ($el.find('option[value=""]').length > 0) { @@ -220,4 +222,4 @@ $(function () { $('select.depdrop').depdrop(); }); -}(window.jQuery)); \ No newline at end of file +}(window.jQuery));