Coding Planet

[JSP, JSTL] forEach๋ฌธ์œผ๋กœ ๋งŒ๋“  ํ…Œ์ด๋ธ” ๋ณ‘ํ•ฉํ•˜๊ธฐ (varStatus="status"์†์„ฑ) ๋ณธ๋ฌธ

๐Ÿ“šํ”„๋กœ์ ํŠธ/๐Ÿ’ณ์‡ผํ•‘๋ชฐ ํ”„๋กœ์ ํŠธ

[JSP, JSTL] forEach๋ฌธ์œผ๋กœ ๋งŒ๋“  ํ…Œ์ด๋ธ” ๋ณ‘ํ•ฉํ•˜๊ธฐ (varStatus="status"์†์„ฑ)

jhj.sharon 2023. 6. 20. 12:16
๋ฐ˜์‘ํ˜•
์‡ผํ•‘๋ชฐ ํ”„๋กœ์ ํŠธ์—์„œ ์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ๋งŒ๋“ค ๋•Œ JSP๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค. ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ cartList๋ฅผ๊ฐ€์ ธ์™€์„œ ํ…Œ์ด๋ธ”๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค. ์ด ๋•Œ, ๋ฐฐ์†ก๋น„๋Š” ์ฃผ๋ฌธ 1๋ฒˆ๋‹น ๋ถ€๊ณผ๋˜๋ฏ€๋กœ ๋ฐฐ์†ก๋น„ ์˜์—ญ์ด ๋ณ‘ํ•ฉ(merge)๋˜๋„๋ก ํ•˜๋ คํ•œ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด ๋ฐฐ์†ก๋น„ ์˜์—ญ์˜ td๋ฅผ ํ†ตํ•ฉํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

 

1. ํ…Œ์ด๋ธ” ์ฝ”๋“œ

<!-- ์ฃผ๋ฌธ ๋ชฉ๋ก -->
            <div class="cart-item-wrapper" >
                <div class="cart-table-container" >
                    <table id="cart-item-table">
                        <thead>
                            <th> <input type="checkbox"></th>
                            <th>์ƒํ’ˆ์ •๋ณด</th>
                            <th>์ˆ˜๋Ÿ‰</th>
                            <th>์ฃผ๋ฌธ๊ธˆ์•ก</th>
                            <th>๋ฐฐ์†ก๋น„</th>
                        </thead>
                        <tbody>
                            <c:choose>
                                <c:when test="${not empty cartList}">
                                <!-- ์ƒํ’ˆ์ด ์žˆ๋‹ค๋ฉด -->
                                <!-- ํ–ฅ์ƒ๋œ for๋ฌธ์ฒ˜๋Ÿผ ์‚ฌ์šฉ -->
                                <c:forEach var="cart" items="${cartList}" varStatus="status">
                                    <tr style="justify-content: center;" id="${cart.cartId}">
                                        <td> <input type="checkbox"></td>
                                        <td>
                                            <div class="cart-item-detail">
                                                <div class="cart-item-img">
                                                    <img src="${contextPath}/${cart.productImage}" style="width: 80px;" alt="">
                                                </div>
                                                <div class="cart-item-info">
                                                    <div class="cart-item-name">${cart.productName}</div>
                                                    <div class="cart-item-option">Option : ${cart.cartOption}</div>
                                                    <div class="cart-item-unit-price">${cart.productPrice}์›</div>  
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <div class="cart-item-qty">
                                                <div class="qty-container">
                                                    <button class="qtybtn minus-btn" type="button">-</button>
                                                    <input class="qty" autocapitalize="none" type="text" inputmode="numeric" value="${cart.quantity}">
                                                    <button class="qtybtn plus-btn" type="button">+</button>
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <div><span class="cart-item-price">${cart.productPrice}</span>์›</div>
                                        </td>
                                        <c:if test="${status.first}">
                                            <td rowspan="${cartList.size()}">3,000์›</td>
                                        </c:if>
                                    </tr>
                                </c:forEach>
                                </c:when>
                                <c:otherwise>
                                    <tr>
                                    <!-- ์ƒํ’ˆ์ด ์—†๋‹ค๋ฉด -->
                                    <td colspan="5">์žฅ๋ฐ”๊ตฌ๋‹ˆ์— ์ƒํ’ˆ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.</td>
                                    </tr>
                                </c:otherwise>
                            </c:choose>

                        </tbody>
                           
                    </table>
                </div>
               
            </div>

 

  • JSTL <c:forEach>๋ฌธ์„ ํ†ตํ•ด <tr>ํƒœ๊ทธ๊ฐ€ ๋ฐ˜๋ณต๋˜๋ฉฐ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜จ ์ •๋ณด๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.
  • ์ด ๋•Œ <tr>์€ ์„œ๋ฒ„์— ์ €์žฅ๋œ ์ •๋ณด๋กœ ๊ทธ ์ˆ˜๊ฐ€ ๋™์ ์œผ๋กœ ์ •ํ•ด์ง€๋ฏ€๋กœ rowspan์„ ์‚ฌ์šฉํ•ด์„œ ์ปฌ๋Ÿผ์„ ๋ณ‘ํ•ฉํ•  ๋•Œ varStatus="status"๊ฐ€ ํ•„์š”ํ•˜๋‹ค. rowspan์„ ์ด์šฉํ•˜์—ฌ ๋ณ‘ํ•ฉํ•  ๋•Œ๋Š” ๋ณ‘ํ•ฉํ•˜๋Š” ์ฒซ๋ฒˆ์งธ <td>์—์„œ ์†์„ฑ๊ฐ’์„ ์ค˜์•ผํ•˜๋Š”๋ฐ ํ•ด๋‹น td๊ฐ€ ์ฒซ๋ฒˆ์งธ์ธ์ง€๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” status.first๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
  •  <c:if test="${status.first}"> ์กฐ๊ฑด์„ ํ†ตํ•ด ์ฒซ๋ฒˆ์งธ <tr>์—์„œ ๋ณ‘ํ•ฉ์ด ์ผ์–ด๋‚˜๋„๋ก ํ•œ๋‹ค.
  • ๋ณ‘ํ•ฉ๋  rowspan์€  <td rowspan="${cartList.size()}">์„ ํ†ตํ•ด ๋™์ ์œผ๋กœ ์ •ํ•˜๋„๋ก ํ•œ๋‹ค.

 

 

2. varStatus="status"์†์„ฑ

  • `varStatus="status"`๋Š” JSTL `<c:forEach>` ํƒœ๊ทธ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ณ€์ˆ˜ ์ƒํƒœ ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.
  • `varStatus` ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ฐ˜๋ณต๋ฌธ์˜ ์ƒํƒœ ์ •๋ณด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • `varStatus` ์†์„ฑ์„ ์„ค์ •ํ•˜๋ฉด JSTL์€ ๋ฐ˜๋ณต๋ฌธ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ด๋ฅผ `varStatus`์— ์ง€์ •ํ•œ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ `varStatus` ๊ฐ’์„ "status"๋กœ ์„ค์ •ํ•˜์—ฌ ์ƒํƒœ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.
  • `status` ๊ฐ์ฒด์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์œ ์šฉํ•œ ์†์„ฑ์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”๋ฐ ๋ช‡ ๊ฐ€์ง€ ์ฃผ์š” ์†์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

    - `index`: ํ˜„์žฌ ๋ฐ˜๋ณต์˜ ์ธ๋ฑ์Šค(0๋ถ€ํ„ฐ ์‹œ์ž‘)
    - `count`: ํ˜„์žฌ๊นŒ์ง€์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜. 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ์ฆ.
    - `first`: ํ˜„์žฌ ๋ฐ˜๋ณต์ด ์ฒซ ๋ฒˆ์งธ ๋ฐ˜๋ณต์ธ์ง€ ์—ฌ๋ถ€. `true` ๋˜๋Š” `false` 
    - `last`: ํ˜„์žฌ ๋ฐ˜๋ณต์ด ๋งˆ์ง€๋ง‰ ๋ฐ˜๋ณต์ธ์ง€ ์—ฌ๋ถ€, `true` ๋˜๋Š” `false` 

    ๋”ฐ๋ผ์„œ `varStatus="status"`๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด `status` ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๋ฐ˜๋ณต๋ฌธ์˜ ํ˜„์žฌ ์ƒํƒœ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ๋กœ์ง์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ„์ฒ˜๋Ÿผ `status.first` ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ๋ฐ˜๋ณต์ธ ๊ฒฝ์šฐ์—๋งŒ ํŠน์ • ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ์กฐ๊ฑด์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ฐ˜์‘ํ˜•
Comments