Datatable的分页入门

源码请见1.Datatable的分页入门

一、 目标

使用mybatis获取数据库中的数据,使用datatable对页面进行分页展示。

二、 使用到的资源
  1. datatable的资源包(如我用的DataTables-1.10.15.zip)。(下载地址datatable中文地址 OR datatable offical site
三、 项目结构

这里写图片描述
webapp下的extensions和media目录是拷贝的datatables资源中的2个目录。

四、 项目部分代码说明
4.1 使用README.sql创建数据库
#create a database
create database UserCenter;
#create a table
use UserCenter;
create table user(
    id int primary key auto_increment,
    name varchar(50),
    passwd varchar(50),
    age int
);
#add much data
insert into user(name,passwd,age) value('hash','1234',21);
insert into user(name,passwd,age) value('maven','1234',11);
insert into user(name,passwd,age) value('memorylorry','1234',22);
insert into user(name,passwd,age) value('lucy','1234',41);
insert into user(name,passwd,age) value('duck','1234',11);
insert into user(name,passwd,age) value('salary','1234',13);
insert into user(name,passwd,age) value('monkey','1234',51);
insert into user(name,passwd,age) value('dog','1234',22);
insert into user(name,passwd,age) value('march','1234',51);
insert into user(name,passwd,age) value('benz','1234',21);
insert into user(name,passwd,age) value('cookie','1234',12);
insert into user(name,passwd,age) value('fun','1234',55);
4.2 前台要装载datatable

通过columns数组选择要加载的列名;通过设置processing、serverSide、ajax三个参数加载数据。

$('#example').DataTable({
    columns : [ {
            "sWidth" : "100px",
            bSearchable : false,
            bSortable : false,
            data : 'id'
        }, {
            "sWidth" : "100px",
            bSearchable : true,
            bSortable : false,
            data : 'name'
        }, {
            "sWidth" : "100px",
            bSearchable : false,
            bSortable : false,
            data : 'passwd'
        }, {
            "sWidth" : "100px",
            bSearchable : false,
            bSortable : false,
            data : 'age'
        } ],
        "processing" : true,
        "serverSide" : true,
        "ajax" : "../getUserContentByMyself"
    });

这个ajax提出的请求时,会使用GET方式向服务器发送一些参数,如下所示,尤其是两个加黑的参数,表示页索引和页大小:

http://localhost:8080/DatatableTest/getUserContentByMyself
?draw=1&columns[0][data]=id&columns[0][name]=&columns[0][searchable]=false
&columns[0][orderable]=false&columns[0][search][value]=&columns[0][search][regex]=false
&columns[1][data]=name&columns[1][name]=&columns[1][searchable]=true
&columns[1][orderable]=false&columns[1][search][value]=&columns[1][search][regex]=false
&columns[2][data]=passwd&columns[2][name]=&columns[2][searchable]=false
&columns[2][orderable]=false&columns[2][search][value]=&columns[2][search][regex]=false
&columns[3][data]=age&columns[3][name]=&columns[3][searchable]=false
&columns[3][orderable]=false&columns[3][search][value]=&columns[3][search][regex]=false
&order[0][column]=0&order[0][dir]=asc&start=0&length=10&search[value]=
&search[regex]=false&_=1497077565075

4.3 后台获取数据

控制器要做这些事:

@Controller
public class GetUserByMyself {
    @Autowired
    @Qualifier("getUserPaginationService")
    private GetUserPaginationService getUserPaginationService;

    // 返回列表的内容
    @RequestMapping("getUserContentByMyself")
    @ResponseBody
    public Object getUserContent(HttpServletRequest request) {
        //获取页索引和页大小
        int start = Integer.parseInt(request.getParameter("start"));
        int len = Integer.parseInt(request.getParameter("length"));

        Map<String, String> params = new HashMap<String, String>();
        //根据页索引和页大小查询数据
        Map<String, Object> result = getUserPaginationService.getDetails(params, start, len);
        //返回数据
        return JSONArray.toJSON(result);
    }
}

DAO的实现方法:

public class GetUserPaginationDAO extends SqlSessionDaoSupport {

    // 获取分页的总数
    public Integer getUserSize(Map<String, String> params) {
        String statementCount = "cn.dectfix.dto.UserMapper.getUserSize";
        Integer totalSize = this.getSqlSession().selectOne(statementCount,params);
        if (totalSize != null)
            return totalSize;
        return 0;
    }

    // 获取分页的内容
    public Map<String, Object> getUserContent(Integer totalSize,Map<String, String> params,Integer skipResults, Integer rows) {
        Map<String, Object> resMap = new HashMap<String, Object>();
        String statementDetail = "cn.dectfix.dto.UserMapper.getUserContent";
        //装入要返回前台datatables的参数
        resMap.put("data",this.getSqlSession().selectList(statementDetail, params, new RowBounds(skipResults, rows)));
        resMap.put("recordsTotal", totalSize);
        resMap.put("recordsFiltered", totalSize);
        return resMap;
    }

}

代码中通过selectList(statement,params,RowBounds)进行限定数据库查询的范围;
为了满足datatables控件的需求,需要向前台返回data、recordsFiltered、recordsTotal等参数。为什么呢?可以查看datatable实例返回ajax请求的内容格式,它也是带有这些参数的!

4.4 总结

这样就完成了基础的datatables的初始化,可能你还需要修改语言风格,搜索框的功能,或则是列排序的功能等,可以留意留意官网的做法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MEMORYLORRY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值