简体   繁体   English

jqgrid java服务器端分页

[英]jqgrid java server side paging

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. 提前致谢。

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. 服务器端分页的逻辑:假设您总共有1000条记录,每页必须显示为50条记录。 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. 我假设您只在显示第一页中的记录时拉出前50条记录,然后单击“下一步”按钮要从数据库中检索要在网格中显示的下50条记录。

You don't need the onPaging : function. 您不需要onPaging:函数。 Just setting paging:true will suffice. 仅将pageing:true设置就足够了。

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

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. //假设您总共有1000条记录。 This should be set dynamically. 应该动态设置。 For time being it is hardcoded to 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;

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

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