/board/list.jsp
<div class="table">
<div class="divTableBody">
<div class="row">
<div class="cell" style="width:5%"><input type="checkbox" name="allChk" id="allChk"></div> <!-- 전체선택 -->
<div class="cell" style="width:5%">No</div>
<div class="cell" style="width:50%">제목</div>
<div class="cell" style="width:15%">작성자</div>
<div class="cell" style="width:15%">작성일</div>
<div class="cell" style="width:10%">조회</div>
</div>
<!-- 반복 -->
<c:set var="numbering" value="${pageMaker.totalCount - (pageMaker.cri.page -1)*pageMaker.cri.perPageNum }" />
<c:forEach var="boardVO" items="${list}">
<div class="row">
<div class="cell2 align"><input type="checkbox" name="rowChk" id="rowChk" value="${boardVO.bno}"></div> <!-- 개별선택 value="pk키" 필수 -->
<div class="cell2 align">${numbering}</div>
<div class="cell2">
<a href="view${pageMaker.makeQuery(pageMaker.cri.page)}&id=${id}&tab=${tab}&fno=${boardVO.fno}&bno=${boardVO.bno}">${boardVO.title}</a>
</div>
<div class="cell2 align">${boardVO.name}</div>
<div class="cell2 align"><fmt:formatDate value="${boardVO.regdate}" pattern="yyyy.MM.dd" /></div>
<div class="cell2 align">${boardVO.viewcnt}</div>
</div>
<c:set var="numbering" value="${numbering-1}" />
</c:forEach>
</div>
</div>
BoardDAO
public void chkdelete(int i) throws Exception; // 선택삭제
BoardDAOImpl
@Inject
private SqlSession sqlSession;
private static final String namespace = "org.zerock.mapper.BoardMapper";
@Override
public void chkdelete(int i) throws Exception {
sqlSession.delete(namespace+".chkdelete", i);
}
BoardService
public void chkdelete(int i) throws Exception; // 선택삭제
BoardServiceImpl
@Inject
private BoardDAO dao;
@Override
public void chkdelete(int i) throws Exception {
dao.chkdelete(i);
}
BoardMapper
<delete id="chkdelete">
delete from board where bno=#{i} and tab='b'
</delete>
/board/list.jsp - script
<script>
// 체크박스 여부(all, row 연동시키기)
$(function(){
var chkOne = document.getElementById("rowChk");
var rowCnt = chkOne.length;
// allChk를 선택하면 rowChk 다 선택되게
$("input[name=allChk]").click(function(){
var chkArr = $("input[name=rowChk]");
for(var i=0; i<chkArr.length; i++){
chkArr[i].checked = this.checked;
}
});
$("input[name=rowChk]").click(function(){
// 한페이지에 rowChk는 10개임, 10개가 다 체크되었다면 allChk도 체크되게
if($("input[name=rowChk]:checked").length == 10) {
$("input[name=allChk]")[0].checked = true; // 체크된 개수가 10개라면 allChk도 체크되게
} else {
$("input[name=allChk]")[0].checked = false; // 10개가 아니라면 allChk는 체크안되게
}
});
});
// 선택 삭제 구현
function ckbox_del() {
//alert("선택삭제 클릭");
var chk_list = $("input[name=rowChk]");
var chk_result = $("input[name=rowChk]:checked").length;
//alert("선택된 개수="+chk_result);
var conf = confirm("해당 게시글을 삭제하시겠습니까?");
if(conf) { // confirm 확인 눌렀다면
var arr = new Array(); // 배열생성
for(var i=0; i<chk_list.length; i++){
if(chk_list[i].checked){
arr.push(chk_list[i].value); // 선택되어있는 값 배열에 저장
}
}
//alert(arr);
$.ajax({
url : "/board/chkdelete",
type : "get",
data : "bno="+arr , // "변수명="+값 //cotroller에서 requestparam(value="bno")로 사용됨
error: function(){
alert("실패");
},
success : function(data){ // result 0으로 반환
alert("삭제처리 되었습니다.");
location.reload(); // 현재페이지 새로고침(list)
}
});
}
}
</script>
BoardController
// 선택삭제
@ResponseBody
@RequestMapping(value="/chkdelete", method = RequestMethod.GET)
public int chkdelete2(@RequestParam(value="bno") int[] arr) throws Exception {
log.info("선택삭제===");
log.info("arr:"+arr);
int result = 0;
for(int num : arr) { // arr을 더 작은 num으로 쪼개서 담기
service.chkdelete(num);
}
return result; // 0 반환
}
'프로젝트 로직 모음집 > spring' 카테고리의 다른 글
Today is 기분선택 (0) | 2023.06.03 |
---|---|
게시판별 new수/총수 (0) | 2023.06.03 |
최신글4개list (0) | 2023.06.03 |