欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP 使用jqGrid实现读写删的代码(json)

程序员文章站 2022-06-24 16:08:23
jqgrid是一个优秀的基于jquery的datagrid框架,想必大伙儿也不陌生,网上基于asp的资料很少,我提供一个,数据格式是json的: 、一个针对jqgrid的j...
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,好东西~~