我正在使用数据表为我的数据构建表。
我正在将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.
因此,我该如何配置数据表,以便每当使用分页功能时,都调用AJAX资源并获取数据并绘制数据。
http://datatables.net/release-datatables/examples/data_sources/server_side.html中有一个示例
但是代码在PHP中。但是看看它,您会发现它使用的请求参数是iDisplayStart
和iDisplayLength
您将不得不在服务器端Java中重新实现此功能。
以下是我使用过的一些代码(使用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);
还有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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句