纯手工打造dropdownlist控件
先上图吧,看看效果。
JS代码:
; (function ($) {
var DropdownList = function (oDataSouce, oControlsContainer, oListContainer) {
this._DataSouce = typeof (oDataSouce) == "object" ? oDataSouce : $.parseJSON(oDataSouce);
this._parentElement = oControlsContainer;//容器控件对象
this._oSelectInputDiv;//选择后显示内容控件
this._ListContainer = oListContainer;//下拉列表控件对象
this._ListContainerClick = false;//判断列表是否被点击过
this._ListPostionLeft;//列表左边的距离
this._ListPostionTop;//列表上边的距离
this._IsInited = false;//是否已经初始化数据
this._MouseoutElement = false;//判断鼠标是否离开某个元素
this._RemberMouseClickNum = 1;////记录鼠标点击下拉时的次数;
this._ArrayCollection = new Array;//多选后用于存放结果; this.AllowShowCheckBox = false;//是否允许多选
this.ControlsContainerWidth = 200;//默认控件宽度为200像素
}; DropdownList.prototype = {
InitControl: function () {
var _oSelf = this;
var _oSelectBorderDiv = $("<div>", { "class": "selectBorder", style: "width:" + _oSelf.ControlsContainerWidth + "px;" }).appendTo(this._parentElement);
_oSelectBorderDiv.mouseover(function () { $(this).css({ border: "1px solid blue" }) }).mouseout(function () { $(this).css({ border: "1px solid black" }) });
_oSelf._oSelectInputDiv = $("<div>", { "class": "selectInput", style: "width:" + (_oSelf.ControlsContainerWidth - 10) + "px;" }).appendTo($(_oSelectBorderDiv));
var _oSelectImgDiv = $("<div>", { "class": "selectImg" }).appendTo($(_oSelectBorderDiv));
var _oSelectImg = $("<img>", {
src: "images/arrow.gif", style: "cursor:pointer",
click: function () {
var _oShowList = { width: _oSelectBorderDiv.width(), border: " 1px solid gray", display: "" };
if (!_oSelf._IsInited) {
_oSelf._InitData();
_oSelf._ListPostionLeft = _oSelectBorderDiv.position().left;
_oSelf._ListPostionTop = _oSelectBorderDiv.position().top + _oSelectBorderDiv.height() + 10;
_oSelf._ListContainer.css(_oShowList);
_oSelf._ListContainer.offset({ top: _oSelf._ListPostionTop, left: _oSelf._ListPostionLeft });
_oSelf._IsInited = true;
_oSelf._RemberMouseClickNum += 1;
} else {
if (_oSelf._RemberMouseClickNum % 2 == 0) {
_oSelf._MouseoutElement = true;
_oSelf._RemberMouseClickNum = 1;
} if (_oSelf._ListContainerClick) {
_oSelf._RemberMouseClickNum += 1;
_oSelf._MouseoutElement = false;
_oSelf._BindDocumentEvent();
_oSelf._ListContainer.css(_oShowList);
_oSelf._ListContainerClick = false;
}
else {
_oSelf._RemberMouseClickNum += 1;
_oSelf._BindDocumentEvent();
_oSelf._ListContainer.css(_oShowList);
}
} _oSelf._SetListboxSelectedStatus(_oSelf.AllowShowCheckBox);
}, mouseout: function () {
_oSelf._MouseoutElement = true;
_oSelf._RemberMouseClickNum += 1;
}
}).appendTo(_oSelectImgDiv);
},
_InitData: function () {
var _oSelf = this;
var _oSelectCollection;
for (var i = 0, _iDataCpount = _oSelf._DataSouce.length; i < _iDataCpount; i++) {
var _oDiv = $("<div>", {
"class": "lists", id: "div_" + i, selected: "false", selectedIndex: "" + i + "", title: "" + this._DataSouce[i].text + "", click: function () {
if (_oSelf.AllowShowCheckBox) {
_oSelf._BindListboxChecboxClickEvent(this);
} else { _oSelf._GetListboxText(this); }
}, mouseout: function () { _oSelf._MouseoutElement = true; }
}).mouseover(function () {
_oSelf._BindMouseoverEvent($(this));
_oSelf._BindDocumentEvent();
}).mouseout(function () {
_oSelf._BindMouseoutEvent($(this));
});
this._ListContainer.append(_oDiv);
if (_oSelf.AllowShowCheckBox) {
_oDiv.append($("<input>", {
type: "checkbox", id: "checkbox_" + i, "for": "label_" + i, click: function (oCheck) {
var _sID = oCheck.srcElement.id || oCheck.tagName.id;
var _oParentDiv = $("#" + _sID).parent();
_oSelf._BindListboxChecboxClickEvent(_oParentDiv);
}
}));
} _oDiv.append($("<label>", { id: "label_" + i, key: "" + this._DataSouce[i].value + "" }).html(this._DataSouce[i].text));
} if (_oSelf.AllowShowCheckBox) {
_oSelf._CreateListboxFoot();
}
},
_BindMouseoverEvent: function (oDiv) {
oDiv.mouseover(function () {
if ($(this).attr("selected") != "selected") {
$(this).css({ backgroundColor: "#3A8FCF" })
}
});
},
_BindMouseoutEvent: function (oDiv) {
oDiv.mouseout(function () {
if ($(this).attr("selected") != "selected") {
$(this).css({ backgroundColor: "#fff" })
}
});
},
_BindCheckboxClickEvent: function (oCheckBox, oDiv) {
_oSelf = this; if (!oCheckBox[0].checked) {
oCheckBox[0].checked = true; oDiv.css({ backgroundColor: "#3A8FCF" }).unbind("mouseover").unbind("mouseout");
} else {
oCheckBox[0].checked = false;
oDiv.css({ backgroundColor: "#fff" }).bind("mouseover", _oSelf._BindMouseoverEvent(oDiv)).bind("mouseout", _oSelf._BindMouseoutEvent(oDiv));
}
},
_BindListboxChecboxClickEvent: function (oDiv) {
var _oSelf = this;
var _oParentContainer = $(oDiv);
var _oCheckBoxElement = _oParentContainer.children().first();
var _oLabelElement = _oParentContainer.find("label");
_oSelf._BindCheckboxClickEvent(_oCheckBoxElement, $(oDiv));
_oSelf._MouseoutElement = false;
_oSelectCollection = { key: _oLabelElement.attr("key"), value: _oLabelElement.html(), selectedIndex: $(oDiv).attr("selectedIndex") };
_oSelf._ArrayCollection.push(_oSelectCollection);
},
_BindDocumentEvent: function () {
var _oSelf = this;
$(document).click(function () {
if (_oSelf._MouseoutElement) {
_oSelf._ListContainer.css({ display: "none" });
_oSelf._MouseoutElement = false;
_oSelf._RemberMouseClickNum = 1;
if (_oSelf.AllowShowCheckBox) {
if (_oSelf._oSelectInputDiv.html() == "") {
_oSelf._SetCheckboxSelectedStatus(false, "#fff");
_oSelf._ArrayCollection = new Array;
}
}
}
});
},
_GetListboxText: function (oDiv) {
var _oSelf = this;
var _oLabelElement = $(oDiv).find("label");
var _sSelectedText = _oLabelElement.html();
var _sSelectedValue = _oLabelElement.attr("key");
var _oDisplayText = _oSelf._oSelectInputDiv;
var _iSelectedIndex = $(oDiv).attr("selectedIndex");
_oDisplayText.html(_sSelectedText);
_oDisplayText.attr({ "key": _sSelectedValue, "selected": true, "selectedIndex": _iSelectedIndex, title: "" + _sSelectedText + "" });
_oSelf._SetListboxDisplayStatus();
},
_SetListboxDisplayStatus: function () {
var _oSelf = this;
_oSelf._ListContainer.css({ display: "none" });
_oSelf._ListContainerClick = true;
_oSelf._MouseoutElement = false;
},
_SetListboxSelectedStatus: function (bAllowShowCheckBox) {
var _oSelf = this;
if (bAllowShowCheckBox) {
if (_oSelf._ArrayCollection.length > 0) {
_oSelf._SetCheckboxSelectedStatus(true, "#3A8FCF")
}
} else {
var _sCurrentSelectedText = _oSelf._oSelectInputDiv.attr("selectedIndex");
for (var libIndex = 0, libLen = _oSelf._ListContainer.children().length; libIndex < libLen; libIndex++) {
var _oDiv = _oSelf._ListContainer.children().eq(libIndex);
if (_oDiv.attr("selectedIndex") == _sCurrentSelectedText) {
_oDiv.attr("selected", true).css({ background: "#3A8FCF" });
} else { _oDiv.attr("selected", false).css({ background: "#fff" }); }
}
}
},
_SetCheckboxSelectedStatus: function (bChecked, sColor) {
var _oSelf = this;
for (var _iAcIndex = 0, _iAcLen = _oSelf._ArrayCollection.length; _iAcIndex < _iAcLen; _iAcIndex++) {
var _oDiv = _oSelf._ListContainer.children().eq(_oSelf._ArrayCollection[_iAcIndex].selectedIndex);
_oDiv.find("input[type=checkbox]")[0].checked = bChecked;
_oDiv.css({ background: sColor });
}
}, _CreateListboxFoot: function () {
var _oSelf = this;
$("<div>", { "class": "foot", selectedIndex: "9999" }).append(
$("<img>", {
src: "images/ok.gif", click: function () {
var _sRsult = "";
for (var _iAcIndex = 0, _iAcLen = _oSelf._ArrayCollection.length; _iAcIndex < _iAcLen; _iAcIndex++) {
if (_iAcIndex == 0) {
_sRsult += _oSelf._ArrayCollection[_iAcIndex].value + ";";
}
if (_iAcIndex == _iAcLen - 1) {
_sRsult += _oSelf._ArrayCollection[_iAcIndex].value;
}
}
_oSelf._oSelectInputDiv.html(_sRsult);
_oSelf._MouseoutElement = false;
_oSelf._ListContainer.css({ display: "none" });
}
})).append($("<img>", {
src: "images/cancle.gif", click: function () {
_oSelf._SetCheckboxSelectedStatus(false, "#fff");
_oSelf._ArrayCollection = new Array();
_oSelf._MouseoutElement = true;
_oSelf._BindDocumentEvent();
_oSelf._oSelectInputDiv.html("");
}
})).appendTo(_oSelf._ListContainer);
},
};
$.extend(true, window, {
Controls: { DropdownList: DropdownList }
})
}(jQuery));
下面是HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/JsDropdownList.css" rel="stylesheet" />
<script src="Js/jquery-1.8.3.min.js"></script>
<script src="Js/JsDropdownList.js"></script>
<script type="text/javascript">
$(function () {
var _oDropdownList = new Controls.DropdownList([{ "value": , "text": "测试测试测试测试测试测试1" }, { "value": , "text": "测试测试测试测试测试测试2" }, { "value": , "text": "测试3" }, { "value": , "text": "测试4" }], $("#div_Container"), $("#div_list"));
_oDropdownList.AllowShowCheckBox = true;
_oDropdownList.InitControl();
});
</script>
</head>
<body>
<table>
<tr>
<td>选择:</td>
<td>
<div id="div_Container">
</div>
</td>
</tr>
<tr>
<td>选择:</td>
<td>
<div id="div1" style="float: left;">
<input id="Text2" type="text" />
</div>
</td>
</tr>
<tr>
<td>选择:</td>
<td>
<div id="div2" style="float: left;">
<input id="Text1" type="text" />
</div>
</td>
</tr>
</table>
<div id="div_list" style="display: none; overflow: hidden; padding: 0px; margin: 0px;">
</div>
</body>
</html>
还是那句话,表喷啊。 希望各位给点宝贵的意见, 第一次弄这个东西,还不太会。 见谅!!
后续打算写一个grid表格控件,并结合这个下拉框控件和后台处理程序。grid还在构思中。不知道咋个插入代码下载连接,需要代码的童靴可以留个邮箱吧, 或者直接拷贝上面的代码。
纯手工打造dropdownlist控件的更多相关文章
- 纯手工打造漂亮的瀑布流,五大插件一个都不少Bootstrap+jQuery+Masonry+imagesLoaded+Lightbox!
前两天写的文章<纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!>受到很多网友的喜爱,今天特别推出姊妹篇<纯手工打造漂亮的瀑 ...
- [置顶] 纯手工打造漂亮的瀑布流,五大插件一个都不少Bootstrap+jQuery+Masonry+imagesLoaded+Lightbox!
前两天写的文章<纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!>受到很多网友的喜爱,今天特别推出姊妹篇<纯手工打造漂亮的瀑 ...
- DropDownList控件
1.DropDownList控件 <asp:DropDownList runat="server" ID="DropDownList1" AutoPost ...
- DropDownList 控件不能触发SelectedIndexChanged 事件
相信DropDownList 控件不能触发SelectedIndexChanged 事件已经不是什么新鲜事情了,原因也无外乎以下几种: 1.DropDownList 控件的属性 AutoPostBac ...
- 三级联动---DropDownList控件
AutoPostBack属性:意思是自动回传,也就是说此控件值更改后是否和服务器进行交互比如Dropdownlist控件,若设置为True,则你更换下拉列表值时会刷新页面(如果是网页的话),设置为fl ...
- c#中DropDownList控件绑定枚举数据
c# asp.net 中DropDownList控件绑定枚举数据 1.枚举(enum)代码: private enum heros { 德玛 = , 皇子 = , 大头 = , 剑圣 = , } 如果 ...
- DropDownList 控件
今天打算学习下dropdownlist控件的取值,当你通过数据库控件或dataset绑定值后,但又希望显示指定的值,这可不是简单的值绑定就OK,上网搜了一些资料,想彻底了解哈,后面发现其中有这么大的奥 ...
- DropDownList控件学习
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 客户端用JavaScript填充DropDownList控件 服务器端读不到值
填充没有任何问题,但是在服务器端却取不出来下拉表中的内容.页面代码如下. <form id="form1" runat="server"> < ...
随机推荐
- 佛山Uber优步司机奖励政策(2月1日~2月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- PAT 1070. Mooncake (25)
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
- ACM 关于数据输入加速
转载请注明出处:http://blog.csdn.net/a1dark 分析:我们都知道运行时间对我们来说很重要.有时候不惜用大量的内存去换取一点时间.有些人可能都比较关注这个问题.首先时间上:cin ...
- mybatis10 实现类代理对象开发
mapper实现类代理对象开发 要想让mybatis自动创建dao接口实现类的代理对象,必须遵循一些规则: SqlSession sqlSession = sqlSessionFactory.open ...
- pcap 安装(debian7 linux) qt 使用pcap.h
安装方法一.sudo apt-get install libpcap-dev 安装方法二. 去http://www.tcpdump.org/下载最新的libpcap.tar.gz包 解压以后 ./co ...
- runtime重写description方法打印model属性和值
在开发过程中, 往往会有很多的model来装载属性. 而在开发期间经常会进行调试查看model里的属性值是否正确. 那么问题来了, 在objective-c里使用NSLog("%@" ...
- RedHat7安装Tomcat
编译安装Tomcat 下载jdk (http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...
- 动态树 Link-Cut Trees
动态树 动态树问题, 即要求我们维护一个由若干棵子结点无序的有根树组成的森林. 要求这个数据结构支持对树的分割.合并,对某个点到它的根的路径的某些操作,以及对某个点的子树进行的某些操作. 在这里我们考 ...
- angularJs ionic phoneGap 分享
由于坑较多 就如“天下难事,必作于易吧” 最近有机会接触到了git node angularJs ionic phoneGap 很开森也很痛苦 分享如下 推荐的几个博客地址: ionic开发插件之n ...
- VB------VS2012 IDE
当编辑器的前面出现很多小点不影响 运行的时候 Ctrl+E+S就可以取消