ASP 는 jqGrid 를 사용 하여 읽 기와 쓰기 삭제 코드 를 실현 합 니 다(json)

10588 단어 ASPjsonjqGrid
jqGrid 는 jQuery 를 바탕 으로 하 는 우수한 DataGrid 프레임 워 크 입 니 다.모두 가 낯 설 지 않 을 것 입 니 다.인터넷 에서 ASP 를 바탕 으로 하 는 자료 가 매우 적 습 니 다.제 가 제공 하 는 데이터 형식 은 json 입 니 다.jqGrid 를 대상 으로 하 는 json 류 입 니 다.이 코드 는 홈 페이지 포럼 의 일부 PHP 에서 전 환 된 것 같 습 니 다.우 리 는 json.asp 로 저장 하고 코드 를 붙 입 니 다
 
<%
response.Charset="utf-8"
'---------------------------------------
' JSONClass
' Select JSON
'------------------------------------------
Class JSONClass
' , Private
Dim SqlString ' Select
Dim JSON ' JSON
Dim DBConnection ' Connection
'
Public Function GetJSON ()
dim Rs
dim returnStr
dim i
dim oneRecord
'
Set Rs= Server.CreateObject("ADODB.Recordset")
Rs.open SqlString,DBConnection,1,1
if page<>"" then
epage=cint(page)
if epage<1 then epage=1
if epage>rs.pagecount then epage=rs.pagecount
else
epage=1
end if
rs.pagesize = rows
rs.absolutepage = epage
' JSON
if Rs.eof=false and Rs.Bof=false then
returnStr="{ total: "& rs.pagecount &", page: "& page &", records: "& rs.recordcount &", rows:["
for j=0 to rs.pagesize-1
if rs.bof or rs.eof then exit for
' -------
'oneRecord = "{id:" & chr(34) &Rs.Fields(0).Value&chr(34)&",cell:["& chr(34) &Rs.Fields(0).Value&chr(34)&","
oneRecord = "{id:" & chr(34) &Rs.Fields(0).Value&chr(34)&",cell:["& chr(34) &Rs.Fields(0).Value&chr(34)&","
for i=1 to Rs.Fields.Count -1
'oneRecord=oneRecord & chr(34) &Rs.Fields(i).Name&chr(34)&":"
oneRecord=oneRecord & chr(34) &Rs.Fields(i).Value&chr(34) &","
Next
' ","
oneRecord=left(oneRecord,InStrRev(oneRecord,",")-1)
oneRecord=oneRecord & "]},"
'------------
returnStr=returnStr & oneRecord
Rs.MoveNext
next
' ","
returnStr=left(returnStr,InStrRev(returnStr,",")-1)
returnStr=returnStr & "]}"
end if
Rs.close
set Rs=Nothing
GetJSON=returnStr
End Function
' ,
Private Function check()
End Function
'
End Class
%>
2.데 이 터 를 표시 하 는 asp 파일 을 만 듭 니 다.예 를 들 어 list.asp,코드 는 다음 과 같 습 니 다
 
<!--#include file="conn.asp" -->
<!--#include file="json.asp" -->
<%
dim page,rows,sidx,sord
page = request.QueryString("page") 'page
rows = request.QueryString("rows") 'pagesize
sidx = request.QueryString("sidx") 'order by ??
sord = request.QueryString("sord")
if page="" then page = 1 end if
if rows = "" then rows = 10 end if
if sidx = "" then sidx = "id" end if
if sord = "" then sord ="asc" end if
Dim strSearchOn, strField, strFieldData, strSearchOper, strWhere
strSearchOn = Request("_search")
If (strSearchOn = "true") Then
strField = Request("searchField")
If (strField = "id" Or strField = "Title" Or strField = "NickName") Then
strFieldData = Request("searchString")
strSearchOper = Request("searchOper")
'construct where
strWhere = " Where " & strField
Select Case strSearchOper
Case "bw" : 'Begin With
strFieldData = strFieldData & "%"
strWhere = strWhere & " LIKE '" & strFieldData & "'"
Case "eq" : 'Equal
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " = " & strFieldData
Else
strWhere = strWhere & " = '" & strFieldData & "'"
End If
Case "ne": 'Not Equal
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " <> " & strFieldData
Else
strWhere = strWhere & " <> '"& strFieldData &"'"
End If
Case "lt": 'Less Than
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " <" & strFieldData
Else
strWhere = strWhere & " <'"& strFieldData &"'"
End If
Case "le": 'Less Or Equal
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " <= " & strFieldData
Else
strWhere = strWhere & " <= '"& strFieldData &"'"
End If
Case "gt": 'Greater Than
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " > " & strFieldData
Else
strWhere = strWhere & " > '"& strFieldData &"'"
End If
Case "ge": 'Greater Or Equal
If(IsNumeric(strFieldData)) Then
strWhere = strWhere & " >= " & strFieldData
Else
strWhere = strWhere & " >= '"& strFieldData &"'"
End If
Case "ew" : 'End With
strWhere = strWhere & " LIKE '%" & strFieldData & "'"
Case "cn" : 'Contains
strWhere = strWhere & " LIKE '%" & strFieldData & "%'"
End Select
End if
End If
server.ScriptTimeout=9000
dim a
set a=new JSONClass
a.Sqlstring="Select id,Title,NickName,Pwd,LastLoginTime From Admin"&strWhere&" "&"order by "& sidx & " " & sord
a.dbconnection=conn
response.Write(a.GetJSon())
conn.close()
set conn = nothing
%>
검색 코드 를 포함 합 니 다.이렇게 기본적으로 읽 기 를 실 현 했 습 니 다.jqGrid 의 editurl 파일 에 대해 우 리 는 edit.asp 라 고 부 릅 니 다.코드 는 다음 과 같 습 니 다
 
<%Option Explicit%>
<!--#include file="config.asp"-->
<%
Dim strOper, strID, strNickName, strTitle, strPwd
strOper = Request("oper")
strID = Replace(Request("Id"),"'","''")
strTitle = Replace(Request("Title"),"'","''")
strNickName = Replace(Request("NickName"),"'","''")
strPwd = Replace(Request("Pwd"),"'","''")
Select Case strOper
Case "add": 'Add Record
strSQL = "Insert Into Admin (Title, NickName, Pwd,LastLoginTime) Values('"&strTitle&"', '"&strNickName&"', '"&strPwd&"',Now()) "
Case "edit": 'Edit Record
strSQL = "Update Admin Set Title = '"&strTitle&"', NickName = '"&strNickName&"', Pwd = '"&strPwd&"' Where id = "&strID
Case "del": 'Delete Record
strSQL = "Delete From Admin Where id = "&strID
End Select
'response.Write strSQL
Dim strSQL,rs
Call OpenDB()
Set rs = Conn.Execute(strSQL)
Call CloseDB()
%>
이것 은 프론트 index.html 코드 입 니 다
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ASP_jqGrid_Test</title>
<link rel="stylesheet" type="text/css" href="jquery-ui-1.7.2.custom.css"/>
<link rel="stylesheet" type="text/css" href="jqgrid.css"/>
<link rel="stylesheet" type="text/css" href="ui.multiselect.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/cn.js"></script>
<script type="text/javascript" src="js/jqGrid.js"></script>
</head>
<body>
<table id="DataGrid" class="scroll"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>
</body>
</html>
<script type="text/javascript">
jQuery("#DataGrid").jqGrid({
url:'list.asp',
datatype: "json",
colNames:['ID',' ',' ',' ',' '],
colModel :[
{
name:'Id',
index:'Id',
width:50
},
{
name:'Title',
index:'Title',
editable:true,
editrules:{
required:true
}
},
{
name:'NickName',
index:'NickName',
editable:true,
editrules:{
required:true
}
},
{
name:'Pwd',
index:'Pwd',
editable:true,
edittype:'password',
hidden:true,
editoptions:{
size:20
},
editrules:{
edithidden:true
}
},
{
name:'LastLoginTime',
index:'LastLoginTime',
align:'right',
editrules:{
required:true
}
} ], caption:" ",
imgpath:'/images',
multiselect: true,
rowNum:20,
rowList:[10,20,30],
pager: jQuery('#pager'),
sortname: 'Id',
viewrecords: true,
sortorder: "desc",
height:400,
width:600,
editurl:"edit.asp"
});
$('#DataGrid').navGrid('#pager',{
refresh: true,
edit: true,
add: true,
del: true,
search: true,
searchtext:" ",
edittext:" ",addtext:" ",deltext:" "
});
</script>
jqGrid,좋 은 것 입 니 다~~

좋은 웹페이지 즐겨찾기