57jqGrid - 数据合并单元格

简介: 57jqGrid - 数据合并单元格

在colModel中使用新的事件cellattr,我们可以轻松的配置数据跨度。

需要注意的是我们通过单元格属性,我们可以将任何有效的属性设置到单元格上,包括样式。

HTML代码举例

<html>
  <head>
    <title>jqGrid 实例</title>
  </head>
  <body>
    ···代码省略···
    <table id="colspan"></table> 
    <div id="pcolspan"></div>
    ···代码省略···
  </body>
</html>

javascript代码举例

$(function(){
  pageInit();
});
function pageInit(){
  jQuery("#colspan").jqGrid({
       url:ctx+'/JSONNameData',
    datatype: "json",
       colNames:['Date', 'Inv No', 'Client', 'Amount','Tax','Total','Notes'],
       colModel:[
         {name:'invdate',index:'invdate', width:90},
         {
        name:'id',
        key : true,
        index:'id',
        width:55,
        cellattr: function(rowId, value, rowObject, colModel, arrData) {
          return ' colspan=2';
        },
        formatter : function(value, options, rData){
          return "Invoce: "+value + " - "+rData['name'];
        }
      },
         {name:'name', index:'name',  width:100,
        cellattr: function(rowId, value, rowObject, colModel, arrData) {
          return " style=display:none; ";
        }
      },
         {name:'amount',index:'amount', width:80, align:"right"},
         {name:'tax',index:'tax', width:80, align:"right"},
         {name:'total',index:'total', width:80,align:"right"},
         {name:'note',index:'note', width:150, sortable:false}
       ],
       rowNum:10,
      width:700,
       rowList:[10,20,30],
       pager: '#pcolspan',
       sortname: 'invdate',
      viewrecords: true,
      sortorder: "desc",
    jsonReader: {
      repeatitems : false
    },
    caption: "Data colspan",
    height: '100%'
  });
  jQuery("#colspan").jqGrid('navGrid','#pcolspan',{edit:false,add:false,del:false});
}

java servlet代码举例

package net.mn886.blog.jqgrid.functionality;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class JSONNameData
 */
public class JSONNameData extends HttpServlet {
  private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JSONNameData() {
        super();
        // TODO Auto-generated constructor stub
    }
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);
  }
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String page = request.getParameter("page");
    String jsondata = "{}";
    if("1".equals(page)){
      jsondata = "{" +
          "    \"page\":\"1\"," +
          "    \"total\":2," +
          "    \"records\":\"13\"," +
          "    \"rows\":[" +
          "      {\"id\":\"10\",\"invdate\":\"2007-10-06\",\"name\":\"Client 2\",\"amount\":\"100.00\",\"tax\":\"20.00\",\"total\":\"120.00\",\"note\":null}," +
          "      {\"id\":\"11\",\"invdate\":\"2007-10-06\",\"name\":\"Client 1\",\"amount\":\"600.00\",\"tax\":\"120.00\",\"total\":\"720.00\",\"note\":null}," +
          "      {\"id\":\"9\",\"invdate\":\"2007-10-06\",\"name\":\"Client 1\",\"amount\":\"200.00\",\"tax\":\"40.00\",\"total\":\"240.00\",\"note\":null}," +
          "      {\"id\":\"13\",\"invdate\":\"2007-10-06\",\"name\":\"Client 3\",\"amount\":\"1000.00\",\"tax\":\"0.00\",\"total\":\"1000.00\",\"note\":null}," +
          "      {\"id\":\"8\",\"invdate\":\"2007-10-06\",\"name\":\"Client 3\",\"amount\":\"200.00\",\"tax\":\"0.00\",\"total\":\"200.00\",\"note\":null}," +
          "      {\"id\":\"12\",\"invdate\":\"2007-10-06\",\"name\":\"Client 2\",\"amount\":\"700.00\",\"tax\":\"140.00\",\"total\":\"840.00\",\"note\":null}," +
          "      {\"id\":\"7\",\"invdate\":\"2007-10-05\",\"name\":\"Client 2\",\"amount\":\"120.00\",\"tax\":\"12.00\",\"total\":\"134.00\",\"note\":null}," +
          "      {\"id\":\"6\",\"invdate\":\"2007-10-05\",\"name\":\"Client 1\",\"amount\":\"50.00\",\"tax\":\"10.00\",\"total\":\"60.00\",\"note\":\"\"}," +
          "      {\"id\":\"5\",\"invdate\":\"2007-10-05\",\"name\":\"Client 3\",\"amount\":\"100.00\",\"tax\":\"0.00\",\"total\":\"100.00\",\"note\":\"no tax at all\"}," +
          "      {\"id\":\"4\",\"invdate\":\"2007-10-04\",\"name\":\"Client 3\",\"amount\":\"150.00\",\"tax\":\"0.00\",\"total\":\"150.00\",\"note\":\"no tax\"}" +
          "    ]" +
          "  }";
    }else{
      jsondata = "{" +
          "    \"page\":\"2\"," +
          "    \"total\":2," +
          "    \"records\":\"13\"," +
          "    \"rows\":[" +
          "      {\"id\":\"2\",\"invdate\":\"2007-10-03\",\"name\":\"Client 1\",\"amount\":\"200.00\",\"tax\":\"40.00\",\"total\":\"240.00\",\"note\":\"note 2\"}," +
          "      {\"id\":\"3\",\"invdate\":\"2007-10-02\",\"name\":\"Client 2\",\"amount\":\"300.00\",\"tax\":\"60.00\",\"total\":\"360.00\",\"note\":\"note invoice 3 & and amp test\"}," +
          "      {\"id\":\"1\",\"invdate\":\"2007-10-01\",\"name\":\"Client 1\",\"amount\":\"100.00\",\"tax\":\"20.00\",\"total\":\"120.00\",\"note\":\"note 1\"}" +
          "    ]" +
          "  }";
    }
    response.getWriter().write(jsondata);
  }
}

目录
相关文章
|
前端开发
表格插件-bootstrap table的隔行换色
表格插件-bootstrap table的隔行换色
125 0
|
JavaScript 前端开发 Java
23jqGrid - 列宽
23jqGrid - 列宽
49 0
|
JavaScript 前端开发 Java
72jqGrid 分组表头 - 有合并列的样式
72jqGrid 分组表头 - 有合并列的样式
58 0
|
JavaScript
jqgrid清空表格
jqgrid清空表格
53 0
|
JSON JavaScript 前端开发
77jqGrid - 带有汇总页脚的分组
77jqGrid - 带有汇总页脚的分组
39 0
|
6月前
|
JavaScript
表格换色的实现方法
表格换色的实现方法
|
6月前
elementUI 写一个表头列名、表体单元格样式、翻页器相对较为动态的表格el-table
elementUI 写一个表头列名、表体单元格样式、翻页器相对较为动态的表格el-table
elementUI 写一个表头列名、表体单元格样式、翻页器相对较为动态的表格el-table
|
JavaScript 前端开发 Java
58jqGrid - 数据合并单元格
58jqGrid - 数据合并单元格
127 0
|
JavaScript 前端开发 Java
71jqGrid 分组表头 - 没有合并列的样式
71jqGrid 分组表头 - 没有合并列的样式
31 0
|
JavaScript 前端开发 Java
22jqGrid - 多级表格2
22jqGrid - 多级表格2
41 0