2012-02-15 100 views
1

我试图添加服务器端排序到我的网格,所以我加了这一点:jqGrid的Java服务器端分页

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

,并当过我点击翻页按钮,它去到我的服务器,并加载网格,所以我再次看到第一页的记录。我的问题是如何连接有关网格记录和我的服务器的数据?有没有服务器端分页的完整示例?我还需要传递给我的服务器才能获得下一页的正确记录?我需要添加到我的网页和什么服务器页面?

任何帮助将appritiated,

感谢的在进展。

回答

1

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

服务器端分页逻辑: 假设您总共有1000条记录,每页显示为50条记录。 我假设您只在第一页显示记录的同时拉出前50条记录,然后单击下一个按钮,您要从数据库中检索要在网格中显示的下50条记录。

您不需要onPaging:函数。只需设置分页: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; 
+0

谢谢你的回答!我会试一试。正如你所理解的,我想有服务器端分页和客户端排序。我应该将服务器端的变量添加到jqrid中吗?或者当我点击下一个/ prev按钮时,这些值自动从jqgrid到服务器端?再次感谢。 – user590586 2012-02-19 08:58:09

+0

由于您希望实现服务器端分页,因此为了排序网格,您需要从服务器中提取剩余的数据。因此,客户端排序不起作用。您将不得不在数据库查询中使用动态排序顺序来为您排序结果集。您可以使用这样的事情,如果{\t \t \t \t \t \t \t \t \t setOrderByField(sortMap.get(SIDX))(SIDX = NULL && sidx.equals( “”)!); \t \t \t \t \t setOrderBy(sord); \t \t \t \t \t \t \t \t}其他{ \t \t \t \t \t //如果没有选择排序,然后如果有做的默认顺序。 \t \t \t \t \t setOrderByField( “雇员”); \t \t \t \t \t setOrderBy(降序); \t \t \t \t} – Ani 2012-02-20 19:16:36

+0

您将需要设置属性文件或静态映射以将网格列名与数据库列名进行映射。而且所有与排序或分页相关的变量都默认出现在网格中,因此您不需要在网格中对其进行外部指定。 – Ani 2012-02-20 19:22:20