by Nathan

테이블 동적 생성 본문

Programming/JavaScript, Json

테이블 동적 생성

넷쓴 2016. 10. 25. 11:00



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<script type="text/javascript" src="/js/jquery.table.min.js"></script>
 
 
<script type="text/javascript">
//<![CDATA[
 
$(document).ready(function(){
    // 슬롯 추가 버튼
    $("#addItemBtn").click(function(){
        var lastItemNo = $("#slotList tr:last").attr("class").replace("item"""); // 가장 마지막 로우 번호
        var newitem = $("#slotList tr:eq(1)").clone();
        newitem.removeClass();
        newitem.addClass("item"+(parseInt(lastItemNo)+1));
         // newitem 객체 초기값으로 생성
        newitem.find("span:eq(0)").text(parseInt(lastItemNo)+1);
        newitem.find("input:eq(0)").val("");
        newitem.find("input:eq(1)").val("");
        newitem.find("input:eq(2)").val("");
        $("#slotList").append(newitem);
    });
 
    // 추가 버튼
    $(".addBtn").live("click"function(){
        // 현재
        var curRow = $(this).parent().parent();
        var curCls = curRow.attr("class");
 
        // 생성될 클래스명
        var crtCls = "";
 
        // 다음
        var nxtRow = null;
        var nxtCls = "";
 
        // 생성될 클래스명, 다음 클래스명 중복일 경우 모두 +1 update
        var len = $("#slotList tr").length-1;
        for(var i=0; i<len; i++){
            // 현재
            if(i > 0){
                curRow = curRow.next();
                curCls = curRow.attr("class");
            }
            // 생성될 클래스명
            crtCls = "item"+(parseInt(curCls.replace("item"""))+1);
            // 다음
            nxtRow = curRow.next();
            nxtCls = nxtRow.attr("class");
            if(i > 0 && nxtCls != null){
                nxtCls = "item"+(parseInt(nxtCls.replace("item"""))+1);
            }
            // 클래스명 변경
            if(crtCls == nxtCls && nxtCls != null){
                nxtRow.removeClass();
                if(i == 0){
                    nxtRow.addClass("item"+(parseInt(nxtCls.replace("item"""))+1));
                    nxtRow.find("span:eq(0)").text((parseInt(nxtCls.replace("item"""))+1));
                }else{
                    nxtRow.addClass(nxtCls);
                    nxtRow.find("span:eq(0)").text(nxtCls.replace("item"""));
                }
            }else{
                curRow = $(this).parent().parent();
                curCls = curRow.attr("class");
                crtCls = "item"+(parseInt(curCls.replace("item"""))+1);
                break;
            }
        }
        
         // 추가 버튼 다음줄에 새로운 row 추가
        var newrow = curRow.clone();
        newrow.removeClass();
        newrow.addClass(crtCls);
        // newrow 객체 초기값으로 생성
        newrow.find("span:eq(0)").text(crtCls.replace("item"""));
        newrow.find("input:eq(0)").val("");
        newrow.find("input:eq(1)").val("");
        newrow.find("input:eq(2)").val("");
        newrow.insertAfter($("#slotList ."+curCls));
    });
    
    // 삭제 버튼
    $(".delBtn").live("click"function(){
        // 현재
        var curRow = $(this).parent().parent();
        var curCls = curRow.attr("class");
        // 다음
        var nxtRow = null;
        var nxtCls = "";
 
        // 생성될 클래스명, 다음 row가 있을 경우, 클래스명을 모두 -1 update
        var len = $("#slotList tr").length-1;
        for(var i=0; i<len; i++){
            // 다음
            if(i == 0){
                nxtRow = curRow.next();
            }else{
                nxtRow = nxtRow.next();
            }
            nxtCls = nxtRow.attr("class");
 
            // 클래스명 변경
            if(nxtCls != null){
                nxtRow.removeClass();
                nxtRow.addClass("item"+(parseInt(nxtCls.replace("item"""))-1));
                nxtRow.find("span:eq(0)").text(parseInt(nxtCls.replace("item"""))-1);
            }else{
                curRow = $(this).parent().parent();
                curCls = curRow.attr("class");
                break;
            }
        }
        curRow.remove();
    });
});
 
//]]>
</script>
 
 
 
<body>
 
    <table id="slotList" border="1px" style="width: 100%;">        
        <tr>
            <th>번호</th>
            <th>항목A</th>
            <th>항목B</th>
            <th>항목C</th>
            <th>추가/삭제</th>
        </tr>
        <tr class="item1">
            <td><span>1</span></td>
            <td><input type="text" id="" name="" value="" /></td>
            <td><input type="text" id="" name="" value="" /></td>
            <td><input type="text" id="" name="" value="" /></td>
            <td><button class="addBtn">추가</button><button class="delBtn">삭제</button></td>
        </tr>
        <tr class="item2">
            <td><span>2</span></td>
            <td><input type="text" id="" name="" value="" /></td>
            <td><input type="text" id="" name="" value="" /></td>
            <td><input type="text" id="" name="" value="" /></td>
            <td><button class="addBtn">추가</button><button class="delBtn">삭제</button></td>
        </tr>
    </table>
    <button id="addItemBtn">슬롯 추가</button>
 
</body>
 

cs

 


 

jquery.table.min.js

 

'Programming > JavaScript, Json' 카테고리의 다른 글

라디오 버튼 선택에 따른 div 보이기/숨기기  (0) 2018.07.24
반복문 4가지  (0) 2017.03.17
[유효성]필수값 체크  (0) 2016.09.06
[유효성]자바스크립트 함수  (0) 2016.09.05
현재 URL 확인하기  (0) 2016.01.12
Comments