最近要写一个分页的功能,找了很多的ajax分页资料,都觉得不是很好,本来想用ext的,可是觉得速度有点慢,在加上公司明确要求用jquery+json 实现分页,没办法,后来发现jquery的flexgrid的分页还不错。就花了点时间写了个。
java 代码:
@Controller
public class InfoIssueAction extends PBaseAction {
public InfoIssueAction(){};
@Autowired
InfoIssueService infoIssueService;
List rows=new ArrayList();
protected int rp;
protected int page=1;
protected int total;
@SuppressWarnings("unchecked")
public String showInfoIssue(){
int startIndex = (page - 1) * rp; //计算查询开始数据下标
total=infoIssueService.getAllInfoIssue().size();
List list=infoIssueService.getInfoIssue(startIndex, rp);
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
InfoIssue infoissue = (InfoIssue) iterator.next();
Map cellMap = new HashMap();
cellMap.put("issueid", infoissue.getIssueid());
cellMap.put("cell", new Object[] {infoissue.getIssueid(),infoissue.getCaption(),
infoissue.getAttribute(),infoissue.getMaketop(),infoissue.getIssued()});
rows.add(cellMap);
}
return "listSuccess";
}
@JSON(name="rows")
public List getRows() {
return rows;
}
public void setRows(List rows) {
this.rows = rows;
}
public int getRp() {
return rp;
}
public void setRp(int rp) {
this.rp = rp;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
jsp代码:
需要导入文件,:
<link rel="stylesheet" type="text/css" href="grid/css/flexigrid/flexigrid.css"></link>
<script type="text/javascript" src="page/js/jquery.js"></script>
<script type="text/javascript" src="page/grid/flexigrid.js"></script>
<div class="bborderx">
<table id="flex1" style="display:none"></table>
</div>
js代码:
$("#flex1").flexigrid
(
{
url: 'infoIssue/InfoIssue!show.action',
dataType: 'json',
colModel : [
{display: '编号', name : 'issueid', width : 60, sortable : true, align: 'center'},
{display: '标题', name : 'caption', width : 120, sortable : true, align: 'left'},
{display: '是否置顶', name : 'maketop', width : 120, sortable : true, align: 'left'},
{display: '是否发布', name : 'issued', width : 120, sortable : true, align: 'left'}
],
buttons : [
{name: '增加', bclass: 'add', onpress : test},
{name: '删除', bclass: 'delete', onpress : test},
{separator: true}
],
searchitems : [
{display: '标题', name : 'caption', isdefault: true}
],
sortname: "issueid",
sortorder: "asc",
usepager: true,
title: '新闻发布 ',
useRp: true,
rp: 1,
showTableToggleBtn: true,
width: 600,
height: 300
}
);
function test(com,grid)
{
if (com=='删除')
{
confirm('是否删除这 ' + $('.trSelected',grid).length + ' 条记录吗?')
}
else if (com=='增加')
{
alert('增加一条!');
}
}