-
식단 관리 - 다이어트 일기장크루메이트 코드 리뷰!! 2020. 9. 18. 04:40
다이어트 일기장 조회
//dietBoard function dietBoardList(){ $.ajax({ url : '${pageContext.request.contextPath}/user/diet/dietBoardList', success : function(result){ $('#dietBoard_div').empty(); var dietBoardList=""; if(result.dietBoardList.length == 0){ alert('등록된게 없습니다.'); dietBoardList += '<div>'; dietBoardList += '<button type="button" id="dietBoardStartBtn" onclick="insertDietBoardFirst();" class="btn btn-grad">시작하기</button>'; dietBoardList += '</div>'; $('#dietBoard_div').append(dietBoardList); }else{ $.each(result.dietBoardList, function(index, item){ if(Number(item.diet_board_seq)%2 ==0){ // 0이면 왼쪽 dietBoardList += '<div class="row no-gutters">'; dietBoardList += '<div class="col-md py-2">'; dietBoardList += '<div class="card">'; dietBoardList += '<div class="card-body">'; dietBoardList += '<div class="float-right small">'+item.diet_board_date.split(' ')[0].split('-')[1]+' / '+item.diet_board_date.split(' ')[0].split('-')[2]+'</div>'; dietBoardList += '<h4 class="mb-2">'+item.diet_board_title+'</h4>'; dietBoardList += '<p class="mb-0">'+item.diet_board_content+'</p>'; dietBoardList += '</div>'; dietBoardList += '</div>'; dietBoardList += '</div>'; dietBoardList += '<div class="col-md-1 text-center timeline-line flex-column d-none d-md-flex">'; dietBoardList += '<div class="timeline-dot"></div>'; dietBoardList += '</div>'; dietBoardList += '<div class="col-md"></div>'; dietBoardList += '</div>'; }else{ // 0이 아니면 오른쪽 dietBoardList += '<div class="row no-gutters">'; dietBoardList += '<div class="timeline-top"></div>'; dietBoardList += '<div class="col-md"> </div>'; dietBoardList += '<div class="col-md-1 text-center timeline-line flex-column d-none d-md-flex">'; dietBoardList += '<div class="timeline-dot"></div>'; dietBoardList += '</div>'; dietBoardList += '<div class="col-md py-2">'; dietBoardList += '<div class="card">'; dietBoardList += '<div class="card-body">'; dietBoardList += '<div class="float-right small">'+item.diet_board_date.split(' ')[0].split('-')[1]+' / '+item.diet_board_date.split(' ')[0].split('-')[2]+'</div>'; dietBoardList += '<h4 class="mb-2">'+item.diet_board_title+'</h4>'; dietBoardList += '<p>'+item.diet_board_content+'</p>'; dietBoardList += '</div>'; dietBoardList += '</div>'; dietBoardList += '</div>'; dietBoardList += '</div>'; } }) $('#dietBoard_div').append(dietBoardList); } }, error : function(){ } }) }
관리 버튼 클릭시
// updateDietBoard function updateDietBoardForm(){ $.ajax({ url : '${pageContext.request.contextPath}/user/diet/dietBoardList', success : function(result){ if(result.dietBoardList == null){ alert('등록된게 없습니다.'); } $('#dietBoard_div').empty(); var dietBoardList=""; var count = 0; $.each(result.dietBoardList, function(index, item){ if(Number(item.diet_board_seq)%2 ==0){ // 0이면 왼쪽 dietBoardList += '<div class="row no-gutters" ><input type="hidden" class="diet_board_no" value="'+item.diet_board_no+'">'; dietBoardList += '<div class="col-md py-2">'; dietBoardList += '<div class="card">'; dietBoardList += '<div class="card-body">'; dietBoardList += '<div class="float-right small">'+item.diet_board_date.split(' ')[0].split('-')[1]+' / '+item.diet_board_date.split(' ')[0].split('-')[2]+'</div>'; dietBoardList += '<h4 class="mb-2"><textarea class="diet_board_title" style="background-color:transparent">'+item.diet_board_title+'</textarea></h4>'; dietBoardList += '<p class="mb-0"><textarea class="diet_board_content" style="background-color:transparent">'+item.diet_board_content+'</textarea></p>'; dietBoardList += '</div>'; dietBoardList += '</div>'; dietBoardList += '</div>'; dietBoardList += '<div class="col-md-1 text-center timeline-line flex-column d-none d-md-flex">'; dietBoardList += '<div class="timeline-dot"></div>'; dietBoardList += '</div>'; dietBoardList += '<div class="col-md"></div>'; dietBoardList += '</div>'; }else{ // 0이 아니면 오른쪽 dietBoardList += '<div class="row no-gutters"><input type="hidden" class="diet_board_no" value="'+item.diet_board_no+'">'; dietBoardList += '<div class="timeline-top"></div>'; dietBoardList += '<div class="col-md"> </div>'; dietBoardList += '<div class="col-md-1 text-center timeline-line flex-column d-none d-md-flex">'; dietBoardList += '<div class="timeline-dot"></div>'; dietBoardList += '</div>'; dietBoardList += '<div class="col-md py-2">'; dietBoardList += '<div class="card">'; dietBoardList += '<div class="card-body">'; dietBoardList += '<div class="float-right small">'+item.diet_board_date.split(' ')[0].split('-')[1]+' / '+item.diet_board_date.split(' ')[0].split('-')[2]+'</div>'; dietBoardList += '<h4 class="mb-2"><textarea class="diet_board_title" style="background-color:transparent">'+item.diet_board_title+'</textarea></h4>'; dietBoardList += '<p><textarea class="diet_board_content" style="background-color:transparent">'+item.diet_board_content+'</textarea></p>'; dietBoardList += '</div>'; dietBoardList += '</div>'; dietBoardList += '</div>'; dietBoardList += '</div>'; } }) $('#dietBoard_div').append(dietBoardList); $('#dietBoardControl').text('완료'); $('#dietBoardControl').attr('onclick', 'updateDietBoardComplete();'); $('#dietBoardAddBtn').attr('disabled', false); $('#dietBoardAddBtn').css('opacity', '100'); }, error : function(result){ alert('updateDietBoardForm 실패'); } }) }
완료버튼 클릭시
function updateDietBoardComplete(){ var diet_board_title_class = $('.diet_board_title'); var diet_board_content_class = $('.diet_board_content'); var diet_board_no_class = $('.diet_board_no'); var diet_board_list = new Array(); // list<Map<String, String>> for(var i = 0; i < diet_board_title_class.length; i++){ var data = new Object(); data.diet_board_title = diet_board_title_class.eq(i).val(); data.diet_board_content = diet_board_content_class.eq(i).val(); data.diet_board_no = diet_board_no_class.eq(i).val(); diet_board_list.push(data); } var jsonData = JSON.stringify(diet_board_list) ; // alert(jsonData); $.ajax({ type : 'POST', url : '${pageContext.request.contextPath}/user/diet/updateDietBoard', contentType : 'application/json; charset=UTF-8', async: false, data : jsonData, success : function(){ alert('성공'); dietBoardList(); $('#dietBoardControl').text('관리'); $('#dietBoardControl').attr('onclick', 'updateDietBoardForm();'); $('#dietBoardAddBtn').attr('disalbed', ''); $('#dietBoardAddBtn').css('opacity', '0'); }, error : function(){ alert('실패'); } }) }
처음 일기장을 추가할 경우 / 기존에 추가했던 내역이 있고 추가로 일기장을 쓸 경우
function insertDietBoardFirst(){ $.ajax({ url : '${pageContext.request.contextPath}/user/diet/insertDietBoardFirst', success : function(result){ updateDietBoardForm(); }, erorr : function(result){ alert('dietBoardInsert 실패'); } }) } function insertDietBoard(){ $.ajax({ url : '${pageContext.request.contextPath}/user/diet/insertDietBoard', success : function(result){ updateDietBoardForm(); }, erorr : function(result){ alert('dietBoardInsert 실패'); } }) }
<insert id="insertDietBoard" parameterType="Map"> <selectKey keyProperty="diet_board_seq" resultType="java.lang.String" order="BEFORE"> SELECT MAX(DIET_BOARD_SEQ)+1 DIET_BOARD_SEQ FROM DIET_BOARD WHERE mem_no = #{mem_no} </selectKey> INSERT INTO DIET_BOARD(DIET_BOARD_NO, DIET_BOARD_DATE, DIET_BOARD_TITLE, DIET_BOARD_CONTENT, MEM_NO, DIET_BOARD_SEQ)VALUES( diet_board_seq.nextval, sysdate, '제목을 입력해주세요', '내용을 입력해주세요', #{mem_no}, #{diet_board_seq} ) </insert> <insert id="insertDietBoardFirst" parameterType="Map"> INSERT INTO DIET_BOARD(DIET_BOARD_NO, DIET_BOARD_DATE, DIET_BOARD_TITLE, DIET_BOARD_CONTENT, MEM_NO, DIET_BOARD_SEQ)VALUES( diet_board_seq.nextval, sysdate, '제목을 입력해주세요', '내용을 입력해주세요', #{mem_no}, 1) </insert>
'크루메이트 코드 리뷰!!' 카테고리의 다른 글
챗봇 (0) 2020.09.18 채팅 (0) 2020.09.18 식단 관리 - 열량 차트 (0) 2020.09.18 식단 관리 - 나의 식단표 (0) 2020.09.18 식단 관리 - 회원 정보 조회, 등록, 수정, 삭제 (0) 2020.09.18