简体   繁体   English

数据表AJAX数据源和自定义分页操作

[英]Datatables AJAX Data Source and Custom Pagination actions

I am using Datatables for building the table for my data. 我正在使用数据表为我的数据构建表。

I am passing JSON response from Server side [Java] to JSP and using Datatables js to build the tables in JSP using the same data. 我正在将JSON响应从服务器端[Java]传递到JSP,并使用Datatables js使用相同的数据在JSP中构建表。

If I pass 100 records, Datatables automatically provides facility to paginate and scroll through the results. But in my case, I always get only 20 records and when I user click on next page, I should call the localServlet to fetch fresh JSON response for next 20records.

So how do I configure Datatable so that whenever pagination function is used, call the AJAX resource and fetch the data and paint it. 因此,我该如何配置数据表,以便每当使用分页功能时,都调用AJAX资源并获取数据并绘制数据。

There is a sample at http://datatables.net/release-datatables/examples/data_sources/server_side.html http://datatables.net/release-datatables/examples/data_sources/server_side.html中有一个示例

however the code is in PHP. 但是代码在PHP中。 But having a look at it you will see that there request parameters it uses are iDisplayStart and iDisplayLength 但是看看它,您会发现它使用的请求参数是iDisplayStartiDisplayLength

You will have to reimplement this in your server side java. 您将不得不在服务器端Java中重新实现它。

Below is some code I have used (using Stripes) 以下是我使用过的一些代码(使用Stripes)

    Long count = (Long) getContext().getRequest().getSession(true).getAttribute("xbcount");
    if (count == null) {
        count = histDao.getCount();
        getContext().getRequest().getSession(true).setAttribute("xbcount", count);
    }

    DataTableRes res = new DataTableRes (getsEcho(), count, count);

    int rowStartIdxAndCount[] = {getiDisplayStart(), getiDisplayLength()};

    List<HistoryUint> list = histDao.findAll(rowStartIdxAndCount);

And the DAO 还有DAO

public List<HistoryUint> findAll(final int... rowStartIdxAndCount) {
    EntityManagerHelper.log("finding all HistoryUint instances",
            Level.INFO, null);
    try {
        final String queryString = "select model from HistoryUint model";
        Query query = getEntityManager().createQuery(queryString);
        if (rowStartIdxAndCount != null && rowStartIdxAndCount.length > 0) {
            int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);
            if (rowStartIdx > 0) {
                query.setFirstResult(rowStartIdx);
            }

            if (rowStartIdxAndCount.length > 1) {
                int rowCount = Math.max(0, rowStartIdxAndCount[1]);
                if (rowCount > 0) {
                    query.setMaxResults(rowCount);
                }
            }
        }
        return query.getResultList();
    } catch (RuntimeException re) {
        EntityManagerHelper.log("find all failed", Level.SEVERE, re);
        throw re;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM