【问题标题】:jqgrid java server side pagingjqgrid java服务器端分页
【发布时间】:2012-02-15 15:48:28
【问题描述】:

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

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

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

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

提前致谢。

【问题讨论】:

    标签: java javascript jakarta-ee jqgrid


    【解决方案1】:

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

    服务器端分页的逻辑: 假设您总共有 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;
    

    【讨论】:

    • 感谢您的回答!我会试一试。如您所知,我想要服务器端分页和客户端排序。我应该将服务器端的变量添加到 jqrid 吗?或者当我单击下一个/上一个按钮时,这些值会自动从 jqgrid 到服务器端?再次感谢。
    • 由于要实现服务器端分页,所以为了对网格进行排序,需要从服务器拉取剩余数据。因此客户端排序将不起作用。您必须在数据库查询中使用动态 order by 子句为您对结果集进行排序。你可以使用这样的东西 if(sidx != null && ! sidx.equals("")){ setOrderByField(sortMap.get(sidx)); setOrderBy(sord); }else{ // 如果没有选择排序,则按照默认排序(如果有)。 setOrderByField("employeeId"); setOrderBy(desc); }
    • 您需要设置属性文件或静态映射来将网格列名称与数据库列名称进行映射。而且所有与排序或分页相关的变量都默认存在于网格中,因此您不需要在网格中从外部指定它们..
    • 能否提供完整的代码?即服务器端和客户端。
    猜你喜欢
    • 2015-09-07
    • 2014-05-17
    • 1970-01-01
    • 2010-10-31
    • 2011-06-16
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多