Coding Planet
[JSP, JSTL] forEach๋ฌธ์ผ๋ก ๋ง๋ ํ ์ด๋ธ ๋ณํฉํ๊ธฐ (varStatus="status"์์ฑ) ๋ณธ๋ฌธ
๐ํ๋ก์ ํธ/๐ณ์ผํ๋ชฐ ํ๋ก์ ํธ
[JSP, JSTL] forEach๋ฌธ์ผ๋ก ๋ง๋ ํ ์ด๋ธ ๋ณํฉํ๊ธฐ (varStatus="status"์์ฑ)
jhj.sharon 2023. 6. 20. 12:16๋ฐ์ํ
์ผํ๋ชฐ ํ๋ก์ ํธ์์ ์ฅ๋ฐ๊ตฌ๋๋ฅผ ๋ง๋ค ๋ JSP๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค. ์๋ฒ๋ก๋ถํฐ cartList๋ฅผ๊ฐ์ ธ์์ ํ ์ด๋ธ๋ก ๊ตฌํํ๊ณ ์๋ค. ์ด ๋, ๋ฐฐ์ก๋น๋ ์ฃผ๋ฌธ 1๋ฒ๋น ๋ถ๊ณผ๋๋ฏ๋ก ๋ฐฐ์ก๋น ์์ญ์ด ๋ณํฉ(merge)๋๋๋ก ํ๋ คํ๋ค.
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` ์์ฑ์ ์ฌ์ฉํ์ฌ ์ฒซ ๋ฒ์งธ ๋ฐ๋ณต์ธ ๊ฒฝ์ฐ์๋ง ํน์ ๋์์ ์ํํ๋๋ก ์กฐ๊ฑด์ ์ง์ ํ ์ ์๋ค.
๋ฐ์ํ
'๐ํ๋ก์ ํธ > ๐ณ์ผํ๋ชฐ ํ๋ก์ ํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ต๊ทผ ๋ณธ ์ํ ๊ตฌํํ๊ธฐ : url์์ product_id๊ฐ์ ธ์์ ์ฟ ํค์ ์ ์ฅํ๊ธฐ (0) | 2023.06.15 |
---|---|
์ผํ๋ชฐ ์ํ ์นดํ ๊ณ ๋ฆฌ ๊ตฌํํ๊ธฐ - ์ฟผ๋ฆฌ ์คํธ๋ง (0) | 2023.06.13 |
Comments