熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Web編程 >> 正文

ASP 使用jqGrid實現讀寫刪的代碼(json)

2022-06-13   來源: Web編程 
jqGrid是一個優秀的基於jQuery的DataGrid框架想必大伙兒也不陌生網上基於ASP的資料很少我提供一個數據格式是json的
一個針對jqGrid的json類這段代碼似乎是由官網論壇的一些PHP中轉化而來我們存為jsonasp代碼貼一下

  復制代碼 代碼如下:

  <%
responseCharset=utf

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= ServerCreateObject(ADODBRecordset)
Rsopen SqlStringDBConnection
if page<> then
epage=cint(page)
if epage< then epage=
if epage>rspagecount then epage=rspagecount
else
epage=
end if
rspagesize = rows
rsabsolutepage = epage
生成JSON字符串
if Rseof=false and RsBof=false then
returnStr={ total: & rspagecount & page: & page & records: & rsrecordcount & rows:[
for j= to rspagesize
if rsbof or rseof then exit for

oneRecord = {id: & chr() &RsFields()Value&chr()&cell:[& chr() &RsFields()Value&chr()&
oneRecord = {id: & chr() &RsFields()Value&chr()&cell:[& chr() &RsFields()Value&chr()&
for i= to RsFieldsCount
oneRecord=oneRecord & chr() &RsFields(i)Name&chr()&:
oneRecord=oneRecord & chr() &RsFields(i)Value&chr() &
Next
去除記錄最後一個字段後的
oneRecord=left(oneRecordInStrRev(oneRecord))
oneRecord=oneRecord & ]}

returnStr=returnStr & oneRecord
RsMoveNext
next
去除所有記錄數組後的
returnStr=left(returnStrInStrRev(returnStr))
returnStr=returnStr & ]}
end if
Rsclose
set Rs=Nothing
GetJSON=returnStr
End Function
私用方法在類中使用
Private Function check()
End Function

End Class
%>


制作顯示數據的asp文件listasp代碼如下

  復制代碼 代碼如下:

  <!#include file=connasp >
<!#include file=jsonasp >
<%
dim pagerowssidxsord
page = requestQueryString(page) page
rows = requestQueryString(rows) pagesize
sidx = requestQueryString(sidx) order by ??
sord = requestQueryString(sord)
if page= then page = end if
if rows = then rows = 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
serverScriptTimeout=
dim a
set a=new JSONClass
aSqlstring=Select idTitleNickNamePwdLastLoginTime From Admin&strWhere& &order by & sidx & & sord
adbconnection=conn
responseWrite(aGetJSon())
connclose()
set conn = nothing
%>


裡面把搜索的代碼涵蓋了這樣基本實現了讀至於jqGrid中的editurl的文件我們稱其editasp代碼如下

  復制代碼 代碼如下:

  <%Option Explicit%>
<!#include file=configasp>
<%
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 PwdLastLoginTime) 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
responseWrite strSQL
Dim strSQLrs
Call OpenDB()
Set rs = ConnExecute(strSQL)
Call CloseDB()
%>


這是前台l代碼

  復制代碼 代碼如下:

  <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN >
<html xmlns=>
<head>
<meta httpequiv=ContentType content=text/html; charset=utf />
<title>ASP_jqGrid_Test</title>
<link rel=stylesheet type=text/css />
<link rel=stylesheet type=text/css />
<link rel=stylesheet type=text/css />
<script type=text/javascript src=js/jqueryjs></script>
<script type=text/javascript src=js/cnjs></script>
<script type=text/javascript src=js/jqGridjs></script>
</head>
<body>
<table id=DataGrid class=scroll></table>
<div id=pager class=scroll style=textalign:center;></div>
</body>
</html>
<script type=text/javascript>
jQuery(#DataGrid)jqGrid({
url:listasp
datatype: json
colNames:[ID管理員賬號管理員昵稱密碼上次登錄時間]
colModel :[
{
name:Id
index:Id
width:
}
{
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:
}
editrules:{
edithidden:true
}
}
{
name:LastLoginTime
index:LastLoginTime
align:right
editrules:{
required:true
}
} ] caption:管理員列表
imgpath:/images
multiselect: true
rowNum:
rowList:[]
pager: jQuery(#pager)
sortname: Id
viewrecords: true
sortorder: desc
height:
width:
editurl:editasp
});
$(#DataGrid)navGrid(#pager{
refresh: true
edit: true
add: true
del: true
search: true
searchtext:
edittext:addtext:deltext:
});
</script>


jqGrid好東西~~
From:http://tw.wingwit.com/Article/program/Web/201405/30793.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.