ASPはjqGridを用いて読み書き削除を実現するコード(json)

10521 ワード

jqGridは優れたjQueryベースのDataGridフレームワークであり、皆さんもよく知られていると思います.ネット上のASPベースの資料は少ないです.私は1つ提供します.データフォーマットは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、コードは以下の通りです
 
  


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 & " Else
strWhere = strWhere & " 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、コードは以下の通りです.
 
  


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()
%>

これはフロントです.htmlコード
 
  




ASP_jqGrid_Test












<br>jQuery("#DataGrid").jqGrid({ <br>url:'list.asp', <br>datatype: "json", <br>colNames:['ID',' ',' ',' ',' '], <br>colModel :[ <br>{ <br>name:'Id', <br>index:'Id', <br>width:50 <br>}, <br>{ <br>name:'Title', <br>index:'Title', <br>editable:true, <br>editrules:{ <br>required:true <br>} <br>}, <br>{ <br>name:'NickName', <br>index:'NickName', <br>editable:true, <br>editrules:{ <br>required:true <br>} <br>}, <br>{ <br>name:'Pwd', <br>index:'Pwd', <br>editable:true, <br>edittype:'password', <br>hidden:true, <br>editoptions:{ <br>size:20 <br>}, <br>editrules:{ <br>edithidden:true <br>} <br>}, <br>{ <br>name:'LastLoginTime', <br>index:'LastLoginTime', <br>align:'right', <br>editrules:{ <br>required:true <br>} <br>} ], caption:" ", <br>imgpath:'/images', <br>multiselect: true, <br>rowNum:20, <br>rowList:[10,20,30], <br>pager: jQuery('#pager'), <br>sortname: 'Id', <br>viewrecords: true, <br>sortorder: "desc", <br>height:400, <br>width:600, <br>editurl:"edit.asp" <br>}); <br>$('#DataGrid').navGrid('#pager',{ <br>refresh: true, <br>edit: true, <br>add: true, <br>del: true, <br>search: true, <br>searchtext:" ", <br>edittext:" ",addtext:" ",deltext:" " <br>}); <br>

jqGrid、いいもの~~