zoukankan      html  css  js  c++  java
  • 下拉列表中复选框多选

    
    
    /**
     * @author zhixin wen <wenzhixin2010@gmail.com>
     */
    
    .ms-parent {
        display: inline-block;
        position: relative;
        vertical-align: middle;
    }
    
    .ms-choice {
        display: block;
         100%;
        height: 26px;
        padding: 0;
        overflow: hidden;
        cursor: pointer;
        border: 1px solid #aaa;
        text-align: left;
        white-space: nowrap;
        line-height: 26px;
        color: #444;
        text-decoration: none;
        -webkit-border-radius: 4px;
        -moz-border-radius: 4px;
        border-radius: 4px;
        background-color: #fff;
    }
    
    .ms-choice.disabled {
        background-color: #f4f4f4;
        background-image: none;
        border: 1px solid #ddd;
        cursor: default;
    }
    
    .ms-choice > span {
        position: absolute;
        top: 0;
        left: 0;
        right: 20px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        display: block;
        padding-left: 8px;
    }
    
    .ms-choice > span.placeholder {
        color: #999;
    }
    
    .ms-choice > div {
        position: absolute;
        top: 0;
        right: 0;
         20px;
        height: 25px;
        background: url('multiple-select.png') left top no-repeat;
    }
    
    .ms-choice > div.open {
        background: url('multiple-select.png') right top no-repeat;
    }
    
    .ms-drop {
         100%;
        overflow: hidden;
        display: none;
        margin-top: -1px;
        padding: 0;
        position: absolute;
        z-index: 1000;
        background: #fff;
        color: #000;
        border: 1px solid #aaa;
        -webkit-border-radius: 4px;
        -moz-border-radius: 4px;
        border-radius: 4px;
    }
    
    .ms-drop.bottom {
        top: 100%;
        -webkit-box-shadow: 0 4px 5px rgba(0, 0, 0, .15);
        -moz-box-shadow: 0 4px 5px rgba(0, 0, 0, .15);
        box-shadow: 0 4px 5px rgba(0, 0, 0, .15);
    }
    
    .ms-drop.top {
        bottom: 100%;
        -webkit-box-shadow: 0 -4px 5px rgba(0, 0, 0, .15);
        -moz-box-shadow: 0 -4px 5px rgba(0, 0, 0, .15);
        box-shadow: 0 -4px 5px rgba(0, 0, 0, .15);
    }
    
    .ms-search {
        display: inline-block;
        margin: 0;
        min-height: 26px;
        padding: 4px;
        position: relative;
        white-space: nowrap;
         100%;
        z-index: 10000;
    }
    
    .ms-search input {
         100%;
        height: auto !important;
        min-height: 24px;
        padding: 0 20px 0 5px;
        margin: 0;
        outline: 0;
        font-family: sans-serif;
        font-size: 1em;
        border: 1px solid #aaa;
        -webkit-border-radius: 0;
        -moz-border-radius: 0;
        border-radius: 0;
        -webkit-box-shadow: none;
        -moz-box-shadow: none;
        box-shadow: none;
        background: #fff url('multiple-select.png') no-repeat 100% -22px;
        background: url('multiple-select.png') no-repeat 100% -22px, -webkit-gradient(linear, left bottom, left top, color-stop(0.85, white), color-stop(0.99, #eeeeee));
        background: url('multiple-select.png') no-repeat 100% -22px, -webkit-linear-gradient(center bottom, white 85%, #eeeeee 99%);
        background: url('multiple-select.png') no-repeat 100% -22px, -moz-linear-gradient(center bottom, white 85%, #eeeeee 99%);
        background: url('multiple-select.png') no-repeat 100% -22px, -o-linear-gradient(bottom, white 85%, #eeeeee 99%);
        background: url('multiple-select.png') no-repeat 100% -22px, -ms-linear-gradient(top, #ffffff 85%, #eeeeee 99%);
        background: url('multiple-select.png') no-repeat 100% -22px, linear-gradient(top, #ffffff 85%, #eeeeee 99%);
    }
    
    .ms-search, .ms-search input {
        -webkit-box-sizing: border-box;
        -khtml-box-sizing: border-box;
        -moz-box-sizing: border-box;
        -ms-box-sizing: border-box;
        box-sizing: border-box;
    }
    
    .ms-drop ul {
        overflow: auto;
        margin: 0;
        padding: 5px 8px;
    }
    
    .ms-drop ul > li {
        list-style: none;
        display: list-item;
        background-image: none;
        position: static;
    }
    
    .ms-drop ul > li .disabled {
        opacity: .35;
        filter: Alpha(Opacity=35);
    }
    
    .ms-drop ul > li.multiple {
        display: block;
        float: left;
    }
    
    .ms-drop ul > li.group {
        clear: both;
    }
    
    .ms-drop ul > li.multiple label {
         100%;
        display: block;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    
    .ms-drop ul > li label {
        font-weight: normal;
        display: block;
        white-space: nowrap;
    }
    
    .ms-drop ul > li label.optgroup {
        font-weight: bold;
    }
    
    .ms-drop input[type="checkbox"] {
        vertical-align: middle;
    }
    
    .ms-drop .ms-no-results {
        display: none;
    }
    
    
    

      

    /** * @author zhixin wen <wenzhixin2010@gmail.com> * @version 1.2.1 * * http://wenzhixin.net.cn/p/multiple-select/ */ (function ($) { 'use strict'; // it only does '%s', and return '' when arguments are undefined var sprintf = function (str) { var args = arguments, flag = true, i = 1; str = str.replace(/%s/g, function () { var arg = args[i++]; if (typeof arg === 'undefined') { flag = false; return ''; } return arg; }); return flag ? str : ''; }; var removeDiacritics = function (str) { var defaultDiacriticsRemovalMap = [ {'base':'A', 'letters':/[u0041u24B6uFF21u00C0u00C1u00C2u1EA6u1EA4u1EAAu1EA8u00C3u0100u0102u1EB0u1EAEu1EB4u1EB2u0226u01E0u00C4u01DEu1EA2u00C5u01FAu01CDu0200u0202u1EA0u1EACu1EB6u1E00u0104u023Au2C6F]/g}, {'base':'AA','letters':/[uA732]/g}, {'base':'AE','letters':/[u00C6u01FCu01E2]/g}, {'base':'AO','letters':/[uA734]/g}, {'base':'AU','letters':/[uA736]/g}, {'base':'AV','letters':/[uA738uA73A]/g}, {'base':'AY','letters':/[uA73C]/g}, {'base':'B', 'letters':/[u0042u24B7uFF22u1E02u1E04u1E06u0243u0182u0181]/g}, {'base':'C', 'letters':/[u0043u24B8uFF23u0106u0108u010Au010Cu00C7u1E08u0187u023BuA73E]/g}, {'base':'D', 'letters':/[u0044u24B9uFF24u1E0Au010Eu1E0Cu1E10u1E12u1E0Eu0110u018Bu018Au0189uA779]/g}, {'base':'DZ','letters':/[u01F1u01C4]/g}, {'base':'Dz','letters':/[u01F2u01C5]/g}, {'base':'E', 'letters':/[u0045u24BAuFF25u00C8u00C9u00CAu1EC0u1EBEu1EC4u1EC2u1EBCu0112u1E14u1E16u0114u0116u00CBu1EBAu011Au0204u0206u1EB8u1EC6u0228u1E1Cu0118u1E18u1E1Au0190u018E]/g}, {'base':'F', 'letters':/[u0046u24BBuFF26u1E1Eu0191uA77B]/g}, {'base':'G', 'letters':/[u0047u24BCuFF27u01F4u011Cu1E20u011Eu0120u01E6u0122u01E4u0193uA7A0uA77DuA77E]/g}, {'base':'H', 'letters':/[u0048u24BDuFF28u0124u1E22u1E26u021Eu1E24u1E28u1E2Au0126u2C67u2C75uA78D]/g}, {'base':'I', 'letters':/[u0049u24BEuFF29u00CCu00CDu00CEu0128u012Au012Cu0130u00CFu1E2Eu1EC8u01CFu0208u020Au1ECAu012Eu1E2Cu0197]/g}, {'base':'J', 'letters':/[u004Au24BFuFF2Au0134u0248]/g}, {'base':'K', 'letters':/[u004Bu24C0uFF2Bu1E30u01E8u1E32u0136u1E34u0198u2C69uA740uA742uA744uA7A2]/g}, {'base':'L', 'letters':/[u004Cu24C1uFF2Cu013Fu0139u013Du1E36u1E38u013Bu1E3Cu1E3Au0141u023Du2C62u2C60uA748uA746uA780]/g}, {'base':'LJ','letters':/[u01C7]/g}, {'base':'Lj','letters':/[u01C8]/g}, {'base':'M', 'letters':/[u004Du24C2uFF2Du1E3Eu1E40u1E42u2C6Eu019C]/g}, {'base':'N', 'letters':/[u004Eu24C3uFF2Eu01F8u0143u00D1u1E44u0147u1E46u0145u1E4Au1E48u0220u019DuA790uA7A4]/g}, {'base':'NJ','letters':/[u01CA]/g}, {'base':'Nj','letters':/[u01CB]/g}, {'base':'O', 'letters':/[u004Fu24C4uFF2Fu00D2u00D3u00D4u1ED2u1ED0u1ED6u1ED4u00D5u1E4Cu022Cu1E4Eu014Cu1E50u1E52u014Eu022Eu0230u00D6u022Au1ECEu0150u01D1u020Cu020Eu01A0u1EDCu1EDAu1EE0u1EDEu1EE2u1ECCu1ED8u01EAu01ECu00D8u01FEu0186u019FuA74AuA74C]/g}, {'base':'OI','letters':/[u01A2]/g}, {'base':'OO','letters':/[uA74E]/g}, {'base':'OU','letters':/[u0222]/g}, {'base':'P', 'letters':/[u0050u24C5uFF30u1E54u1E56u01A4u2C63uA750uA752uA754]/g}, {'base':'Q', 'letters':/[u0051u24C6uFF31uA756uA758u024A]/g}, {'base':'R', 'letters':/[u0052u24C7uFF32u0154u1E58u0158u0210u0212u1E5Au1E5Cu0156u1E5Eu024Cu2C64uA75AuA7A6uA782]/g}, {'base':'S', 'letters':/[u0053u24C8uFF33u1E9Eu015Au1E64u015Cu1E60u0160u1E66u1E62u1E68u0218u015Eu2C7EuA7A8uA784]/g}, {'base':'T', 'letters':/[u0054u24C9uFF34u1E6Au0164u1E6Cu021Au0162u1E70u1E6Eu0166u01ACu01AEu023EuA786]/g}, {'base':'TZ','letters':/[uA728]/g}, {'base':'U', 'letters':/[u0055u24CAuFF35u00D9u00DAu00DBu0168u1E78u016Au1E7Au016Cu00DCu01DBu01D7u01D5u01D9u1EE6u016Eu0170u01D3u0214u0216u01AFu1EEAu1EE8u1EEEu1EECu1EF0u1EE4u1E72u0172u1E76u1E74u0244]/g}, {'base':'V', 'letters':/[u0056u24CBuFF36u1E7Cu1E7Eu01B2uA75Eu0245]/g}, {'base':'VY','letters':/[uA760]/g}, {'base':'W', 'letters':/[u0057u24CCuFF37u1E80u1E82u0174u1E86u1E84u1E88u2C72]/g}, {'base':'X', 'letters':/[u0058u24CDuFF38u1E8Au1E8C]/g}, {'base':'Y', 'letters':/[u0059u24CEuFF39u1EF2u00DDu0176u1EF8u0232u1E8Eu0178u1EF6u1EF4u01B3u024Eu1EFE]/g}, {'base':'Z', 'letters':/[u005Au24CFuFF3Au0179u1E90u017Bu017Du1E92u1E94u01B5u0224u2C7Fu2C6BuA762]/g}, {'base':'a', 'letters':/[u0061u24D0uFF41u1E9Au00E0u00E1u00E2u1EA7u1EA5u1EABu1EA9u00E3u0101u0103u1EB1u1EAFu1EB5u1EB3u0227u01E1u00E4u01DFu1EA3u00E5u01FBu01CEu0201u0203u1EA1u1EADu1EB7u1E01u0105u2C65u0250]/g}, {'base':'aa','letters':/[uA733]/g}, {'base':'ae','letters':/[u00E6u01FDu01E3]/g}, {'base':'ao','letters':/[uA735]/g}, {'base':'au','letters':/[uA737]/g}, {'base':'av','letters':/[uA739uA73B]/g}, {'base':'ay','letters':/[uA73D]/g}, {'base':'b', 'letters':/[u0062u24D1uFF42u1E03u1E05u1E07u0180u0183u0253]/g}, {'base':'c', 'letters':/[u0063u24D2uFF43u0107u0109u010Bu010Du00E7u1E09u0188u023CuA73Fu2184]/g}, {'base':'d', 'letters':/[u0064u24D3uFF44u1E0Bu010Fu1E0Du1E11u1E13u1E0Fu0111u018Cu0256u0257uA77A]/g}, {'base':'dz','letters':/[u01F3u01C6]/g}, {'base':'e', 'letters':/[u0065u24D4uFF45u00E8u00E9u00EAu1EC1u1EBFu1EC5u1EC3u1EBDu0113u1E15u1E17u0115u0117u00EBu1EBBu011Bu0205u0207u1EB9u1EC7u0229u1E1Du0119u1E19u1E1Bu0247u025Bu01DD]/g}, {'base':'f', 'letters':/[u0066u24D5uFF46u1E1Fu0192uA77C]/g}, {'base':'g', 'letters':/[u0067u24D6uFF47u01F5u011Du1E21u011Fu0121u01E7u0123u01E5u0260uA7A1u1D79uA77F]/g}, {'base':'h', 'letters':/[u0068u24D7uFF48u0125u1E23u1E27u021Fu1E25u1E29u1E2Bu1E96u0127u2C68u2C76u0265]/g}, {'base':'hv','letters':/[u0195]/g}, {'base':'i', 'letters':/[u0069u24D8uFF49u00ECu00EDu00EEu0129u012Bu012Du00EFu1E2Fu1EC9u01D0u0209u020Bu1ECBu012Fu1E2Du0268u0131]/g}, {'base':'j', 'letters':/[u006Au24D9uFF4Au0135u01F0u0249]/g}, {'base':'k', 'letters':/[u006Bu24DAuFF4Bu1E31u01E9u1E33u0137u1E35u0199u2C6AuA741uA743uA745uA7A3]/g}, {'base':'l', 'letters':/[u006Cu24DBuFF4Cu0140u013Au013Eu1E37u1E39u013Cu1E3Du1E3Bu017Fu0142u019Au026Bu2C61uA749uA781uA747]/g}, {'base':'lj','letters':/[u01C9]/g}, {'base':'m', 'letters':/[u006Du24DCuFF4Du1E3Fu1E41u1E43u0271u026F]/g}, {'base':'n', 'letters':/[u006Eu24DDuFF4Eu01F9u0144u00F1u1E45u0148u1E47u0146u1E4Bu1E49u019Eu0272u0149uA791uA7A5]/g}, {'base':'nj','letters':/[u01CC]/g}, {'base':'o', 'letters':/[u006Fu24DEuFF4Fu00F2u00F3u00F4u1ED3u1ED1u1ED7u1ED5u00F5u1E4Du022Du1E4Fu014Du1E51u1E53u014Fu022Fu0231u00F6u022Bu1ECFu0151u01D2u020Du020Fu01A1u1EDDu1EDBu1EE1u1EDFu1EE3u1ECDu1ED9u01EBu01EDu00F8u01FFu0254uA74BuA74Du0275]/g}, {'base':'oi','letters':/[u01A3]/g}, {'base':'ou','letters':/[u0223]/g}, {'base':'oo','letters':/[uA74F]/g}, {'base':'p','letters':/[u0070u24DFuFF50u1E55u1E57u01A5u1D7DuA751uA753uA755]/g}, {'base':'q','letters':/[u0071u24E0uFF51u024BuA757uA759]/g}, {'base':'r','letters':/[u0072u24E1uFF52u0155u1E59u0159u0211u0213u1E5Bu1E5Du0157u1E5Fu024Du027DuA75BuA7A7uA783]/g}, {'base':'s','letters':/[u0073u24E2uFF53u00DFu015Bu1E65u015Du1E61u0161u1E67u1E63u1E69u0219u015Fu023FuA7A9uA785u1E9B]/g}, {'base':'t','letters':/[u0074u24E3uFF54u1E6Bu1E97u0165u1E6Du021Bu0163u1E71u1E6Fu0167u01ADu0288u2C66uA787]/g}, {'base':'tz','letters':/[uA729]/g}, {'base':'u','letters':/[u0075u24E4uFF55u00F9u00FAu00FBu0169u1E79u016Bu1E7Bu016Du00FCu01DCu01D8u01D6u01DAu1EE7u016Fu0171u01D4u0215u0217u01B0u1EEBu1EE9u1EEFu1EEDu1EF1u1EE5u1E73u0173u1E77u1E75u0289]/g}, {'base':'v','letters':/[u0076u24E5uFF56u1E7Du1E7Fu028BuA75Fu028C]/g}, {'base':'vy','letters':/[uA761]/g}, {'base':'w','letters':/[u0077u24E6uFF57u1E81u1E83u0175u1E87u1E85u1E98u1E89u2C73]/g}, {'base':'x','letters':/[u0078u24E7uFF58u1E8Bu1E8D]/g}, {'base':'y','letters':/[u0079u24E8uFF59u1EF3u00FDu0177u1EF9u0233u1E8Fu00FFu1EF7u1E99u1EF5u01B4u024Fu1EFF]/g}, {'base':'z','letters':/[u007Au24E9uFF5Au017Au1E91u017Cu017Eu1E93u1E95u01B6u0225u0240u2C6CuA763]/g} ]; for (var i = 0; i < defaultDiacriticsRemovalMap.length; i++) { str = str.replace(defaultDiacriticsRemovalMap[i].letters, defaultDiacriticsRemovalMap[i].base); } return str; }; function MultipleSelect($el, options) { var that = this, name = $el.attr('name') || options.name || ''; this.options = options; // hide select element this.$el = $el.hide(); // label element this.$label = this.$el.closest('label'); if (this.$label.length === 0 && this.$el.attr('id')) { this.$label = $(sprintf('label[for="%s"]', this.$el.attr('id').replace(/:/g, '\:'))); } // restore class and title from select element this.$parent = $(sprintf( '<div class="ms-parent %s" %s/>', $el.attr('class') || '', sprintf('title="%s"', $el.attr('title')))); // add placeholder to choice button this.$choice = $(sprintf([ '<button type="button" class="ms-choice">', '<span class="placeholder">%s</span>', '<div></div>', '</button>' ].join(''), this.options.placeholder)); // default position is bottom this.$drop = $(sprintf('<div class="ms-drop %s"%s></div>', this.options.position, sprintf(' style=" %s"', this.options.dropWidth))); this.$el.after(this.$parent); this.$parent.append(this.$choice); this.$parent.append(this.$drop); if (this.$el.prop('disabled')) { this.$choice.addClass('disabled'); } this.$parent.css('width', this.options.width || this.$el.css('width') || this.$el.outerWidth() + 20); this.selectAllName = 'data-name="selectAll' + name + '"'; this.selectGroupName = 'data-name="selectGroup' + name + '"'; this.selectItemName = 'data-name="selectItem' + name + '"'; if (!this.options.keepOpen) { $(document).click(function (e) { if ($(e.target)[0] === that.$choice[0] || $(e.target).parents('.ms-choice')[0] === that.$choice[0]) { return; } if (($(e.target)[0] === that.$drop[0] || $(e.target).parents('.ms-drop')[0] !== that.$drop[0] && e.target !== $el[0]) && that.options.isOpen) { that.close(); } }); } } MultipleSelect.prototype = { constructor: MultipleSelect, init: function () { var that = this, $ul = $('<ul></ul>'); this.$drop.html(''); if (this.options.filter) { this.$drop.append([ '<div class="ms-search">', '<input type="text" autocomplete="off" autocorrect="off" autocapitilize="off" spellcheck="false">', '</div>'].join('') ); } if (this.options.selectAll && !this.options.single) { $ul.append([ '<li class="ms-select-all">', '<label>', sprintf('<input type="checkbox" %s /> ', this.selectAllName), this.options.selectAllDelimiter[0], this.options.selectAllText, this.options.selectAllDelimiter[1], '</label>', '</li>' ].join('')); } $.each(this.$el.children(), function (i, elm) { $ul.append(that.optionToHtml(i, elm)); }); $ul.append(sprintf('<li class="ms-no-results">%s</li>', this.options.noMatchesFound)); this.$drop.append($ul); this.$drop.find('ul').css('max-height', this.options.maxHeight + 'px'); this.$drop.find('.multiple').css('width', this.options.multipleWidth + 'px'); this.$searchInput = this.$drop.find('.ms-search input'); this.$selectAll = this.$drop.find('input[' + this.selectAllName + ']'); this.$selectGroups = this.$drop.find('input[' + this.selectGroupName + ']'); this.$selectItems = this.$drop.find('input[' + this.selectItemName + ']:enabled'); this.$disableItems = this.$drop.find('input[' + this.selectItemName + ']:disabled'); this.$noResults = this.$drop.find('.ms-no-results'); this.events(); this.updateSelectAll(true); this.update(true); if (this.options.isOpen) { this.open(); } }, optionToHtml: function (i, elm, group, groupDisabled) { var that = this, $elm = $(elm), classes = $elm.attr('class') || '', title = sprintf('title="%s"', $elm.attr('title')), multiple = this.options.multiple ? 'multiple' : '', disabled, type = this.options.single ? 'radio' : 'checkbox'; if ($elm.is('option')) { var value = $elm.val(), text = that.options.textTemplate($elm), selected = $elm.prop('selected'), style = sprintf('style="%s"', this.options.styler(value)), $el; disabled = groupDisabled || $elm.prop('disabled'); $el = $([ sprintf('<li class="%s %s" %s %s>', multiple, classes, title, style), sprintf('<label class="%s">', disabled ? 'disabled' : ''), sprintf('<input type="%s" %s%s%s%s>', type, this.selectItemName, selected ? ' checked="checked"' : '', disabled ? ' disabled="disabled"' : '', sprintf(' data-group="%s"', group)), sprintf('<span>%s</span>', text), '</label>', '</li>' ].join('')); $el.find('input').val(value); return $el; } if ($elm.is('optgroup')) { var label = that.options.labelTemplate($elm), $group = $('<div/>'); group = 'group_' + i; disabled = $elm.prop('disabled'); $group.append([ '<li class="group">', sprintf('<label class="optgroup %s" data-group="%s">', disabled ? 'disabled' : '', group), this.options.hideOptgroupCheckboxes || this.options.single ? '' : sprintf('<input type="checkbox" %s %s>', this.selectGroupName, disabled ? 'disabled="disabled"' : ''), label, '</label>', '</li>' ].join('')); $.each($elm.children(), function (i, elm) { $group.append(that.optionToHtml(i, elm, group, disabled)); }); return $group.html(); } }, events: function () { var that = this, toggleOpen = function (e) { e.preventDefault(); that[that.options.isOpen ? 'close' : 'open'](); }; if (this.$label) { this.$label.off('click').on('click', function (e) { if (e.target.nodeName.toLowerCase() !== 'label' || e.target !== this) { return; } toggleOpen(e); if (!that.options.filter || !that.options.isOpen) { that.focus(); } e.stopPropagation(); // Causes lost focus otherwise  }); } this.$choice.off('click').on('click', toggleOpen) .off('focus').on('focus', this.options.onFocus) .off('blur').on('blur', this.options.onBlur); this.$parent.off('keydown').on('keydown', function (e) { switch (e.which) { case 27: // esc key  that.close(); that.$choice.focus(); break; } }); this.$searchInput.off('keydown').on('keydown',function (e) { // Ensure shift-tab causes lost focus from filter as with clicking away if (e.keyCode === 9 && e.shiftKey) { that.close(); } }).off('keyup').on('keyup', function (e) { // enter or space // Avoid selecting/deselecting if no choices made if (that.options.filterAcceptOnEnter && (e.which === 13 || e.which == 32) && that.$searchInput.val()) { that.$selectAll.click(); that.close(); that.focus(); return; } that.filter(); }); this.$selectAll.off('click').on('click', function () { var checked = $(this).prop('checked'), $items = that.$selectItems.filter(':visible'); if ($items.length === that.$selectItems.length) { that[checked ? 'checkAll' : 'uncheckAll'](); } else { // when the filter option is true that.$selectGroups.prop('checked', checked); $items.prop('checked', checked); that.options[checked ? 'onCheckAll' : 'onUncheckAll'](); that.update(); } }); this.$selectGroups.off('click').on('click', function () { var group = $(this).parent().attr('data-group'), $items = that.$selectItems.filter(':visible'), $children = $items.filter(sprintf('[data-group="%s"]', group)), checked = $children.length !== $children.filter(':checked').length; $children.prop('checked', checked); that.updateSelectAll(); that.update(); that.options.onOptgroupClick({ label: $(this).parent().text(), checked: checked, children: $children.get(), instance: that }); }); this.$selectItems.off('click').on('click', function () { that.updateSelectAll(); that.update(); that.updateOptGroupSelect(); that.options.onClick({ label: $(this).parent().text(), value: $(this).val(), checked: $(this).prop('checked'), instance: that }); if (that.options.single && that.options.isOpen && !that.options.keepOpen) { that.close(); } if (that.options.single) { var clickedVal = $(this).val(); that.$selectItems.filter(function() { return $(this).val() !== clickedVal; }).each(function() { $(this).prop('checked', false); }); that.update(); } }); }, open: function () { if (this.$choice.hasClass('disabled')) { return; } this.options.isOpen = true; this.$choice.find('>div').addClass('open'); this.$drop[this.animateMethod('show')](); // fix filter bug: no results show this.$selectAll.parent().show(); this.$noResults.hide(); // Fix #77: 'All selected' when no options if (!this.$el.children().length) { this.$selectAll.parent().hide(); this.$noResults.show(); } if (this.options.container) { var offset = this.$drop.offset(); this.$drop.appendTo($(this.options.container)); this.$drop.offset({ top: offset.top, left: offset.left }); } if (this.options.filter) { this.$searchInput.val(''); this.$searchInput.focus(); this.filter(); } this.options.onOpen(); }, close: function () { this.options.isOpen = false; this.$choice.find('>div').removeClass('open'); this.$drop[this.animateMethod('hide')](); if (this.options.container) { this.$parent.append(this.$drop); this.$drop.css({ 'top': 'auto', 'left': 'auto' }); } this.options.onClose(); }, animateMethod: function (method) { var methods = { show: { fade: 'fadeIn', slide: 'slideDown' }, hide: { fade: 'fadeOut', slide: 'slideUp' } }; return methods[method][this.options.animate] || method; }, update: function (isInit) { var selects = this.options.displayValues ? this.getSelects() : this.getSelects('text'), $span = this.$choice.find('>span'), sl = selects.length; if (sl === 0) { $span.addClass('placeholder').html(this.options.placeholder); } else if (this.options.allSelected && sl === this.$selectItems.length + this.$disableItems.length) { $span.removeClass('placeholder').html(this.options.allSelected); } else if (this.options.ellipsis && sl > this.options.minimumCountSelected) { $span.removeClass('placeholder').text(selects.slice(0, this.options.minimumCountSelected) .join(this.options.delimiter) + '...'); } else if (this.options.countSelected && sl > this.options.minimumCountSelected) { $span.removeClass('placeholder').html(this.options.countSelected .replace('#', selects.length) .replace('%', this.$selectItems.length + this.$disableItems.length)); } else { $span.removeClass('placeholder').text(selects.join(this.options.delimiter)); } if (this.options.addTitle) { $span.prop('title', this.getSelects('text')); } // set selects to select this.$el.val(this.getSelects()).trigger('change'); // add selected class to selected li this.$drop.find('li').removeClass('selected'); this.$drop.find('input:checked').each(function () { $(this).parents('li').first().addClass('selected'); }); // trigger <select> change event if (!isInit) { this.$el.trigger('change'); } }, updateSelectAll: function (isInit) { var $items = this.$selectItems; if (!isInit) { $items = $items.filter(':visible'); } this.$selectAll.prop('checked', $items.length && $items.length === $items.filter(':checked').length); if (!isInit && this.$selectAll.prop('checked')) { this.options.onCheckAll(); } }, updateOptGroupSelect: function () { var $items = this.$selectItems.filter(':visible'); $.each(this.$selectGroups, function (i, val) { var group = $(val).parent().attr('data-group'), $children = $items.filter(sprintf('[data-group="%s"]', group)); $(val).prop('checked', $children.length && $children.length === $children.filter(':checked').length); }); }, //value or text, default: 'value' getSelects: function (type) { var that = this, texts = [], values = []; this.$drop.find(sprintf('input[%s]:checked', this.selectItemName)).each(function () { texts.push($(this).parents('li').first().text()); values.push($(this).val()); }); if (type === 'text' && this.$selectGroups.length) { texts = []; this.$selectGroups.each(function () { var html = [], text = $.trim($(this).parent().text()), group = $(this).parent().data('group'), $children = that.$drop.find(sprintf('[%s][data-group="%s"]', that.selectItemName, group)), $selected = $children.filter(':checked'); if (!$selected.length) { return; } html.push('['); html.push(text); if ($children.length > $selected.length) { var list = []; $selected.each(function () { list.push($(this).parent().text()); }); html.push(': ' + list.join(', ')); } html.push(']'); texts.push(html.join('')); }); } return type === 'text' ? texts : values; }, setSelects: function (values) { var that = this; this.$selectItems.prop('checked', false); this.$disableItems.prop('checked', false); $.each(values, function (i, value) { that.$selectItems.filter(sprintf('[value="%s"]', value)).prop('checked', true); that.$disableItems.filter(sprintf('[value="%s"]', value)).prop('checked', true); }); this.$selectAll.prop('checked', this.$selectItems.length === this.$selectItems.filter(':checked').length + this.$disableItems.filter(':checked').length); $.each(that.$selectGroups, function (i, val) { var group = $(val).parent().attr('data-group'), $children = that.$selectItems.filter('[data-group="' + group + '"]'); $(val).prop('checked', $children.length && $children.length === $children.filter(':checked').length); }); this.update(); }, enable: function () { this.$choice.removeClass('disabled'); }, disable: function () { this.$choice.addClass('disabled'); }, checkAll: function () { this.$selectItems.prop('checked', true); this.$selectGroups.prop('checked', true); this.$selectAll.prop('checked', true); this.update(); this.options.onCheckAll(); }, uncheckAll: function () { this.$selectItems.prop('checked', false); this.$selectGroups.prop('checked', false); this.$selectAll.prop('checked', false); this.update(); this.options.onUncheckAll(); }, focus: function () { this.$choice.focus(); this.options.onFocus(); }, blur: function () { this.$choice.blur(); this.options.onBlur(); }, refresh: function () { this.init(); }, filter: function () { var that = this, text = $.trim(this.$searchInput.val()).toLowerCase(); if (text.length === 0) { this.$selectAll.parent().show(); this.$selectItems.parent().show(); this.$disableItems.parent().show(); this.$selectGroups.parent().show(); this.$noResults.hide(); } else { this.$selectItems.each(function () { var $parent = $(this).parent(); $parent[removeDiacritics($parent.text().toLowerCase()).indexOf(removeDiacritics(text)) < 0 ? 'hide' : 'show'](); }); this.$disableItems.parent().hide(); this.$selectGroups.each(function () { var $parent = $(this).parent(); var group = $parent.attr('data-group'), $items = that.$selectItems.filter(':visible'); $parent[$items.filter(sprintf('[data-group="%s"]', group)).length ? 'show' : 'hide'](); }); //Check if no matches found if (this.$selectItems.parent().filter(':visible').length) { this.$selectAll.parent().show(); this.$noResults.hide(); } else { this.$selectAll.parent().hide(); this.$noResults.show(); } } this.updateOptGroupSelect(); this.updateSelectAll(); this.options.onFilter(text); } }; $.fn.multipleSelect = function () { var option = arguments[0], args = arguments, value, allowedMethods = [ 'getSelects', 'setSelects', 'enable', 'disable', 'open', 'close', 'checkAll', 'uncheckAll', 'focus', 'blur', 'refresh', 'close' ]; this.each(function () { var $this = $(this), data = $this.data('multipleSelect'), options = $.extend({}, $.fn.multipleSelect.defaults, $this.data(), typeof option === 'object' && option); if (!data) { data = new MultipleSelect($this, options); $this.data('multipleSelect', data); } if (typeof option === 'string') { if ($.inArray(option, allowedMethods) < 0) { throw 'Unknown method: ' + option; } value = data[option](args[1]); } else { data.init(); if (args[1]) { value = data[args[1]].apply(data, [].slice.call(args, 2)); } } }); return typeof value !== 'undefined' ? value : this; }; $.fn.multipleSelect.defaults = { name: '', isOpen: false, placeholder: '', selectAll: true, selectAllDelimiter: ['[', ']'], minimumCountSelected: 3, ellipsis: false, multiple: false, multipleWidth: 80, single: false, filter: false,  undefined, dropWidth: undefined, maxHeight: 250, container: null, position: 'bottom', keepOpen: false, animate: 'none', // 'none', 'fade', 'slide' displayValues: false, delimiter: ', ', addTitle: false, filterAcceptOnEnter: false, hideOptgroupCheckboxes: false, selectAllText: '全选', allSelected: '全部选中', countSelected: '# / % 选中', noMatchesFound: '没有匹配结果', styler: function () { return false; }, textTemplate: function ($elm) { return $elm.html(); }, labelTemplate: function ($elm) { return $elm.attr('label'); }, onOpen: function () { return false; }, onClose: function () { return false; }, onCheckAll: function () { return false; }, onUncheckAll: function () { return false; }, onFocus: function () { return false; }, onBlur: function () { return false; }, onOptgroupClick: function () { return false; }, onClick: function () { return false; }, onFilter: function () { return false; } }; })(jQuery);
    $.get('printer/findOilGunByOilStation.do',{'_':new Date().getTime(),oilStationId:oilStationId},function (data) {
                if(data!=null&&data.length!=0){
                    var data=$.parseJSON(data);
                    $("#oilGun").empty();
                    $("#oilGun").multipleSelect();
                    for(var i=0;i<data.length;i++){
    //                    console.log(data[i].oilGunId+"--"+data[i].oilGunNumber);
                        $("#oilGun").append('<option class="text-left" value="'+data[i].oilGunId+'">'+data[i].oilGunNumber+'号枪</option>');
                        $("#oilGun").multipleSelect();
                    }
                }
            });
    
    <div class="form-group">
                                        <label class="col-sm-4 text-right">枪号</label>
                                        <div class="col-sm-5 text-left">
                                            <select id="oilGun" name="oilGun" style="200px" multiple="multiple" onchange="oilGunChange()">
                                            </select>
                                        </div>
                                    </div>
    
    function oilGunChange(callback) {
            //console.log($("#oilGun").val());
            var oilGunIdArr=$("#oilGun").val();
            var $node=null;
            if($("#my_check_id").size()==0){
                $node=$("#oilGun").next().clone(true);
                $node.attr("id","my_check_id");
                $("#oilGun").parent().append($node);
            }else{
                $node=$("#my_check_id");
            }
            if(oilGunIdArr==null){
                $node.text("枪号为空,请选择");
                $node.attr("style","display:block;color:#D15E5E");
                $node.attr("data-bv-result","INVALID");
                $node.attr("data-bv-validator","xxx");
            }else{
                $node.attr("style","display:none");
                $node.attr("data-bv-result","VALID");
                if(callback){
                    callback();
                }
            }
        }
    

      修改js源代码,为业务服务

  • 相关阅读:
    VS2005在使用membership的时候,如何连接Access数据库?
    今天想开始写计划的项目,可是就是静不下心来,乱糟糟的!
    今天想开始写计划的项目,可是就是静不下心来,乱糟糟的!
    有钱真好
    网页左边和上面的空隙如何设置成为0
    vim 配色方案(目测有上百个)
    Git 远程仓库的管理和使用
    vim 使用图
    Python 编程挑战
    python 网络爬虫
  • 原文地址:https://www.cnblogs.com/liangmm/p/11647575.html
Copyright © 2011-2022 走看看