[jQuery] Table 셀 병합 ( colspan / rowspan ) 하는법

2019. 6. 27. 15:25
반응형

[jQuery] Table 셀 병합 ( colspan / rowspan ) 하는법


테이블 결과값이 같을 경우 셀을 병합하여 

보다 보기 편하게 사용할때 사용하는 방법이다


기존 다음과 같은표를

 학년

이름 

 1

 5

 제니퍼

 1

 1

 칼

 2

 5

 존


▼▼▼▼▼▼▼▼▼

다음과 같이 변형 할때 사용하는 방법이다.

 학년

이름 

 1

 5

 제니퍼

 1

 칼

 2

 5

 존


팁*

혹시 테이블 읽는데 시간이 걸려 병합되는과정이 보인다면 테이블을 히든처리한다음 

병합후 보여주는 기능을 사용하면 좋을듯하다.



▼  Jquery 사용 예제

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
73
74
75
76
77
78
79
80
jQuery(function() {
  
  $('#테이블 ID').rowspan(0); 
  $('#테이블 ID').rowspan(1);
 
 });
 
 
 
/* 
 * 
 * 같은 값이 있는 열을 병합함
 * 
 * 사용법 : $('#테이블 ID').rowspan(0);
 * 
 */     
$.fn.rowspan = function(colIdx, isStats) {       
    return this.each(function(){      
        var that;     
        $('tr', this).each(function(row) {      
            $('td:eq('+colIdx+')', this).filter(':visible').each(function(col) {
                
                if ($(this).html() == $(that).html()
                    && (!isStats 
                            || isStats && $(this).prev().html() == $(that).prev().html()
                            )
                    ) {            
                    rowspan = $(that).attr("rowspan"|| 1;
                    rowspan = Number(rowspan)+1;
 
                    $(that).attr("rowspan",rowspan);
                    
                    // do your action for the colspan cell here            
                    $(this).hide();
                    
                    //$(this).remove(); 
                    // do your action for the old cell here
                    
                } else {            
                    that = this;         
                }          
                
                // set the that if not already set
                that = (that == null) ? this : that;      
            });     
        });    
    });  
}; 
 
 
/* 
 * 
 * 같은 값이 있는 행을 병합함
 * 
 * 사용법 : $('#테이블 ID').colspan (0);
 * 
 */   
$.fn.colspan = function(rowIdx) {
    return this.each(function(){
        
        var that;
        $('tr', this).filter(":eq("+rowIdx+")").each(function(row) {
            $(this).find('th').filter(':visible').each(function(col) {
                if ($(this).html() == $(that).html()) {
                    colspan = $(that).attr("colSpan"|| 1;
                    colspan = Number(colspan)+1;
                    
                    $(that).attr("colSpan",colspan);
                    $(this).hide(); // .remove();
                } else {
                    that = this;
                }
                
                // set the that if not already set
                that = (that == null) ? this : that;
                
            });
        });
    });
}
cs


※ 참고 : http://willifirulais.blogspot.com/2007/07/jquery-table-column-break.html

반응형

BELATED ARTICLES

more