jqgrid java服务器端分页

发布于 2025-01-06 15:01:29 字数 414 浏览 0 评论 0原文

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

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

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

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

提前致谢。

I'm trying to add server side sorting to my grid, so I added this:

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

and when ever I clicked the next page button it went to my server and loaded the grid again, so I see the records from the first page again. my question is how do I connect the data about the grid records and my server? Is there a full example of server side paging? what else do I need to pass into my server inorder to get the currect records for the next page? what do i neeed to add to my web page and what to the server page?

Any help will be appritiated,

Thank's In Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2025-01-13 15:01:29

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

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

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

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

// 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;

Please confirm are you doing server side sorting or server side paging. From the question I understand that you are trying to retrieve the next page data from the server on click of next/prev button in the grid. If your objective is just to get the paging data then below logic would help. If you are interested in server side sort + server side paging a similar approach needs to be followed for that.

Logic for Server side paging:
Lets assume you have total of 1000 records which has to be displayed as 50 records per page.
I am assuming that you only pull the 1st 50 records while displaying the records in first page and then on click of next button you want to retrieve the next 50 records to be displayed in the grid from the database.

You don't need the onPaging : function. Just setting paging:true will suffice.

Have following variables in the java class with getter and 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;
    }

After that what you need is some calculations to set the fields for the grid as per retrieved records.

// Assuming you have 1000 records in total. This should be set dynamically. For time being it is hardcoded to 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文