您好,登錄后才能下訂單哦!
adauhuehkek最近做項目的時候遇到一個需求,需要在錄入數(shù)據(jù)的時候檢索已經(jīng)存在的數(shù)據(jù)記錄,并從中提取相似的數(shù)據(jù)進行展示并選擇,以提高錄入效率,簡單的說,這個功能有點像在谷歌、百度搜索框里輸入一個關(guān)鍵字,然后自動在下邊列舉出與關(guān)鍵字相似的信息供選擇。好啦,現(xiàn)在功能說完了,下邊就直入正題,把兩種方法都列出來,以供需要的人去選擇使用,其實兩種方法的區(qū)別之處很小,主要是在返回檢索結(jié)果時調(diào)用方法不一樣,一種是map(),另一種是each(),這兩個方法的區(qū)別我就不說了,簡單總結(jié)就是map()要從建數(shù)組,each()直接返回原始數(shù)組,基于這一點,在內(nèi)存開銷上顯然each()更好一點,當(dāng)然,這個也不一概而論,看各自需求了。
服務(wù)端:
getAddress.asp
<!--#include file="Conn.asp" --> <!--#include file="TypeJson.asp" --> <% dim myrs,sqlstr,singleJson,sqlstr2,q Set myrs=server.CreateObject("adodb.recordset") 'q=Replace(Request.QueryString("q"),"'","''") q=request.Item("param") set singleJson = new MtRecToJson sqlstr = "select address from callrecord where address like'%"&q&"%'" sqlstr2="select id,usr,uid,usrType,corp from usr order by id" sqlstr3="select top 1 * from usr where 1=2" if q<>"" or q<>null then myrs.Open sqlstr,Conn,1.1 else myrs.Open sqlstr2,Conn,1.1 end if singleJson.setRecordset(myrs) response.write singleJson.getListJsonDB() if not IsEmpty(myrs) then if myrs.State>0 then myrs.close end if set myrs = nothing end if conn.close set conn = nothing %>
TypeJson.asp
<% 'JSON 接口通用類 Class MtRecToJson private recordset private json_str private mask_fields private Sub Class_Initialize end sub 'public property let setRecordset(byval rec) ' set recordset = rec 'end property '設(shè)置值 參數(shù)為ADODB.recordset對象 public sub setRecordset(rec) if TypeName(rec)="Recordset" then set recordset = rec end if end sub '獲得JSON public Function getOneJsonDB() dim i json_str = "{" if not IsEmpty(recordset) then For i=0 To recordset.fields.count-1 json_str = json_str & """"&recordset.fields(i).name&"""" json_str = json_str & ":" json_str = json_str & """" if not recordset.eof then json_str = json_str & recordset.fields(i).value end if json_str = json_str & """" if i<recordset.fields.count-1 then json_str = json_str & "," end if Next end if json_str = json_str & "}" getOneJsonDB = json_str end function '獲得JSON 格式的list public Function getListJsonDB() dim i,k json_str = json_str & "[" if not IsEmpty(recordset) then For k=0 To recordset.recordcount-1 if k>=recordset.pageSize then Exit for If recordset.Eof Then Exit For json_str = json_str & "{" For i=0 To recordset.fields.count-1 json_str = json_str & """"&recordset.fields(i).name&"""" json_str = json_str & ":" json_str = json_str & """" if not recordset.eof then json_str = json_str & recordset.fields(i).value end if json_str = json_str & """" if i<recordset.fields.count-1 then json_str = json_str & "," end if Next json_str = json_str & "}" if k<recordset.recordcount-1 then json_str = json_str & "," end if recordset.MoveNext next end if if(Right(json_str,1)=Chr(44)) then'查看拼接字符串最后是否有異常(偶爾存在逗號,不知道為什么),如果有就主動添加一個結(jié)尾字段 json_str = json_str & """end""]" else json_str = json_str & "]" end if getListJsonDB = json_str end function end class %>
客戶端:
show.asp
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Remote JSON</title> <link href="css/themes/default/easyui.css" rel="stylesheet" /> <link href="css/themes/icon.css" rel="stylesheet" /> <link href="css/themes/color.css" rel="stylesheet" /> <script type="text/javascript" src="script/jquery.min.js"></script> <script type="text/javascript" src="script/jquery.easyui.min.js"></script> <script type="text/javascript" src="script/easyui-lang-zh_CN.js"></script> </head> <body> <h3>Remote JSON</h3> <p>This sample shows how to use JSON to retrieve data from a remote site.</p> <div ></div> <div class="easyui-panel" > <div > <input id="s1" name="s1" class="easyui-combobox" /> </div> </div> <script language="javascript"> var myloader = function(param, success, error) { var q = param.q || ''; if (q.length < 2) { return false } $.ajax({ type: 'post', url: 'getAddress.asp', dataType: 'json', //contentType: 'application/x-www-form-urlencoded:charset=UTF-8', data: { param: q }, success: function(data) { //alert(data); // var items = $.map(data, function(value) { // return { // address: value // }; // }); var items = $.each(data, function(value) { return this; //遍歷數(shù)組中的值 }); success(items);//調(diào)用loader的success方法,將items添加到下拉框中 }, error: function() { error.apply(this); } }); } $(function() { $('#s1').combobox({ loader: myloader, mode: 'remote', valueField: 'address', textField: 'address', editable:'true', hasDownArrow: false }); }) </script> </body> </html>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。