From f94a6b5f19d426b6d2394696e2992cb68e583dd0 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 19 Jul 2019 11:30:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E7=BD=91=E9=A1=B5?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E5=88=9D=E5=A7=8B=E5=8C=96=E6=8E=A7=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/longbow-select/longbow-select.js | 89 +++++++++++++------ 1 file changed, 62 insertions(+), 27 deletions(-) diff --git a/Bootstrap.Admin/wwwroot/lib/longbow-select/longbow-select.js b/Bootstrap.Admin/wwwroot/lib/longbow-select/longbow-select.js index 88114688..b3f7e419 100644 --- a/Bootstrap.Admin/wwwroot/lib/longbow-select/longbow-select.js +++ b/Bootstrap.Admin/wwwroot/lib/longbow-select/longbow-select.js @@ -4,13 +4,13 @@ var lgbSelect = function (element, options) { this.$element = $(element); this.options = $.extend({}, lgbSelect.DEFAULTS, options); - this.init(); + this.initDom(); }; lgbSelect.VERSION = '1.0'; lgbSelect.Author = 'argo@163.com'; lgbSelect.DataKey = "lgb.select"; - lgbSelect.Template = '
'; + lgbSelect.Template = '
'; lgbSelect.Template += ''; lgbSelect.Template += ''; lgbSelect.Template += ' '; @@ -43,7 +43,65 @@ $.fn.lgbSelect.Constructor = lgbSelect; var _proto = lgbSelect.prototype; + _proto.initDom = function () { + var that = this; + + // 初始化根据不同的控件进行不同的方案 + if (this.$element.prop('nodeName') === 'SELECT') this.initBySelect(); + else this.init(); + + // bind event + this.$ctl.on('click', '.form-select-input', function (e) { + e.preventDefault(); + + that.$ctl.toggleClass('open'); + // calc width + that.$ctl.find('.dropdown-menu').outerWidth(that.$ctl.outerWidth()); + }); + + this.$ctl.on('click', 'a.dropdown-item', function (e) { + e.preventDefault(); + + var $this = $(this); + $this.parent().children().removeClass('active'); + that.val($this.attr('data-val'), true); + }); + + $(document).on('click', function (e) { + if (that.$input[0] !== e.target) + that.closeMenu(); + }); + + this.$element.data(lgbSelect.DataKey, this); + }; + _proto.init = function () { + this.$ctl = this.$element; + this.$element = this.$ctl.find('input:hidden[data-toggle="lgbSelect"]'); + + // 下拉组合框 + this.$input = this.$ctl.find('.form-select-input'); + this.$menus = this.$ctl.find('.dropdown-menu'); + + if (this.options.borderClass) { + this.$input.addClass(this.options.borderClass); + } + + this.source = this.$menus.children().map(function (index, ele) { + return { value: $(ele).attr('data-val'), text: $(ele).text(), selected: $(ele).selected }; + }); + + // 设置值 + var value = this.$input.val(); + var option = this.$menus.find('a[data-val="' + value + '"]'); + if (option.length === 0) option = this.$menus.find('a[data-val="' + this.$element.attr('data-default-val') + '"]'); + if (option.length === 1) option.addClass('active'); + + this.$input.val(option.text()); + this.$element.val(option.attr('data-val')).attr('data-text', option.text()); + }; + + _proto.initBySelect = function () { var getUID = function (prefix) { if (!prefix) prefix = 'lgb'; do prefix += ~~(Math.random() * 1000000); @@ -96,36 +154,13 @@ // replace element select -> input hidden this.$element.remove(); - this.$element = $('').val(that.val()).insertAfter(this.$ctl); - this.$element.data(lgbSelect.DataKey, this); + this.$element = $('').val(that.val()).insertBefore(this.$input); // bind ori atts attrs.forEach(function (v) { that.$element.attr(v.name, v.value); }); - // bind event - this.$ctl.on('click', '.form-select-input', function (e) { - e.preventDefault(); - - that.$ctl.toggleClass('open'); - // calc width - that.$ctl.find('.dropdown-menu').outerWidth(that.$ctl.outerWidth()); - }); - - this.$ctl.on('click', 'a.dropdown-item', function (e) { - e.preventDefault(); - - var $this = $(this); - $this.parent().children().removeClass('active'); - that.val($this.attr('data-val'), true); - }); - - $(document).on('click', function (e) { - if (that.$input[0] !== e.target) - that.closeMenu(); - }); - // init dropdown-menu this.reset(data); }; @@ -193,6 +228,6 @@ }; $(function () { - $('select[data-toggle="lgbSelect"]').lgbSelect(); + $('[data-toggle="lgbSelect"]').lgbSelect(); }); })(jQuery);