java - jqgrid java服务器端分页

标签 java javascript jakarta-ee jqgrid

我正在尝试将服务器端排序添加到我的网格中,因此我添加了以下内容:

onPaging : function(which_button) {
                 _$("#myGrid").setGridParam({datatype: "xml"});
                _$("#myGrid").trigger('reloadGrid');
            }

当我单击下一页按钮时,它会转到我的服务器并再次加载网格,因此我再次看到第一页中的记录。我的问题是如何连接有关网格记录和我的服务器的数据?有服务器端分页的完整示例吗?我还需要将什么传递到我的服务器才能获取下一页的当前记录?我需要向我的网页添加什么以及向服务器页面添加什么?

如有任何帮助,我们将不胜感激,

提前致谢。

最佳答案

请确认您正在执行服务器端排序还是服务器端分页。从问题中我了解到您正在尝试通过单击网格中的下一个/上一个按钮从服务器检索下一页数据。如果您的目标只是获取分页数据,那么以下逻辑将会有所帮助。如果您对服务器端排序+服务器端分页感兴趣,则需要遵循类似的方法。

服务器端分页逻辑: 假设您总共有 1000 条记录,每页必须显示 50 条记录。 我假设您在第一页中显示记录时仅提取前 50 条记录,然后单击下一步按钮时您希望从数据库中检索要在网格中显示的下 50 条记录。

您不需要 onPaging : 函数。只需设置 paging:true 就足够了。

在 java 类中具有以下带有 getter 和 setter 的变量

// Total pages
    private Integer total = 0;
     //get how many rows we want to have into the grid - rowNum attribute in the grid
    private Integer rows = 0;
    //Get the requested page. By default grid sets this to 1.
    private Integer page = 0;
    // All Record
    private Integer records = 0;
    // sorting order ascending or descending
    private String sord;
    // get index row - i.e. user click to sort
    private String sidx;
/**
     * @return the total
     */
    public Integer getTotal() {
        return total;
    }

    /**
     * @param total the total to set
     */
    public void setTotal(Integer total) {
        this.total = total;
    }

    /**
     * @return the rows
     */
    public Integer getRows() {
        return rows;
    }

    /**
     * @param rows the rows to set
     */
    public void setRows(Integer rows) {
        this.rows = rows;
    }

    /**
     * @return the page
     */
    public Integer getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(Integer page) {
        this.page = page;
    }

    /**
     * @return the records
     */
    public Integer getRecords() {
        return records;
    }

    /**
     * @param records the records to set
     */
    public void setRecords(Integer records) {
        this.records = records;

        if(this.records > 0 && this.rows > 0){
            this.total = (int)Math.ceil((double) this.records/(double) this.rows);
        }else{
            this.total = 0;
        }
    }

    /**
     * @return the sord
     */
    public String getSord() {
        return sord;
    }

    /**
     * @param sord the sord to set
     */
    public void setSord(String sord) {
        this.sord = sord;
    }

    /**
     * @return the sidx
     */
    public String getSidx() {
        return sidx;
    }

    /**
     * @param sidx the sidx to set
     */
    public void setSidx(String sidx) {
        this.sidx = sidx;
    }

之后,您需要进行一些计算,以根据检索到的记录设置网格的字段。

//假设你总共有1000条记录。这应该动态设置。目前它被硬编码为 1000。

setRecords(1000);
// for first time when we have page=0, it should 
// be page =1;
// If last page is required and if page no crosses total count
                int displayCount = count/rows;
                int remainder = count%rows;
                page = (page<=displayCount?page:count==0?0:remainder==0?displayCount:displayCount+1);


                int to = (getRows() * getPage());
                int from = to - getRows();

                if (to > getRecords()) to = getRecords();

                if (from > to) {
                    from = 0;
                    page = 1;
                }

setTotal((int) Math.ceil((double) getRecords() / (double) getRows()));

                if(getTotal() == 0) page =0;

关于java - jqgrid java服务器端分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9296522/

相关文章:

java - 能否用轮询 Web 服务调用代替 JMS 消息传递?

java - 使用 spring jpa 逐行获取数据库

javascript 对象作为参数不返回正确的对象

javascript - 点击子菜单不显示

javascript - 客户端提交无效 JSON : lexical error: invalid string in json text. ↵

java - 设置自定义字段名称和代码

java - 获取 java.lang.OutOfMemoryError : Java heap space when creating images(thumbnails/images) from pdfFile using PDFRender API

java - 在比较您知道长度始终为 1 的字符串时,有什么更好的表现?

java - Mockito 的参数捕捉器行为

java - 为什么 TomCat 8 不能在我的 Ubuntu 15.04 系统上运行?