A customized combobox with JQuery
要求实现一个轻量级的在客户端筛选的combobox,支持大数据量(超过1000个items),能快速检索内容,并支持数据的设置和活动等基本操作。在这之前尝试过使用Jquery UI的Autocomplete,但是当数据量太大时客户端检索速度太慢(甚至会导致浏览器卡死)。索性干脆基于JQuery自己写一个吧!
所依赖的库:
CSS:
body {
font-size: 14px;
font-family: "microsoft yahei", Verdana, Arial, sans-serif, "Times New Roman";
max-width: 500px;
margin: 0 auto;
-webkit-text-size-adjust: none;
}
.combobox-menu {
background-color: #fff;
border: 1px solid #ccc;
min-height: 100px;
overflow-y: auto;
font-size: 0.875rem;
padding:;
}
.combobox-menu li {
list-style: none;
padding: .625em 1em;
cursor: pointer;
}
.combobox-menu li.selected {
background-color: #337ab7;
color: #fff;
}
.combobox-menu li.normal:hover {
background-color: #d9edf7;
}
.combobox-menu-dropdown {
position: absolute;
z-index:;
-webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.175);
}
.combobox-menu-caret {
display: inline-block;
width:;
height:;
margin-left: 2px;
vertical-align: middle;
border-top: 4px dashed;
border-right: 4px solid transparent;
border-left: 4px solid transparent;
position: absolute;
right: 8px;
top: 50%;
}
JavaScript:
/* event wrapper */
var eventWrapper = function () {
this.init();
}; eventWrapper.prototype.init = function () {
this.events = {};
}; eventWrapper.prototype.on = function (key, func) {
if (func instanceof Function) {
if (!this.events[key]) {
this.events[key] = [func];
} else {
this.events[key].push(func);
}
return this;
}
}; eventWrapper.prototype.emmit = function (key) {
if (this.events[key]) {
var _list = this.events[key];
var that = this;
var args = arguments;
[].shift.apply(args);
for (var i = 0, ci; ci = _list[i]; i++) {
ci.apply(that, args);
}
}
}; eventWrapper.prototype.remove = function (key) {
delete this.events[key];
}; /*********************/ /* combobox控件 */
var comboBox = function (element, options) {
var def = {
menuStyle: {maxHeight: 180},
readonly: false,
data: []
};
this.opts = $.extend(true, def, options);
var that = this; this.event = new eventWrapper();
this.source = [];
this.selected = null;
this.$source = $(element);
if (this.opts.readonly) {
this.$source.attr('readonly', 'readonly');
}
this.$source.on('input paste', function () {
that.changeText($(this));
});
this.$container = $('<div style="position:relative;"></div>');
// this.$caret = $('<span class="combobox-menu-caret"></span>');
this.$menu = $('<ul class="combobox-menu"></ul>');
this.$lis = null;
this.$menu.on('click', 'li', function () {
if (!that.opts.readonly) {
that.select($(this).attr('data-value'));
}
});
this.init();
}; comboBox.prototype.init = function () {
if (this.opts.data && this.opts.data.length > 0) {
this.$source.after(this.$menu);
// this.render(this.opts.data);
this.$menu.css('minWidth', this.$source.outerWidth());
this.$lis = this.$menu.find('li');
this.setMenuStyle(); var that = this;
this.$menu.addClass('combobox-menu-dropdown').hide();
this.$source.wrap(that.$container);
// this.$caret.insertAfter(that.$source); if (!this.opts.readonly) {
that.$source.on('click', function () {
if ($(this).val().trim().length === 0) return;
that.expandMenu(that);
}); // 添加关闭下拉列表事件
$(document).click(function (e) {
var target = e.target;
if (!$(target).is(that.$source) && !$(target).parents().is(that.$menu)) {
that.$menu.hide();
if (that.getSelected() && that.$source.val().trim().length === 0) {
that.select(that.getSelected().val);
}
}
});
}
}
}; comboBox.prototype.render = function (data) {
for (var i = 0, ci; ci = data[i]; i++) {
var _item = $('<li></li>');
_item.attr('data-value', ci.val);
_item.text(ci.txt);
if (ci.selected) {
this.selected = ci;
}
this.$menu.append(_item);
}
}; comboBox.prototype.update = function (data) {
this.$menu.empty();
if (data) {
this.render(data);
}
}; comboBox.prototype.expandMenu = function () {
if (this.$menu.is(':hidden')) {
this.$menu.show();
}
}; comboBox.prototype.setMenuStyle = function () {
if (this.opts.menuStyle) {
this.$menu.css(this.opts.menuStyle);
}
}; comboBox.prototype.select = function (val) {
if (!val) return;
var item = _.find(this.opts.data, {val: val});
if (item) {
this.selected = item;
this.update([this.selected]);
this.$source.val(item.txt);
this.$source.attr('data-value', item.val);
this.$menu.hide();
this.event.emmit('change', item);
}
}; comboBox.prototype.changeText = function (item) {
var _data = null;
this.expandMenu();
var _txt = item.val();
if ($.trim(_txt).length > 0) {
var reg = new RegExp(_txt, 'i');
_data =_.filter(this.opts.data, function (o) {
return reg.test(o.txt);
});
}
this.update(_data);
}; comboBox.prototype.getSelected = function () {
return this.selected;
}; comboBox.prototype.setSelected = function (val) {
this.select(val);
}; comboBox.prototype.onChange = function (fn) {
this.on('change', fn);
}; comboBox.prototype.on = function (key, fn) {
this.event.on(key, fn);
};
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>comboBox</title>
<link rel="stylesheet" href="bootstrap.min.css"/>
<link rel="stylesheet" href="style.css"/>
<script src="jquery.min.js"></script>
<script src="underscore-min.js"></script>
<script src="comboBox.js"></script>
</head>
<body>
<form>
<div class="form-group">
<label for="sel_mct">选择项:</label>
<input type="text" class="form-control" value="" id="sel_mct" placeholder="输入名称过滤">
<input type="hidden" id="mct" name="mct"/>
</div>
</form>
<script type="text/javascript">
$(function () {
var _combox = new comboBox($('#sel_mct'), {
menuStyle: {textAlign: 'left'},
data: [
{val: '1', txt: 'Microsoft'},
{val: '2', txt: 'Google'},
{val: '3', txt: 'Facebook'},
{val: '4', txt: 'Twitter'},
{val: '5', txt: 'Apple Inc.'},
{val: '6', txt: 'Amazon.cn'},
{val: '7', txt: 'Adobe'},
{val: '8', txt: 'Oracle'},
{val: '9', txt: 'LinkedIn'},
{val: '10', txt: 'Alibaba'}
]
});
_combox.on('change', function () {
$('#mct').val(_combox.getSelected().val);
});
_combox.setSelected('2');
});
</script>
</body>
</html>
主要方法:
- comboBox构造函数,接受一个JQuery对象作为宿主HTML元素,该元素为页面上的input标签。另外一个参数指定comboBox的配置项,包括:
- menuStyle:指定下拉框的样式,默认样式有maxHeight: 180. 所有样式均以JQuery css的方式进行指定。
- readonly:Boolean类型,是否为只读。
- data:指定数据源。数据源为JSON数组,其中每一个元素是带有val和txt属性的JSON,val表示item的值,txt表示item的显示文本。
- setSelected,用于在comboBox初始化完毕之后,设置默认选中的元素。接受一个字符串变量,用于表示选中item的值。
- getSelected,获取选中的item,将返回一个JSON对象,包含val和txt属性。
- onChange事件,当comboBox中的item被选中时触发。
完整示例下载:comboBox.zip
另外推荐一个好用的select控件,功能很强大!
A customized combobox with JQuery的更多相关文章
- js简单显示和隐藏div,触发超链接,动态更改button值,setInterval()简单使用,jquery easyui弹出框简单使用 .
js简单显示和隐藏div .<!DOCTYPE html> .<html> .<head> .<meta charset="UTF-8"& ...
- comboBox的多选框之疑难杂症——逗号篇
提笔写正文之前,首先要再次提醒一下自己,因为总是记不住,以至大神同事们都开始用“嫌弃”的眼光看自己了——遇到问题,自己去解决,没有什么问题是解决不掉的,不要在没认真努力思考之前就去麻烦大神同事,切记切 ...
- (一)jQuery EasyUI 的EasyLoader载入原理
1.第一次看了官网的demo.引用的是EasyLoader.js文件,而不是引用jquery.easyui.min.js文件,我就有疑问了,百度一下. jQuery EasyUI是一款基于JQuery ...
- easyui comboBox的多选框之疑难杂症——逗号篇
提笔写正文之前,首先要再次提醒一下自己,因为总是记不住,以至大神同事们都开始用"嫌弃"的眼光看自己了-- 记得使用easyui中的combobox吗?效果是酱紫滴: ...
- C#进阶系列——一步一步封装自己的HtmlHelper组件:BootstrapHelper(三:附源码)
前言:之前的两篇封装了一些基础的表单组件,这篇继续来封装几个基于bootstrap的其他组件.和上篇不同的是,这篇的有几个组件需要某些js文件的支持. 本文原创地址:http://www.cnblog ...
- nodejs 使用fs实现多级联动
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0gAAAEdCAIAAAC5WdDhAAAgAElEQVR4nO3da3Mc153f8X4feq5lFR
- JS组件系列——使用HTML标签的data属性初始化JS组件
前言:最近使用bootstrap组件的时候发现一个易用性问题,很多简单的组件初始化都需要在JS里面写很多的初始化代码,比如一个简单的select标签,因为仅仅只是需要从后台获取数据填充到option里 ...
- 3.EasyUI学习总结(三)——easyloader源码分析
easyloader模块是用来加载jquery easyui的js和css文件的,即easyloader可以在调用的时候自动加载当前页面所需的文件,不用再自己引用, 而且它可以分析模块的依赖关系,先加 ...
- easyloader源码
/** * easyloader - jQuery EasyUI * * Licensed under the GPL: * http://www.gnu.org/licenses/gpl.txt * ...
随机推荐
- #Java学习之路——基础阶段(第十一篇)
我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...
- 【机器学习】--模型评估指标之混淆矩阵,ROC曲线和AUC面积
一.前述 怎么样对训练出来的模型进行评估是有一定指标的,本文就相关指标做一个总结. 二.具体 1.混淆矩阵 混淆矩阵如图: 第一个参数true,false是指预测的正确性. 第二个参数true,p ...
- Vue.js-03:第三章 - 事件修饰符的使用
一.前言 熟悉了 Vue 的指令系统后,在实际开发中,不可避免的会使用到对于事件的操作,如何处理 DOM 事件流,成为我们必须要掌握的技能.不同于传统的前端开发,在 Vue 中给我们提供了事件修饰符这 ...
- Spring之旅第二篇-Spring IOC概念及原理分析
一.IOC概念 上一篇已经了解了spring的相关概念,并且创建了一个Spring项目.spring中有最重要的两个概念:IOC和AOP,我们先从IOC入手. IOC全称Inversion of Co ...
- Harbor配置https认证
Harbor配置https认证由于Harbor不附带任何证书,它默认使用HTTP来提供注册表请求.但是,强烈建议为任何生产环境启用安全性.因为测试使用,使用自签名证书: 1.创建CA证书 首先创建个目 ...
- XML的创建、解析-C语言
前言:今天在做一个小项目时,客户要求的xml,跟现在有系统要求的不一样,所以要自己重新写函数支持返回,进行简单总结,希望对大家有所帮助. 首先,使用xml函数需要链上动态库libxml2,需要在电脑上 ...
- WebApiClient的接口输入验证
1. 文章目的 随着WebApiClient的不断完善,越来越多开发者选择WebApiClient替换原生的HttpClient,本文将介绍WebApiClient的接口参数输入有效性验证的新特性. ...
- SpringBoot系列——Redis
前言 Redis是一个缓存.消息代理和功能丰富的键值存储.StringBoot提供了基本的自动配置.本文记录一下springboot与redis的简单整合实例 官方文档:https://docs.sp ...
- keil进阶教程
前言 keil只懂得创建软件工程是远远不够的,如果要想顺心使用,应该要懂得部分配置,这样使用心情顺畅,码代码也会越发高效. 设置字号字体 编辑点击编辑菜单,会出现很多子目录,找到配置,点击进入设置页面 ...
- c# 正则表达式替换字符串中常见的特殊字符
第一种,若字符串中含有字母,则使用以下方法 public static string RemoveSpecialCharacterToupper(string hexData) { //下文中的‘\\ ...