多选下拉框(select 下拉多选)

您所在的位置:网站首页 js修改select 多选下拉框(select 下拉多选)

多选下拉框(select 下拉多选)

2023-12-25 06:44| 来源: 网络整理| 查看: 265

(function($) {

$.fn.fSelect = function(options) {

if (typeof options == 'string' ) { var settings = options; } else { //默认配置项 var settings = $.extend({ placeholder: ' ===请选择=== ',//占位符,默认显示什么 numDisplayed: 10,//设置页面上可以显示几个选项(超出这个数量将会显示选择了n项) overflowText: '选择了{n}项',//超出这个数量将会显示"选择了n项" searchText: '搜索',//搜索框中占位符显示什么字 showSearch: true//是否显示搜索框 }, options); }

/** * Constructor */ function fSelect(select, settings) { this.$select = $(select); this.settings = settings; this.create(); }

/** * Prototype class */ fSelect.prototype = { create: function() { var multiple = this.$select.is('[multiple]') ? ' multiple' : ''; this.$select.wrap(''); this.$select.before('' + this.settings.placeholder + ''); this.$select.before(''); // this.$select.addClass('hidden'); this.$select.addClass('visibility-hidden'); this.$wrap = this.$select.closest('.fs-wrap'); //添加全选和清空按钮 var BtnFixed = '全选清空'; this.$wrap.find('.fs-dropdown').append(BtnFixed); this.reload(); },

reload: function() { //如果设置显示搜索框,执行下面方法 if (this.settings.showSearch) { var search = ''; this.$wrap.find('.fs-dropdown').prepend(search); } var choices = this.buildOptions(this.$select); //获取到所有option选项 this.$wrap.find('.fs-options').append(choices);

//选中当前已选的值 var valArr = this.$select.val(); // if(valArr.length){ if(valArr){ var $fsOption = this.$wrap.find('.fs-option'); valArr.forEach(function(currentValue){ $fsOption.each(function(){ if(currentValue == $(this).attr('data-value')){ $(this).addClass("selected"); } }); }); } //获取到选中状态的选项 this.reloadDropdownLabel(); },

destroy: function() { this.$wrap.find('.fs-label-wrap').remove(); this.$wrap.find('.fs-dropdown').remove(); // this.$select.unwrap().removeClass('hidden'); this.$select.unwrap().removeClass('visibility-hidden'); }, //获取select的option选项,并赋值到插件自定义的每一选项div里 buildOptions: function($element) { var $this = this;

var choices = ''; $element.children().each(function(i, el) { var $el = $(el); //支持 optiongroup if ('optgroup' == $el.prop('nodeName').toLowerCase()) { choices += ''; choices += '' + $el.prop('label') + ''; choices += $this.buildOptions($el); choices += ''; } else { var selected = $el.is('[selected]') ? ' selected' : ''; choices += '' + $el.html() + ''; } });

return choices; }, //每次加载时判断一下插件自定义的每一选项div哪些是选中状态,将选中状态对应的文字,push到一个数组labelText里,页面上显示出来的文字就是数组中获取到的文字 reloadDropdownLabel: function() { var settings = this.settings; var labelText = [];

this.$wrap.find('.fs-option.selected').each(function(i, el) { labelText.push($(el).find('.fs-option-label').text()); });

if (labelText.length < 1) { labelText = settings.placeholder; } else if (labelText.length > settings.numDisplayed) { labelText = settings.overflowText.replace('{n}', labelText.length); } else { labelText = labelText.join(', '); }

this.$wrap.find('.fs-label').html(labelText); this.$wrap.find('.fs-label').attr("title",labelText); this.$select.change(); } }

/** * Loop through each matching element */ return this.each(function() { var data = $(this).data('fSelect');

if (!data) { data = new fSelect(this, settings); $(this).data('fSelect', data); }

if (typeof settings == 'string') { data[settings](); } }); } /** * Events */ window.fSelect = { 'active': null, 'idx': -1 };

function setIndexes($wrap) { $wrap.find('.fs-option:not(.hidden)').each(function(i, el) { $(el).attr('data-index', i); $wrap.find('.fs-option').removeClass('hl'); }); $wrap.find('.fs-search input').focus(); window.fSelect.idx = -1; }

function setScroll($wrap) { var $container = $wrap.find('.fs-options'); var $selected = $wrap.find('.fs-option.hl');

var itemMin = $selected.offset().top + $container.scrollTop(); var itemMax = itemMin + $selected.outerHeight(); var containerMin = $container.offset().top + $container.scrollTop(); var containerMax = containerMin + $container.outerHeight();

if (itemMax > containerMax) { // scroll down var to = $container.scrollTop() + itemMax - containerMax; $container.scrollTop(to); } else if (itemMin < containerMin) { // scroll up var to = $container.scrollTop() - containerMin - itemMin; $container.scrollTop(to); } } //插件选项的点击事件 $(document).on('click', '.fs-option', function() { var $wrap = $(this).closest('.fs-wrap');

if ($wrap.hasClass('multiple')) { var selected = [];

$(this).toggleClass('selected'); $wrap.find('.fs-option.selected').each(function(i, el) { selected.push($(el).attr('data-value')); }); } else { var selected = $(this).attr('data-value'); $wrap.find('.fs-option').removeClass('selected'); $(this).addClass('selected'); $wrap.find('.fs-dropdown').hide(); } $wrap.find('select').val(selected); $wrap.find('select').fSelect('reloadDropdownLabel'); });

$(document).on('keyup', '.fs-search input', function(e) { if (40 == e.which) { $(this).blur(); return; }

var $wrap = $(this).closest('.fs-wrap'); var keywords = $(this).val();

$wrap.find('.fs-option, .fs-optgroup-label').removeClass('hidden');

if ('' != keywords) { $wrap.find('.fs-option').each(function() { var regex = new RegExp(keywords, 'gi'); if (null === $(this).find('.fs-option-label').text().match(regex)) { $(this).addClass('hidden'); } });

$wrap.find('.fs-optgroup-label').each(function() { var num_visible = $(this).closest('.fs-optgroup').find('.fs-option:not(.hidden)').length; if (num_visible < 1) { $(this).addClass('hidden'); } }); }

setIndexes($wrap); }); //显示隐藏下拉选项 $(document).on('click', function(e) { var $el = $(e.target); var $wrap = $el.closest('.fs-wrap');

if (0 < $wrap.length) { if ($el.hasClass('fs-label')) { window.fSelect.active = $wrap; var is_hidden = $wrap.find('.fs-dropdown').hasClass('hidden'); $('.fs-dropdown').addClass('hidden');

if (is_hidden) { $wrap.find('.fs-dropdown').removeClass('hidden'); } else { $wrap.find('.fs-dropdown').addClass('hidden'); }

setIndexes($wrap); } } else { $('.fs-dropdown').addClass('hidden'); window.fSelect.active = null; } });

$(document).on('keydown', function(e) { var $wrap = window.fSelect.active;

if (null === $wrap) { return; } else if (38 == e.which) { // up e.preventDefault();

$wrap.find('.fs-option').removeClass('hl');

if (window.fSelect.idx > 0) { window.fSelect.idx--; $wrap.find('.fs-option[data-index=' + window.fSelect.idx + ']').addClass('hl'); setScroll($wrap); } else { window.fSelect.idx = -1; $wrap.find('.fs-search input').focus(); } } else if (40 == e.which) { // down e.preventDefault();

var last_index = $wrap.find('.fs-option:last').attr('data-index'); if (window.fSelect.idx < parseInt(last_index)) { window.fSelect.idx++; $wrap.find('.fs-option').removeClass('hl'); $wrap.find('.fs-option[data-index=' + window.fSelect.idx + ']').addClass('hl'); setScroll($wrap); } } else if (32 == e.which || 13 == e.which) { // space, enter $wrap.find('.fs-option.hl').click(); } else if (27 == e.which) { // esc $('.fs-dropdown').addClass('hidden'); window.fSelect.active = null; } });

//=========================全选===================================== $(document).on('click','#selectAllBtn', function( ) { var $wrap = $(this).closest('.fs-wrap');

if ($wrap.hasClass('multiple')) { var selected = [];

$wrap.find(".fs-option").addClass('selected'); $wrap.find('.fs-option.selected').each(function(i, el) { selected.push($(el).attr('data-value')); }); } $wrap.find('select').val(selected); $wrap.find('select').fSelect('reloadDropdownLabel'); }); //=========================全不选/清空===================================== $(document).on('click','#clearAllBtn', function( ) { var $wrap = $(this).closest('.fs-wrap');

if ($wrap.hasClass('multiple')) { var selected = [];

$wrap.find(".fs-option").removeClass('selected'); $wrap.find('.fs-option.selected').each(function(i, el) { selected.push($(el).attr('data-value')); }); } $wrap.find('select').val(selected); $wrap.find('select').fSelect('reloadDropdownLabel'); });

})(jQuery);



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3