1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>rowspan example</title>
<script src="https://code.jquery.com/jquery-2.2.1.js"></script>
<script type="text/javascript">
 
$(function(){
    $('#rowTable').each(function() {
        var table = this;
        // rowspan할 tr(세로 기준)
        $.each([1,4], function(c, v) {
            var tds = $('>tbody>tr>td:nth-child(' + v + ')', table).toArray();
            var i = 0, j = 0;
            
            for(j = 1; j < tds.length; j ++) {
                if(tds[i].innerHTML != tds[j].innerHTML) {
                    $(tds[i]).attr('rowTable', j - i);
                    i = j;
                    continue;
                }
                $(tds[j]).hide();
            }
            j --;
            
            if(tds[i].innerHTML == tds[j].innerHTML) {
                $(tds[i]).attr('rowTable', j - i + 1);
            }
        });
    });
});
 
</script>
</head>
<body>
    <h1>중복 데이터 셀 병합</h1>    
    <!-- table id를 적고 제이쿼리에서 id 값으로 불러온다. -->
    <table border="1" style="border-collapse:collapse;border:1px gray solid;" id="rowTable">
        <thead>
            <tr>
                <th>제목</th>
                <th>제목</th>
                <th>제목</th>
                <th>제목</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
            </tr>
            <tr>
                <td>a</td>
                <td>b</td>
                <td>c</td>
                <td>d</td>
            </tr>
            <tr>
                <td>a</td>
                <td>2</td>
                <td>3</td>
                <td>d</td>
            </tr>
        </tbody>
    </table>
</body>
</html>
cs

colspan도 이와 같은 방식으로 적용시키면 된다.

+ Recent posts