feat: longbow-select 支持 data-default-val 默认值初始化

This commit is contained in:
Argo Zhang 2019-07-15 08:07:28 +08:00
parent 4b199b5724
commit 5a1d6f2be4
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
1 changed files with 7 additions and 3 deletions

View File

@ -76,11 +76,10 @@
} }
this.$input.attr('placeholder', this.options.placeholder); this.$input.attr('placeholder', this.options.placeholder);
// init dropdown-menu // init dropdown-menu data
var data = this.$element.find('option').map(function () { var data = this.$element.find('option').map(function () {
return { value: this.value, text: this.text, selected: this.selected } return { value: this.value, text: this.text, selected: this.selected }
}); });
this.reset(data);
// bind attribute // bind attribute
["data-valid", "data-required-msg"].forEach(function (v, index) { ["data-valid", "data-required-msg"].forEach(function (v, index) {
@ -126,6 +125,9 @@
if (that.$input[0] !== e.target) if (that.$input[0] !== e.target)
that.closeMenu(); that.closeMenu();
}); });
// init dropdown-menu
this.reset(data);
}; };
_proto.closeMenu = function () { _proto.closeMenu = function () {
@ -155,8 +157,10 @@
$.each(value, function (index) { $.each(value, function (index) {
var $item = $('<a class="dropdown-item" href="#" data-val="' + this.value + '">' + this.text + '</a>'); var $item = $('<a class="dropdown-item" href="#" data-val="' + this.value + '">' + this.text + '</a>');
that.$menus.append($item); that.$menus.append($item);
if (this.selected === true || this.value === oldValue || index === 0) { if (this.selected === true || this.value === oldValue || index === 0 || this.value === that.$element.attr('data-default-val')) {
that.$input.attr('value', this.text); that.$input.attr('value', this.text);
that.$element.val(this.value).attr('data-text', this.text);
$item.addClass('active');
} }
}); });