본문 바로가기

IT/개발

JQuery 사용자 정의 함수 = $.fn

개발을 요구사항중에 row의 값이 같은경우 해당 cell을 merge하라는 요구사항을 접수.

솔직히 아래의 로직은 구글링을 통해 많이 검색되는 함수..

그와는 별개로 $.fn.메소드명을 아래와 같이 정의하고 사용할 수 있다.

      $("#id").메소드(); 
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
/*   
 *   
 * 같은 값이 있는 열을 병합함  
 *   
 * 사용법 : $('#테이블 ID').rowspan(0);  
 *   
 */       
$.fn.rowspan = function(colIdx, isStats) {         
    return this.each(function(){        
        var that;       
        $('tr', this).each(function(row) {        
            $('td',this).eq(colIdx).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;        
            });       
        });      
    });    
};   
 
cs

이번 포스팅을 통해 JQuery에서 나만이 정의해논 함수를 선언하고 사용하고싶다면

위와같은방법으로 사용하면 좋을것 같다.