[JavaScript] 카테고리 필터로 원하는 값 표시하기

리트리버J

·

2020. 11. 11. 22:39

728x90

1. 선택한 필터 란의 값에 카테고리가 추가되면 그 추가된 카테고리만 표시되게 하고 싶었다.

 

2. JavaScript로 구현하였다.

 

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
        /***** 카테고리 *****/
        let filter = function(){
            // 선택한 카테고리가 있을때만 실행
            if($(".last-nav-td").children().length != 0){
                
                let choiceCate = []; // 선택한 카테고리 배열
                let cate;              // 상품 카테고리 배열
        
                let result = 0;      // 결과값
                                    
                // 클릭한 카테고리를 배열로 만들기
                $(".last-nav-td").children().each(function(index,item){
                    
                    // 클릭한 3분류 카테고리와
                    let singleCategory = item.innerHTML;
                    choiceCate.push(singleCategory);
                });
                
                // 상품 하나의 카테고리를 배열로 만들기
                $(".pCategory").each(function(index,item){
                    
                    // product가 갖고 있는 카테고리
                    cate = item.value.split(",");
                    
                    // 상품 카테고리 배열 <=> 선택한 카테고리 배열 비교
                    for(var i = 0; i < choiceCate.length; i++){
                        console.log("선택한 카테고리 : " + choiceCate[i]);
                        for(var j = 0; j < cate.length; j++){
                            console.log(i + "번 상품의 " + j + "번째 카테고리" + cate[j]);
                            if(cate[j] == choiceCate[i]){
                                result++;
                            }
                        }
                    }
                    
                    console.log(result);
                    
                    // 선택한 카테고리의 갯수와 result를 비교, 같으면 전체 카테고리가 전부 포함되어 있는 것이므로 off 클래스 제거
                    if(result == $(".last-nav-td").children().length){
                        $(this).parent().removeClass('off');
                    }
                    // 같지 않다면 카테고리가 1개라도 부족하므로 off 클래스 추가
                    else{
                        $(this).parent().addClass('off');
                    }
                    
                    // 결과값 초기화
                    result = 0;
                });
            }                
            // 선택한 필터가 아무것도 없을 때 off를 지워서 전체가 보일 수 있도록 한다.
            else{
                $(".pCategory").parent().removeClass('off');
            }
        }
        /***** 카테고리 *****/
cs

 

3. 전체코드

 

 

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 
<!DOCTYPE html>
<html lang="ko">
   <head>
      <meta charset="UTF-8">
      <title>list</title>
      <!--폰트-->
      <!-- BLACK HAN SANS 폰트 -->
      <link href="https://fonts.googleapis.com/css2?family=Black+Han+Sans&display=swap" rel="stylesheet">
      <!-- Noto-Sans 폰트-->
      <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR&display=swap" rel="stylesheet">
      <!-- JUA 폰트-->
      <link href="https://fonts.googleapis.com/css2?family=Jua&display=swap" rel="stylesheet">
      <!--제이쿼리CDN-->
      <script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
      <link rel="stylesheet" type="text/css" href="resources/css/productList.css">
      <!-- 폰트 어썸 -->
      <script src="https://kit.fontawesome.com/fef720d792.js" crossorigin="anonymous"></script>
 
   </head>
   <body>
      <div class="header">   
         <%@ include file="../../common/header.jsp" %>
         </div>
      <div class="List-NavBox">
         <div class="List-NavBoxFont">
            <h3>&nbsp;&nbsp; > &nbsp;&nbsp;<span id="category1"></span></h3>
         </div>
         <table class="table_plist">
         
         <!--<c:set var="cd" value="JSON.parse('${categoryList}')"/>-->
         
       <c:forEach var="c" items="${ categoryList2 }" varStatus="status2">
            <tr>
                <td class="List-NavTableTd">
                    <class="font_noto List-NavPtag">
                        <c:if test="${c.level == 1}">
                               ${ c.cateName }
                      </c:if>
                    </p>
                </td>
                <td id="testTable">
                    <c:forEach var="c" items="${ categoryList }" varStatus="status1">
                            <c:if
                                test="${status2.current.cateCode == status1.current.cateCodeRef}">
                                <button class="font_noto List-Button">${ status1.current.cateName }</button>
                            </c:if>
                    </c:forEach>
                    <class="font_noto List-NavPtag"></p>
                </td>
            </tr>
        </c:forEach> 
            <tr>
               <td class="row Cancle-Container List-NavTableTd">
                  <span class="font_noto List-NavPtag">선택한필터</span>
                  <button class="font_jua List-NavButton">초기화</button>
               </td>
               <td class="last-nav-td">
               </td>
            </tr>
         </table>
         <div class="checkbox">
            <label class="font-noto Lsit-checkbox-size">
            <input type="checkbox" value="" > 접속중  </label>
            <select class="selectbox">
                <c:if test="${ what == null || what == '최신순'}">
                    <option>최신순</option>
                    <option>가격순</option>
                    <option>인기순</option>
                </c:if>
                <c:if test="${ what == '가격순'}">
                    <option>가격순</option>
                    <option>최신순</option>
                    <option>인기순</option>
                </c:if>
                <c:if test="${ what == '인기순'}">
                    <option>인기순</option>
                    <option>최신순</option>
                    <option>가격순</option>
                </c:if>    
            </select>
         </div>
         <!--부트스트랩 dropdown button-->
         <div>
            
         </div>
      </div>
      <!--리스트 섹션-->
    <div class="Lsit-Saction">
        <c:forEach var="product" items="${ productList }" varStatus="status">
            <div class="productOne">
                <input type="hidden" class="pCategory" value="${ product.category }">
                <a href="productDetail.do" class="thumbnail">
                    <div class="list_img_div">
                        <img src="resources/pUploadFiles/${ product.renamePic }" class="list_contents_img_index">
                    </div>
                </a>
                <div class="list_contents_marign">
                    <class="font_noto list_explain_index">${ product.title }</p>
                    <div>
                        <img src="resources/img/lv1.png" class="list_rank_index"> 
                        <span class="font_noto">${ product.nickName }</span> 
                        <span class="list_star_container_index"> 
                            <class="fas fa-star"></i>
                            <class="fas fa-star"></i>
                            <class="fas fa-star"></i>
                            <class="fas fa-star"></i>
                            <class="fas fa-star"></i>
                        </span> 
                        <span class="font_noto">(${ product.star })</span> 
                        <span> 
                            <span class="font_noto">${ product.count }명선택</span> 
                            <img src="resources/img/buy.png" class="list_choice_img_index">
                        </span>
                    </div>
                </div>
            </div>
        </c:forEach>
    </div>
    <!-- Paging -->
        <div class="pagination">
          <a href="#">&laquo;</a>
          <class="page_product">1</a>
          <a href="#" class="page_product">2</a>
          <a href="#" class="page_product">3</a>
          <a href="#" class="page_product">4</a>
          <a href="#" class="page_product">5</a>
          <a href="#" class="page_product">6</a>
          <a href="#" class="page_product">&raquo;</a>
        </div>
      
      <%@ include file="../../common/footer.jsp" %>
 
    <script>
    
        console.log("${productList}");
        // 최신순, 인기순, 가격순
        let tempvar = ${categoryList}[0].cateCode;
        
        $(".selectbox").change(function(){
            let temp = $(".selectbox option:selected").val();
            window.location.href = "productList.do?navNo=" + tempvar + "&what=" + temp;
        });
        
        /***** 카테고리 *****/
        let filter = function(){
            // 선택한 카테고리가 있을때만 실행
            if($(".last-nav-td").children().length != 0){
                
                let choiceCate = []; // 선택한 카테고리 배열
                let cate;              // 상품 카테고리 배열
        
                let result = 0;      // 결과값
                                    
                // 클릭한 카테고리를 배열로 만들기
                $(".last-nav-td").children().each(function(index,item){
                    
                    // 클릭한 3분류 카테고리와
                    let singleCategory = item.innerHTML;
                    choiceCate.push(singleCategory);
                });
                
                // 상품 하나의 카테고리를 배열로 만들기
                $(".pCategory").each(function(index,item){
                    
                    // product가 갖고 있는 카테고리
                    cate = item.value.split(",");
                    
                    // 상품 카테고리 배열 <=> 선택한 카테고리 배열 비교
                    for(var i = 0; i < choiceCate.length; i++){
                        console.log("선택한 카테고리 : " + choiceCate[i]);
                        for(var j = 0; j < cate.length; j++){
                            console.log(i + "번 상품의 " + j + "번째 카테고리" + cate[j]);
                            if(cate[j] == choiceCate[i]){
                                result++;
                            }
                        }
                    }
                    
                    console.log(result);
                    
                    // 선택한 카테고리의 갯수와 result를 비교, 같으면 전체 카테고리가 전부 포함되어 있는 것이므로 off 클래스 제거
                    if(result == $(".last-nav-td").children().length){
                        $(this).parent().removeClass('off');
                    }
                    // 같지 않다면 카테고리가 1개라도 부족하므로 off 클래스 추가
                    else{
                        $(this).parent().addClass('off');
                    }
                    
                    // 결과값 초기화
                    result = 0;
                });
            }                
            // 선택한 필터가 아무것도 없을 때 off를 지워서 전체가 보일 수 있도록 한다.
            else{
                $(".pCategory").parent().removeClass('off');
            }
        }
        /***** 카테고리 *****/
        
    
    
        $(function() {
            /*마우스클릭시 버튼 색상변경*/
            $('.List-Button').click(function() {
                $(this).toggleClass('yellowButton');
 
                let ClickWord = $(this).html();
                
                /* 제거 메소드 */
                $('.last-nav-td button').filter(function() {
                    if ($(this).html() === ClickWord) {
                        $(this).remove();
                    }
                });
                
                /*카테고리 클릭시 필터로 넘어가는 기능 (미완성)*/
                var btn = this.outerHTML;
                
                /* 추가 메소드 */
                if (this.className == "font_noto List-Button yellowButton") {
                    $('.last-nav-td').append(btn); 
                }
                
                filter();
            });
 
            /* 클릭 시 동적으로 추가 된 요소 제거*/
            /* 
               왜 $('.last-nav-td button').click(function(){})이 작동되지 않을까??
               
               이유 : https://jeongmatt.blogspot.com/2020/10/append-appendto-script-javascriptjquery.html
            
             */
            $(document).on("click"".last-nav-td button"function() {
 
                $(this).remove();
 
                let ClickWord = $(this).html();
 
                $('.List-Button').filter(function() {
                    if ($(this).html() === ClickWord) {
                        $(this).toggleClass('yellowButton');
                    }
                });
                
                filter();
 
            });
 
 
            /*드롭다운시 텍스트 변경*/
            $('.dropdown-menu li a')
                    .click(
                            function() {
                                var selText = $(this).text();
                                $(this)
                                        .parents('.btn-group')
                                        .find('.dropdown-toggle')
                                        .html(
                                                selText
                                                        + ' <span class="caret"></span>');/*btn-group 에있는 dropdown-toggle찾아서 변경 */
                            });
 
            /*초기화 클릭시 버튼색 없어짐*/
            $('.List-NavButton').on('click'function() {
                $('.last-nav-td').children().remove(); /*td에있는 버튼들 제거*/
                $('.List-Button').attr('class''font_noto List-Button'); /*버튼 속성에 초기 버튼 css클래스 적용*/
                
                filter();
            })
        });
    </script>
    <script>
         // 컨트롤러에서 데이터 받기
         var jsonData = JSON.parse('${categoryList}');
         
         var cate1Arr = new Array();
         var cate1Obj = new Object();
         
         
         
         // 1차 분류 셀렉트 박스에 삽입할 데이터 준비
         for(var i = 0; i < jsonData.length; i++) {
             
             
          
          if(jsonData[i].level == "1") {
           cate1Obj = new Object();  //초기화
           cate1Obj.cateCode = jsonData[i].cateCode;
           cate1Obj.cateName = jsonData[i].cateName;
           cate1Arr.push(cate1Obj);
           
          }
         }
         
 
         // 1차 분류 셀렉트 박스에 데이터 삽입
         var cate1Select = $("#category1")
 
          for(var i = 0; i < cate1Arr.length; i++) {
              cate1Select.append("<span value='" + cate1Arr[i].cateCode + "'>"
               + cate1Arr[i].cateName + "</span>"); 
         } 
         
         
         //------------------------------------------------------------//
       
         $(document).on("change""category"function(){
             
             var jsonData2 = JSON.parse('${ categoryList2 }');
 
             var cate2Arr = new Array();
             var cate2Obj = new Object();
             
             // 2차 분류 셀렉트 박스에 삽입할 데이터 준비
             for(var i = 0; i < jsonData2.length; i++) {
              
              if(jsonData2[i].level == "2") {
               cate2Obj = new Object();  //초기화
               cate2Obj.cateCode = jsonData2[i].cateCode;
               cate2Obj.cateName = jsonData2[i].cateName;
               cate2Obj.cateCodeRef = jsonData2[i].cateCodeRef;
               
               cate2Arr.push(cate2Obj);
              }
             }
             
             var cate2Select = JSON.parse('${ categoryList }');
             
             /*
             for(var i = 0; i < cate2Arr.length; i++) {
               cate2Select.append("<option value='" + cate2Arr[i].cateCode + "'>"
                    + cate2Arr[i].cateName + "</option>");
             }
             */
             
             cate2Select.children().remove();
            
             $("option:selected"this).each(function(){
              
              var selectVal = $(this).val();  
              cate2Select.append("<option value=''>전체</option>");
              
              for(var i = 0; i < cate2Arr.length; i++) {
               if(selectVal == cate2Arr[i].cateCodeRef) {
                cate2Select.append("<option value='" + cate2Arr[i].cateCode + "'>"
                     + cate2Arr[i].cateName + "</option>");
               }
              }
              
             });
             
            });
         
         
             $('form').css('display','inline-block');
             
             
             // 페이지 클릭시 active 클래스 추가 및 제거 되는 스크립트
           $('.pagination a').click(function(){
               $('.pagination a').removeClass('active');
               $(this).addClass('active');
           });
           
      </script>
</body>
</html>
cs

=======

 

Color Script 내부에 주석을 달아놓았기 때문에 별다른 설명은 하지 않았습니다.

728x90