js合并table儲存格執行個體

來源:互聯網
上載者:User

標籤:js 合并 儲存格 java script javascript

這裡展示js合并table的儲存格,代碼親測可行

後台採用springmvc搭建


Record實體類

public class Record {    public String isp;    public String large_area;    public String province;    public String name;    public String age;      ......   //省略get和set方法 }


action方法

        @RequestMapping(value="/handlerList")public ModelAndView handlerList(HttpServletRequest request,HttpServletResponse response) throws Exception {List list = new ArrayList();Record record1 = new Record();record1.setIsp("CUC");record1.setLarge_area("廣東");record1.setProvince("深圳");record1.setName("name1");record1.setAge("age1");Record record2 = new Record();record2.setIsp("CUC");record2.setLarge_area("廣東");record2.setProvince("廣州");record2.setName("name2");record2.setAge("age2");Record record3 = new Record();record3.setIsp("NU");record3.setLarge_area("江西");record3.setProvince("宜春");record3.setName("name3");record3.setAge("age3");Record record4 = new Record();record4.setIsp("NU");record4.setLarge_area("江西");record4.setProvince("宜春");record4.setName("name4");record4.setAge("age4");Record record5 = new Record();record5.setIsp("NU");record5.setLarge_area("江西");record5.setProvince("贛州");record5.setName("name5");record5.setAge("age5");Record record6 = new Record();record6.setIsp("NU");record6.setLarge_area("湖南");record6.setProvince("郴州");record6.setName("name6");record6.setAge("age6");list.add(record1);list.add(record2);list.add(record3);list.add(record4);list.add(record5);list.add(record6);Map map = new HashMap();map.put("list", list);return new ModelAndView("/showList",map);}


jsp頁面展示

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript"src="asserts/js/jquery-1.7.2.min.js"></script>    </head><body>    <table id="testTable" width="100%" border="1"><thead>   <tr align="center">     <th width="94" height="38">電訊廠商</th>     <th width="198" height="38" >7天平均冗餘頻寬(G)</th>     <th width="216" height="38" >今天冗餘頻寬(G)</th>     <th width="196" height="38" >未來30天冗餘頻寬(G)</th>     <th width="263" height="38" >目前已用頻寬(G)</th>   </tr>   </thead>   <thbody>        <c:forEach var="item" items="${list}" varStatus="status">    <tr>       <td>${item.isp }</td>    <td>${item.large_area}</td><td>${item.province}</td><td>${item.name}</td><td>${item.age}</td>    </tr>     </c:forEach>   </thbody>   </table>   </body></html>

 

可以看到未合并時效果如下:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/47/00/wKiom1P1kLGhbTLtAAGpUr5Azgo260.jpg" title="}U{{Q_NKAW7%YFDO~4}W]0X.jpg" alt="wKiom1P1kLGhbTLtAAGpUr5Azgo260.jpg" />

jsp頁面中添加相關的js片段後

<SCRIPT type="text/javascript">$(document).ready(function () {      table_rowspan("#testTable", 1);      table_rowspan("#testTable", 2);      table_rowspan("#testTable", 3);    })//函數說明:合并指定表格(表格id為table_id)指定列(列數為table_colnum)的相同文本的相鄰儲存格//參數說明:table_id 為需要進行合併儲存格的表格的id。如在HTMl中指定表格 id="table1" ,此參數應為 #table1//參數說明:table_colnum 為需要合併儲存格的所在列。為數字,從最左邊第一列為1開始算起。        function table_rowspan(table_id, table_colnum) {            table_firsttd = "";            table_currenttd = "";            table_SpanNum = 0;            colnum_Obj = $(table_id + " tr td:nth-child(" + table_colnum + ")");            colnum_Obj.each(function (i) {                if (i == 0) {                    table_firsttd = $(this);                    table_SpanNum = 1;                } else {                    table_currenttd = $(this);                    if (table_firsttd.text() == table_currenttd.text()) {                        table_SpanNum++;                        table_currenttd.hide(); //remove();                        table_firsttd.attr("rowSpan", table_SpanNum);                    } else {                        table_firsttd = $(this);                        table_SpanNum = 1;                    }                }            });        }                        //函數說明:合并指定表格(表格id為table_id)指定行(行數為table_rownum)的相同文本的相鄰儲存格        //參數說明:table_id 為需要進行合併儲存格的表格id。如在HTMl中指定表格 id="table1" ,此參數應為 #table1        //參數說明:table_rownum 為需要合併儲存格的所在行。其參數形式請參考jQuery中nth-child的參數。        //          如果為數字,則從最左邊第一行為1開始算起。        //          "even" 表示偶數行        //          "odd" 表示奇數行        //          "3n+1" 表示的行數為1、4、7、10.......        //參數說明:table_maxcolnum 為指定行中儲存格對應的最大列數,列數大於這個數值的儲存格將不進行比較合并。        //          此參數可以為空白,為空白則指定行的所有儲存格要進行比較合并。        function table_colspan(table_id, table_rownum, table_maxcolnum) {            if (table_maxcolnum == void 0) {                table_maxcolnum = 0;            }            table_firsttd = "";            table_currenttd = "";            table_SpanNum = 0;            $(table_id + " tr:nth-child(" + table_rownum + ")").each(function (i) {                row_Obj = $(this).children();                row_Obj.each(function (i) {                    if (i == 0) {                        table_firsttd = $(this);                        table_SpanNum = 1;                    } else if ((table_maxcolnum > 0) && (i > table_maxcolnum)) {                        return "";                    } else {                        table_currenttd = $(this);                        if (table_firsttd.text() == table_currenttd.text()) {                            table_SpanNum++;                            table_currenttd.hide(); //remove();                            table_firsttd.attr("colSpan", table_SpanNum);                        } else {                            table_firsttd = $(this);                            table_SpanNum = 1;                        }                   }                });            });        }         </SCRIPT>



效果如下,可以看到欄位相同的列已經進行合并了:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/47/01/wKiom1P1kubgapSoAAGYgSkVvRw379.jpg" title="E_5YGWBPK3A7N29}}@XYQ%A.jpg" alt="wKiom1P1kubgapSoAAGYgSkVvRw379.jpg" />

本文出自 “bulajunjun” 部落格,請務必保留此出處http://5148737.blog.51cto.com/5138737/1543091

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.