博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Textbox search autocompalety
阅读量:4969 次
发布时间:2019-06-12

本文共 7696 字,大约阅读时间需要 25 分钟。

源码:

//@name Textbox search autocompalety//@version 0.1//@author Ni//@description 文本框搜索插件,支持键盘上下键(function ($) {$.setOptions = function (k, p) {p = $.extend({IsShowSearchList: false, //是否显示搜索列表SearchValue: "", //作为搜索条件的值TextSearchSelectedIndex: "", //当前下拉列表所选的索引TextSearchList: new Array(), //下拉列表的数据源TimeoutSearch: null, //延迟搜索Left: 0, //div显示位置 XTop: 0, //div显示位置 YRecordCount: 0, //符合搜索条件的记录行数TimeoutNum: 500, //【可配置】延迟搜索 单位:毫秒Width: "500px", //【可配置】div宽度Url: "", //【可配置】取值方法地址MinSearchLength: 1, //【可配置】最小搜索关键词长度MaxRowCount: 10, //【可配置】搜索框最多显示行数 显示全部为-1onSelect: false //【可配置】行选中事件}, p);var t = {hideTxtSearchList: function () {clearTimeout(p.TimeoutSearch);if ($(".TxtSearchFoucs").attr('v') != undefined || $(".TxtSearchFoucs").attr('v')!=null)p.TextSearchSelectedIndex = parseInt($(".TxtSearchFoucs").attr('v'));if (p.TextSearchSelectedIndex != "") {t.selectTxtSearch(p.TextSearchSelectedIndex);}p.IsShowSearchList = false;p.TextSearchSelectedIndex = "";$("#divTxtSearch").hide();$("#divTxtSearch").empty();$("#iframeOpenTxt").hide();},getTxtSearchList: function () {var e = document.getElementById($(k).attr('id'));if (e.value.length < p.MinSearchLength) return;if (event.keyCode == 38 || event.keyCode == 40) {if (p.IsShowSearchList == false) return;//上下移动事件if (event.keyCode == 38)t.selectTxtSearchUp();if (event.keyCode == 40)t.selectTxtSearchDown();p.SearchValue = e.value;return;}p.Left = t.aspnm_pageX(e) + 1;p.Top = t.aspnm_pageY(e) + 20;p.SearchValue = e.value;clearTimeout(p.TimeoutSearch);p.TimeoutSearch = setTimeout(function () {$.ajax({url: p.Url + encodeURI(p.SearchValue) + "&pagesize=" + p.MaxRowCount,dataType: "json",type: "POST",success: function (da) {p.TextSearchList = new Array();p.RecordCount = da.total;for (var i = 0; i < da.rows.length; i++) {p.TextSearchList[i] = da.rows[i].row;}t.crateTxtSearchList();}});}, p.TimeoutNum);},//创建下拉列表crateTxtSearchList: function () {p.IsShowSearchList = true;p.TextSearchSelectedIndex = "";$("#divTxtSearch").empty();var html;if (p.TextSearchList.length > 0) {html = "
";var rowCount = (p.MaxRowCount == -1 || p.MaxRowCount > p.TextSearchList.length) ? p.TextSearchList.length : p.MaxRowCount;for (var i = 0; i < rowCount; i++) {var listItem = p.TextSearchList[i].split(',');html += "
";for (var j = 0; j < listItem.length; j++) {html += "
";}html += "
";}html += "
" + listItem[j] + "
";if (p.RecordCount > rowCount) {html += "
未列出所有搜索结果!
";}} else {html = "
搜索完毕,没有结果可以显示!
";}$("#divTxtSearch").show();$("#divTxtSearch").html(html);$("#iframeOpenTxt").show();$("#divTxtSearch").css("left", p.Left);$("#divTxtSearch").css("top", p.Top);$("#divTxtSearch").css("width", p.Width);t.setOpenTxtIframe();},//设置IframesetOpenTxtIframe: function () {$("#iframeOpenTxt").show();$("#iframeOpenTxt").css("left", p.Left);$("#iframeOpenTxt").css("top", p.Top);$("#iframeOpenTxt").css("width", document.getElementById('divTxtSearch').offsetWidth + "px");$("#iframeOpenTxt").css("height", document.getElementById('divTxtSearch').offsetHeight + "px");},//Xaspnm_pageX: function (element) {var x = 0;dox += element.offsetLeft;while ((element = element.offsetParent));return x;},//Yaspnm_pageY: function (element) {var y = 0;doy += element.offsetTop;while ((element = element.offsetParent));return y;},//鼠标的选择行事件selectTxtSearchMouse: function (rowIndex) {if ($(".TxtSearchFoucs").attr('v') != undefined || $(".TxtSearchFoucs").attr('v') != null)p.TextSearchSelectedIndex = parseInt($(".TxtSearchFoucs").attr('v'));if (p.TextSearchSelectedIndex != '')$("#TRTxtSearch" + p.TextSearchSelectedIndex).attr("class", "TxtSearchNotFoucs");$("#TRTxtSearch" + rowIndex).attr("class", "TxtSearchFoucs");p.TextSearchSelectedIndex = rowIndex;},//点击'上'触发的js事件selectTxtSearchUp: function () {if ($(".TxtSearchFoucs").attr('v') != undefined || $(".TxtSearchFoucs").attr('v') != null)p.TextSearchSelectedIndex = parseInt($(".TxtSearchFoucs").attr('v'));if (p.TextSearchSelectedIndex == '' || parseInt(p.TextSearchSelectedIndex) == 1) {//最后一个t.selectTxtSearch(p.TextSearchList.length);}else {//上移t.selectTxtSearch(parseInt(p.TextSearchSelectedIndex) - 1);}},//点击'下'触发的js事件selectTxtSearchDown: function () {if ($(".TxtSearchFoucs").attr('v') != undefined || $(".TxtSearchFoucs").attr('v') != null)p.TextSearchSelectedIndex = parseInt($(".TxtSearchFoucs").attr('v'));if (p.TextSearchSelectedIndex == '' || parseInt(p.TextSearchSelectedIndex) == p.TextSearchList.length) {//第一个t.selectTxtSearch('1');}else {//下移t.selectTxtSearch(parseInt(p.TextSearchSelectedIndex) + 1);}},//选择行selectTxtSearch: function (rowIndex) {if (p.TextSearchSelectedIndex != '')$("#TRTxtSearch" + p.TextSearchSelectedIndex).attr("class", "TxtSearchNotFoucs");$("#TRTxtSearch" + rowIndex).attr("class", "TxtSearchFoucs");p.TextSearchSelectedIndex = rowIndex;//将值写到页面指定元素 方法自己实现var rowData = p.TextSearchList[rowIndex - 1].split(',');if (p.onSelect) p.onSelect(rowData);}};$(k).keyup(function () {t.getTxtSearchList();});$(k).blur(function () {t.hideTxtSearchList();});};//程序入口$.fn.TxtSearch = function (p) {this.each(function () {$.setOptions(this, p);});};})(jQuery);document.write('
'+ '');

  效果图:

调用方式:

//自动搜索    $("#txt1").TxtSearch({        Url: "/httpHandler/SystemManagement/HandlerDemo.ashx?type=getpname&key=",        Width: "460",        onSelect: function (rowData) {            $('#hid1').val(rowData[0]);            $('#txt1').val(rowData[1]);        }    });

  

private MenuFun _instance = DataFactory.CreateInstance
("MenuFun", AssemblyKey.BusRoles); private CommonFun commonFun = DataFactory.CreateInstance
("CommonFun", AssemblyKey.BusComm); public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write(Handler(context)); }public string Handler(HttpContext context) { string src = context.Request["type"]; switch (src) { case "getpname": return GetPName(context); default: return string.Empty; } }string GetPName(HttpContext context) { string pageSize = context.Request["pagesize"]; if (string.IsNullOrEmpty(pageSize)) pageSize = "100"; if (pageSize == "-1") pageSize = "10000"; string where = ""; string key = context.Request["key"]; if (!string.IsNullOrEmpty(key)) { if (Utils.IsNumber(key)) where = string.Format(" and FUNMODEL_CD = '{0}'", key); else if(Utils.IsEnglish(key)) { where = string.Format(" and dbo.f_GetPy(FUNMODEL_NM) like '%{0}%'", key); } else { where = string.Format(" and FUNMODEL_NM like '%{0}%'", key); } } DataSet ds = commonFun.GetLimitPageData("v_CYP_MenuName", "FUNMODEL_CD, FUNMODEL_NM,FUNMODEL_LEV,MAXID", where, "FUNMODEL_CD", 1, int.Parse(pageSize)); if (ds != null && ds.Tables.Count == 2 && ds.Tables[1].Rows.Count > 0) { string json = "{\"total\":" + ds.Tables[0].Rows[0][0] + ",\"rows\":["; foreach (DataRow dataRow in ds.Tables[1].Rows) { json += "{\"row\":\""; json = ds.Tables[1].Columns.Cast
().Aggregate(json, (current, dataColumn) => current + (dataRow[dataColumn] + ",")); json = json.Substring(0, json.Length - 1) + "\"},"; } json = json.TrimEnd(',') + "]}"; return json; } return "{\"total\":0,\"rows\":[]}"; }

  

转载于:https://www.cnblogs.com/nimin961145/archive/2013/02/19/2917388.html

你可能感兴趣的文章
java小技巧
查看>>
POJ 3204 Ikki's Story I - Road Reconstruction
查看>>
【BZOJ】2959: 长跑(lct+缩点)(暂时弃坑)
查看>>
iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
查看>>
toad for oracle中文显示乱码
查看>>
SQL中Group By的使用
查看>>
错误org/aopalliance/intercept/MethodInterceptor解决方法
查看>>
两个表格中数据不用是一一对应关系--来筛选不同数据,或者相同数据
查看>>
Strict Standards: Only variables should be passed by reference
查看>>
hiho_offer收割18_题解报告_差第四题
查看>>
AngularJs表单验证
查看>>
静态方法是否属于线程安全
查看>>
02号团队-团队任务3:每日立会(2018-12-05)
查看>>
SQLite移植手记1
查看>>
js05-DOM对象二
查看>>
mariadb BINLOG_FORMAT = STATEMENT 异常
查看>>
C3P0 WARN: Establishing SSL connection without server's identity verification is not recommended
查看>>
iPhone在日本最牛,在中国输得最慘
查看>>
动态方法决议 和 消息转发
查看>>
js 基础拓展
查看>>