%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
'''常规举例
' ar_export P_("PAGE")( 55 ).show
'''字符串参数例子
' ar_export P_("PAGE")( array(55, "name=张三&ange=18") ).show
'''二维Dictionary参数例子
' dim dict : set dict = D_
' dict("name") = "张三"
' dict("age") = 18
' set dict("hobby") = D_ '一维Dictionary参数例子可以去掉dict("hobby")这一项
' dict("hobby")(0) = "篮球"
' dict("hobby")(1) = "足球"
' dict("hobby")(2) = "看电影"
' var_export P_("PAGE")( array( 55, dict ) ).show
' var_export "
"
' var_export request.querystring '{ "page": "4", "hobby": "篮球, 足球, 看电影", "name": "张三", "age": "18" }
Class POPASP_PAGE
private total '记录总个数
private totalPage '总共多少页
private page '当前页
private uri
private parameter '网址中的额外参数
'下面是可配置的
private perpage '每页显示多少条
private header '"条记录"
private first '"首页"
private prev '"上一页"
private [next] '"下一页"
private last '"尾页"
private noRecordTip '无数据的提示
private pageCount '分页中显示的页码个数
private seperator '分隔符
private pageScale '类似 5/30页
private matches
private jump '页面跳转,如果不需要页面跳转,可以 Call oPage.setConfig( "jump" , "" )
Private Sub Class_Initialize
dim arr,pos
header = "条记录"
first = "首页"
prev = "上一页"
[next] = "下一页"
last = "尾页"
pageCount = 5
perpage = C_("PAGE_PAGESIZE")
noRecordTip = "没有可载入的数据"
seperator = " "
pageScale = true
page = POP_MVC.Get( C_("VAR_PAGE") )
if not isNumeric( page ) then
page = 1
end if
if page < 1 then
page = 1
end if
page = CLng( page )
End Sub
Private Sub Class_Terminate
set matches = nothing
End Sub
Public Function getConfig(attr)
Execute "getConfig = " & attr
end Function
Public Sub setConfig(attr,value)
if LCase( attr ) = "parameter" then
Call setParameter( value )
Exit Sub
end if
if isNumeric(value) then
Execute attr & " = " & value
else
Execute attr & " = """ & value & """"
end if
if LCase( attr ) = "perpage" then
perpage = value
totalPage = CLng(Abs(Int(- total / perpage)))
if page > totalPage then
page = totalPage
end if
end if
End Sub
Private Sub setParameter( arg )
if typename( arg ) = "Dictionary" then 'Dictionary对象
dim k1,k2
parameter = ""
for each k1 in arg
if typename( arg(k1) ) = "Dictionary" then
for each k2 in arg(k1)
parameter = parameter & "&" & k1 & "=" & cstr( arg(k1)(k2) )
next
else
parameter = parameter & "&" & k1 & "=" & cstr( arg(k1) )
end if
next
parameter = POP_MVC.ltrim( parameter , "&" )
else
parameter = arg
end if
End Sub
'使用时有两种方法,一种是传入Recordset对象,一种是传入总数
'1. that.assign "page" , P_("PAGE")(rs).show,注意:此时应将其置于thag.assign "list" , array(rs)的前面
'2. that.assign "page" , P_("PAGE")( total ).show,total为总数
'url其它参数传入时,可以使用P_("PAGE")( Array( rs/total, parameter ) ),parameter可以是字符串也可以是最多二维的Dictionary对象
Public Default Function Constructor(byref args)
dim rs
if isArray(args) then
if typename( args(0) ) = "Recordset" then
set rs = args(0)
else
total = args(0)
end if
if ubound( args ) > 0 then
Call setParameter( args(1) )
end if
else
if typename( args ) = "Recordset" then
set rs = args
else
total = args
end if
parameter = ""
end if
if not isEmpty( rs ) then
total = rs.recordCount
totalPage = rs.pageCount
perpage = rs.pageSize
else
totalPage = CLng(Abs(Int(- total / perpage)))
end if
page = CLng(page)
if page > totalPage then
page = totalPage
end if
set Constructor = Me
End Function
'得到页码的链接html
'如 2
Public Function getUrl(dPage,sTxt)
if isEmpty( uri ) then
uri = SELF__
if parameter <> "" AND inStr( POP_MVC.urldecode(uri),POP_MVC.urldecode(parameter) ) < 1 then
if POP_MVC.Get("") = "" then
uri = uri & "?" & parameter
else
uri = uri & "&" & parameter
end if
end if
uri = POP_MVC.String.reg_replace(uri,"","&" & C_("VAR_PAGE") & "=[^&]+","ig")
uri = POP_MVC.String.reg_replace(uri,"","\b" & C_("VAR_PAGE") & "=[^&]+","ig")
if instr( uri , "?" ) > 1 then
if inStr( uri , "&" ) > 1 or not POP_MVC.String.EndsWith( uri , "?" ) then
uri = uri & "&"
end if
else
uri = uri & "?"
end if
end if
getUrl = "" & sTxt & ""
End Function
'将当前页的起始与结束位以数组形式返回,如 Array( 10,19 )
Public Function getRange( )
dim start_,end_
start_ = (page-1) * perpage
end_ = start_ + perpage -1
if end_ > total - 1 then
end_ = total -1
end if
getRange = Array( start_ , end_ )
End Function
'返回页面字符串
'如果总数为0,则提示无数据
'如果总页数不足一页,则输出空字符串
Public Function Show()
dim dLeft,dRight,nav,sFirst,sPrev,sNext,sLast,temp
nav = array()
if total < 1 Then
Show = noRecordTip
Exit Function
end if
If totalPage > 1 then
POP_MVC.Arr.push nav,"" & page & ""
End If
dLeft = page - 1
dRight = page + 1
do While dLeft >= 1 OR dRight <= totalPage
if UBound(nav) >= pageCount-1 then
exit do
end if
if dLeft>= 1 then
POP_MVC.Arr.Unshift nav,getUrl(dLeft,dLeft)
dLeft = dLeft - 1
end if
if dRight <= totalPage then
if dRight >= 1 then
POP_MVC.Arr.Push nav,getUrl(dRight,dRight)
end if
dRight = dRight + 1
end if
Loop
if page = 1 then
sFirst = ""
sPrev = ""
else
if not is_empty(prev) then sPrev = getUrl(page-1,prev) : POP_MVC.Arr.Unshift nav,sPrev
if not is_empty(first) then sFirst = getUrl(1,first) : POP_MVC.Arr.Unshift nav,sFirst
end if
if page = totalPage then
sLast = ""
sNext = ""
else
if not is_empty([next]) then sNext = getUrl( page+1,[next] ) : POP_MVC.Arr.Push nav,sNext
if not is_empty(last) then sLast = getUrl( totalPage,last ) : POP_MVC.Arr.Push nav,sLast
end if
if not is_empty(pageScale) then POP_MVC.Arr.Unshift nav,page & "/" & totalPage & "页"
if not is_empty(header) then POP_MVC.Arr.Unshift nav,"共" & total & header
temp = page
if temp < totalPage then
temp = temp + 1
elseif temp = totalPage then
temp = 1
end if
if totalPage > 1 and isEmpty( jump ) then
jump = " "
POP_MVC.Arr.Push nav,jump
end if
Show = "