jQuery基于xml格式數(shù)據(jù)實(shí)現(xiàn)模糊查詢(xún)及分頁(yè)功能的方法
本文實(shí)例講述了jQuery基于xml格式數(shù)據(jù)實(shí)現(xiàn)模糊查詢(xún)及分頁(yè)功能的方法。分享給大家供大家參考,具體如下:
1、此代碼只支持xml 格式數(shù)據(jù),根據(jù)不同需求修改ajax的success方法就ok了
2、此代碼只是針對(duì)ajax只需一次請(qǐng)求的情況下
String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");};//解決ie trim問(wèn)題
(function($) {
/**
* ===============================================================================================================
* ========== 模糊搜索功能 =================================================================================
* ===============================================================================================================
*
* */
$.xml=function(data){//加載xml
var xmlobj=null;
if(window.ActiveXObject){
var xml;
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
xmlobj=$(xml);
}else{
xmlobj=$(data);
}
return xmlobj;
};
//獲取節(jié)點(diǎn)
$.getnode=function(key,obj){
var nodevalue=obj.children(key).text();
return nodevalue;
};
$.xmldata=null;
$.jsondata=null;
$.indexdata=null;
$.inputid=null;
$.pagetoolid=null;
$.resultdata=null;
$.pagetotal=1;
$.load=function(url){
$.ajax({
type: "post",
url: url,
dataType: "text",
success:function(data){
$.xmldata=$.xml(data);
var jsonstr='{';
var indexstr="{";
$.xmldata.find("QUERYINFO").find("ROW").each(function(i){
var point=i==0?"":",";
var ZZJGDM=$.getnode("ZZJGDM",$(this));
var JGMC=$.getnode("JGMC",$(this));
var DWLX=$.getnode("DWLX",$(this));
var JGJC=$.getnode("JGJC",$(this));
var JGBH=$.getnode("JGBH",$(this));
var ZCDZ=$.getnode("ZCDZ",$(this));
jsonstr+=point+"'"+JGBH+"':{'ZZJGDM':'"+ZZJGDM+"','JGMC':'"+JGMC+"','JGBH':'"+JGBH+"','JGJC':'"+JGJC+"','ZCDZ':'"+ZCDZ+"','DWLX':'"+DWLX+"'}";
indexstr+=point+"'"+JGMC+"':'"+JGBH+"'";
});
jsonstr+="}";
indexstr+="}";
$.indexdata=eval("("+indexstr+")");
$.jsondata=eval("("+jsonstr+")");
}});
};
$.select=function(obj){//選中結(jié)果中的一項(xiàng)時(shí)
var id=obj.attr("id");
$("#"+$.inputid).val($.jsondata[id].JGMC);
$("#reg_companysimplename").val($.jsondata[id].JGJC);
$("#reg_companysimplename_form").val($.jsondata[id].JGJC);
$("#reg_companycode").val($.jsondata[id].ZZJGDM==""?"---":$.jsondata[id].ZZJGDM);
$("#reg_companycode_form").val($.jsondata[id].ZZJGDM==""?"---":$.jsondata[id].ZZJGDM);
$("#reg_companytype").val($.jsondata[id].DWLX);
$("#reg_companytype_form").val($.jsondata[id].DWLX);
$("#reg_jgbh").val($.jsondata[id].JGBH);
$("#reg_regaddress").val($.jsondata[id].ZCDZ);
$("#reg_regaddress_form").val($.jsondata[id].ZCDZ);
$("#"+$.inputid).focus();
$("#"+$.inputid).blur();
};
$.fn.search=function(obj){//程序入口
var oldkeyword="";
var id=obj.id;
var url=obj.url;
$.inputid=$(this).attr("id");
if($.xmldata==null){
$.load(url);
}
//========================================鍵盤(pán)事件==========================================
$(this).keyup(function(){
var resultlist=null;
var keywordvalue=$(this).val().trim();
$("#reg_companysimplename").val("");
$("#reg_companycode").val("");
$("#reg_companytype").val("");
if(keywordvalue==""||oldkeyword==keywordvalue){//解決ie 的keyup 事件異常
if(keywordvalue==""){
$("#"+id+" ul").parent().hide();
}
return;
}else{
oldkeyword=keywordvalue;
}
var jsonstr='{';
var rownum=0,pagesize=10,num=0;
$.pagetotal=1;
$.indexsearch($.indexdata,keywordvalue);
resultlist=$.indexsearch($.indexdata,keywordvalue);
var ids="[";
for(var i in resultlist){//給查詢(xún)結(jié)果分頁(yè)
var point=$.pagetotal==1?"":",";
rownum++;
if(rownum>=pagesize&&rownum%pagesize==0||resultlist.length==rownum){
ids+=","+"'"+resultlist[i]+"']";
jsonstr+=point+"'"+$.pagetotal+"':"+ids;
ids="[";
num=0;
if(rownum%pagesize==0){
$.pagetotal++;
}
}else{
point=num==0?"":",";
ids+=point+"'"+resultlist[i]+"'";
num++;
}
}
jsonstr+="}";
$.resultdata=eval("("+jsonstr+")");
//初始化結(jié)果列表
if(rownum==0)return;
var pagenum=1;
$.pageto(pagenum,$.resultdata,$("#"+id+" ul"));
if($.pagetotal>1){
$.pagetool.createpagetool($.pagetotal);
$.pagetool.pageto(pagenum,$.pagetotal);
$("#pagetool span[pagenum='1']").css("backgroundColor",'lightblue');
}else{
$("#pagetool").html("");
}
$("#pagetool .num").click(function(){
pagenum=eval($(this).text());
$("#pagetool span").css("backgroundColor",'white');
$(this).css("backgroundColor",'lightblue');
$.pageto(pagenum,$.resultdata,$("#"+id+" ul"));
});
$("#pagetool .up").click(function(){
pagenum--;
if(pagenum!=0){
$("#pagetool span").css("backgroundColor",'white');
$("#pagetool span[pagenum='"+pagenum+"']").css("backgroundColor",'lightblue');
$.pageto(pagenum,$.resultdata,$("#"+id+" ul"));
$.pagetool.pageto(pagenum);
}else{
pagenum++;
}
});
$("#pagetool .down").click(function(){
pagenum++;
if(pagenum<=$.pagetotal){
$("#pagetool span").css("backgroundColor",'white');
$("#pagetool span[pagenum='"+pagenum+"']").css("backgroundColor",'lightblue');
$.pageto(pagenum,$.resultdata,$("#"+id+" ul"));
$.pagetool.pageto(pagenum);
}else{
pagenum--;
}
});
});
};
//工具欄分頁(yè)
$.pagetool = {
createpagetool : function(pagetotal) {
var html = "<span class='up'>up</span>";
var pagetoolpagenum=1;
for (var i = 1; i <= pagetotal; i++) {
if(i%5==1&&i>5){
pagetoolpagenum++;
}
html += "<span pagenum='"+i+"' i='" +pagetoolpagenum + "' class='num'>" + i
+ "</span>";
}
html += "<span class='down'>down</span>";
$("#pagetool").html(html);
},
pageto : function(pagenum,pagetotal) {
var pagetoolpagenum=1;
if(pagenum>5){
pagetoolpagenum=Math.ceil(pagenum/5);
}
$("#pagetool span").hide();
$("#pagetool span:first").show();
$("#pagetool span[i='"+pagetoolpagenum+"']").show();
$("#pagetool span:last").show();
}
};
//模糊搜索
$.indexsearch=function(indexdata,keyword){
var resultids=new Array();
for (var key in $.indexdata){
if(key.indexOf(keyword)!=-1)
resultids.push($.indexdata[key]);
}
return resultids;
};
//跳頁(yè)程序
$.pageto=function(pagenum,data,obj){
var list=data[pagenum];
var html="";
obj.html(html);
obj.parent().show();
$(list).each(function(i){
try{
html+='<li id="'+list[i]+'" >'+$.jsondata[list[i]].JGMC+'</li>';
}catch(e){}
});
obj.html(html);
obj.find("li").bind({//結(jié)果集的點(diǎn)擊事件
"click":function(){
$.select($(this));
obj.parent().hide();
obj.parent().find("#pagetool").html("");
},
"mouseover":function(){//結(jié)果集的鼠標(biāo)懸浮事件
$(this).parent().find("li").css("backgroundColor","white");
$(this).css("backgroundColor","lightblue");
}
});
};
}(jQuery));
在頁(yè)面中調(diào)用
<div class="item_input fl">
<input id="reg_companyname" autocomplete="off" type="text" class="text" name="DWBM_SV"/>
<div id="resultlist" class="hidden" style="width:300px;margin-top: 1px;display:none;">
<ul></ul>
<div class="pagetool" id="pagetool"></div>
</div>
</div>
<script>
$("#reg_companyname").search({"id":"resultlist","url":"/getcompany.go"});
</script>
運(yùn)行效果 (不同的效果需要不同的樣式)

PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《jQuery操作xml技巧總結(jié)》、《jquery中Ajax用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jquery zTree異步加載、模糊搜索簡(jiǎn)單實(shí)例分享
- jquery ztree實(shí)現(xiàn)模糊搜索功能
- jQuery實(shí)現(xiàn)id模糊查詢(xún)的小例子
- jquery easyui combobox模糊過(guò)濾(示例代碼)
- jQuery實(shí)現(xiàn)select模糊查詢(xún)(反射機(jī)制)
- jQuery簡(jiǎn)單實(shí)現(xiàn)的HTML頁(yè)面文本框模糊匹配查詢(xún)功能完整示例
- jQuery實(shí)現(xiàn)模糊查詢(xún)的方法分析
- jQuery實(shí)現(xiàn)模糊搜索功能的方法分析
相關(guān)文章
jQuery簡(jiǎn)單實(shí)現(xiàn)仿京東商城的左側(cè)菜單效果代碼
這篇文章主要介紹了jQuery簡(jiǎn)單實(shí)現(xiàn)仿京東商城的左側(cè)菜單效果代碼,通過(guò)簡(jiǎn)單的jQuery鼠標(biāo)事件及元素動(dòng)態(tài)變換實(shí)現(xiàn)樣式動(dòng)態(tài)切換功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-09-09
解決用jquery load加載頁(yè)面到div時(shí),不執(zhí)行頁(yè)面js的問(wèn)題
這篇文章主要介紹了解決用jquery load加載頁(yè)面到div時(shí),不執(zhí)行頁(yè)面js的問(wèn)題。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
在jQuery1.5中使用deferred對(duì)象 著放大鏡看Promise
在那篇經(jīng)典的關(guān)于jQuery1.5中Deferred使用方法介紹的文章中(譯文見(jiàn)這里),有下面一段描述2011-03-03
jQuery UI 實(shí)現(xiàn)email輸入提示實(shí)例
40個(gè)新鮮出爐的jQuery 插件和免費(fèi)教程[上]

