select2,利用ajax高效查询大数据列表2(可搜索、可分页)

接下来,把关键的源码贴出来,可能和你的项目不吻合,但可以参考。


BaseConditionVO.java

public class BaseConditionVO {
    public final static int PAGE_SHOW_COUNT = 50;
    private int pageNum = 1;
    private int numPerPage = 0;
    private int totalCount = 0;
    private String orderField;
    private String orderDirection;

    /**
     * @Fields ps : 对参数类型进行封装.
     */
    private Map<String, Object> mo = new HashMap<String, Object>();

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getNumPerPage() {
        return numPerPage > 0 ? numPerPage : PAGE_SHOW_COUNT;
    }

    public void setNumPerPage(int numPerPage) {
        this.numPerPage = numPerPage;
    }

    public String getOrderField() {
        return orderField;
    }

    public void setOrderField(String orderField) {
        this.orderField = orderField;
    }

    public String getOrderDirection() {
        return "desc".equals(orderDirection) ? "desc" : "asc";
    }

    public void setOrderDirection(String orderDirection) {
        this.orderDirection = orderDirection;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getStartIndex() {
        int pageNum = this.getPageNum() > 0 ? this.getPageNum() - 1 : 0;
        return pageNum * this.getNumPerPage();
    }

    public RowBounds createRowBounds() {
        RowBounds ro = new RowBounds(this.getStartIndex(), this.getNumPerPage());
        return ro;
    }

    /**
     * @Title: addParams
     * @Description: 添加查询条件
     * @param key
     * @param value
     */
    public void addParams(String key, Object value) {
        this.getMo().put(key, value);
    }

    /** 
    * @Title: getParams 
    * @Description: 获取查询条件
    * @param key
    * @return
    */
    public Object getParams(String key) {
        return this.getMo().get(key);
    }

    /**
     * @return the mo
     */
    public Map<String, Object> getMo() {
        return mo;
    }

    /**
     * @param mo
     *            the mo to set
     */
    public void setMo(Map<String, Object> mo) {
        this.mo = mo;
    }
}


selec2的分页和Java端分页参数匹配


protected BaseConditionVO getBaseConditionVOForTable(HttpServletRequest req) {
    BaseConditionVO vo = new BaseConditionVO();
    // 当前页
    int currentPage = StrUtil.parseStringToInt(req.getParameter("page"));
    // 一页显示多少行
    int sizes = StrUtil.parseStringToInt(req.getParameter("rows"));
    // 排序
    String sortOrder = StrUtil.getString(req.getParameter("sord"));
    String sortCol = StrUtil.getString(req.getParameter("sidx"));
    vo.setNumPerPage(sizes);
    vo.setPageNum(currentPage);
    vo.setOrderField(sortCol);
    vo.setOrderDirection(sortOrder);
    return vo;
}

Java端到select2端的数据封装

@XStreamAlias("pageGrid")
@SuppressWarnings("rawtypes")
public class PageGrid {
    private int page;
    // 总页数,和select2的processResults.pagination匹配
    private int total;
    private int records;

    // 数据结果集,和select2的processResults.results匹配
    private List data;

    public int getPage() {
        return this.page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getTotal() {
        return this.total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getRecords() {
        return this.records;
    }

    public void setRecords(int records) {
        this.records = records;
    }

    public List getData() {
        return this.data;
    }

    public void setData(List data) {
        this.data = data;
    }
}
上一篇:云安全常见的AWS错误配置


下一篇:mysql中按in中的数据排序