ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 식단 관리 - 나의 식단표
    크루메이트 코드 리뷰!! 2020. 9. 18. 04:09

    달력 클릭 시

    
    function dietDayInfoList(){
    	$.ajax({
    		type : 'POST',
    		url : '${pageContext.request.contextPath}/user/diet/calendarModal',
    		dataType : 'JSON',
    		data : {
    			dd_date : dd_date
    		},
    		error : function(result) {
    			alert("실패");
    		},
    		success : function(result) {
    
    			$('#dietDayInfo_thead1').empty();
    			$('#dietDayInfo_tbody1').empty();
    			$('#dietDayInfo_thead2').empty();
    			$('#dietDayInfo_tbody2').empty();
    			$('#dietDayInfo_thead3').empty();
    			$('#dietDayInfo_tbody3').empty();
    			
    			var itemIndex = 1;
    			
     			dd_no = result.dietDay.dd_no;
    
    			$('#dietDayInfo_thead1').append('<tr>');
    			$('#dietDayInfo_thead1').append('<th scope="col">#</th>');
    			$('#dietDayInfo_thead1').append('<th scope="col">메뉴</th>');
    			$('#dietDayInfo_thead1').append('<th scope="col">칼로리</th>');
    			$('#dietDayInfo_thead1').append('<th scope="col"></th>');
    
    			$('#dietDayInfo_thead2').append('<tr>');
    			$('#dietDayInfo_thead2').append('<th scope="col">#</th>');
    			$('#dietDayInfo_thead2').append('<th scope="col">메뉴</th>');
    			$('#dietDayInfo_thead2').append('<th scope="col">칼로리</th>');
    			$('#dietDayInfo_thead2').append('<th scope="col"></th>');
    
    			$('#dietDayInfo_thead3').append('<tr>');
    			$('#dietDayInfo_thead3').append('<th scope="col">#</th>');
    			$('#dietDayInfo_thead3').append('<th scope="col">메뉴</th>');
    			$('#dietDayInfo_thead3').append('<th scope="col">칼로리</th>');
    			$('#dietDayInfo_thead3').append('<th scope="col"></th>');
    
    			if(result.dietDayInfoList1 != null){
    				$.each(result.dietDayInfoList1, function(index, item){
    	 				$('#dietDayInfo_tbody1').append('<tr>')
    	 				$('#dietDayInfo_tbody1').append('<td><input type="hidden" value="'+item.dd_no +'">'+itemIndex++ +'</td>'); 				
    	 				$('#dietDayInfo_tbody1').append('<td>'+item.menu_name+'</td>');
    	 				$('#dietDayInfo_tbody1').append('<td>'+item.menu_kcal+'</td>');
    	 				$('#dietDayInfo_tbody1').append('<td><button class="btn btn-success" onclick="menu_delete1(this)"><input type="hidden" value="'+item.ddi_no +'">삭제</button></td>');
    	 				$('#dietDayInfo_tbody1').append('</tr>');
    	 			 });
    			}
    
    			if(result.dietDayInfoList2 != null){
    				$.each(result.dietDayInfoList2, function(index, item){
    	 				$('#dietDayInfo_tbody2').append('<tr>')
    	 				$('#dietDayInfo_tbody2').append('<td><input type="hidden" value="'+item.dd_no +'">'+itemIndex++ +'</td>'); 				
    	 				$('#dietDayInfo_tbody2').append('<td>'+item.menu_name+'</td>');
    	 				$('#dietDayInfo_tbody2').append('<td>'+item.menu_kcal+'</td>');
    	 				$('#dietDayInfo_tbody2').append('<td><button class="btn btn-success" onclick="menu_delete2(this)"><input type="hidden" value="'+item.ddi_no +'">삭제</button></td>');
    	 				$('#dietDayInfo_tbody2').append('</tr>')	 
    	 			 });	
    			}
    
    			if(result.dietDayInfoList3 != null){
    				$.each(result.dietDayInfoList3, function(index, item){
    	 				$('#dietDayInfo_tbody3').append('<tr>')
    	 				$('#dietDayInfo_tbody3').append('<td><input type="hidden" value="'+item.dd_no +'">'+itemIndex++ +'</td>'); 				
    	 				$('#dietDayInfo_tbody3').append('<td>'+item.menu_name+'</td>');
    	 				$('#dietDayInfo_tbody3').append('<td>'+item.menu_kcal+'</td>');
    	 				$('#dietDayInfo_tbody3').append('<td><button class="btn btn-success" onclick="menu_delete3(this)"><input type="hidden" value="'+item.ddi_no +'">삭제</button></td>');
    	 				$('#dietDayInfo_tbody3').append('</tr>');	 
    	 			 });
    			}
    			
     			$('#dd_kcal').empty();
     			
    			$('#dd_kcal').append('<div>'+result.dietDay.dd_kcal+'</div>');
    		}
    	});
    }

     

    달력 클릭시 ajax로 요청하는 컨트롤러

    @RequestMapping("calendarModal")
    	public ModelAndView calendarModal(ModelAndView andView, String dd_date, HttpServletRequest request,
    			HttpSession session, Map<String, String> params, Diet_dayVO dietDay, String dd_no) throws Exception {
    
    		// 일별 식단 등록
    
    		session = request.getSession();
    
    		MemberVO memberInfo = (MemberVO) session.getAttribute("LOGIN_MEMBERINFO");
    
    		dietDay.setMem_no(memberInfo.getMem_no());
    
    		params.put("dd_date", dd_date);
    		params.put("mem_no", memberInfo.getMem_no());
    
    		// 처음 선택시에는 잘 들어가는데 그다음 다른창을 선택하면 insert가 3번된다?
    		if (dietService.selectDietDay(params) == null) {
    			dd_no = dietService.insertDietDay(dietDay) + "";
    			params.put("dd_no", dd_no);
    			
    		} else {
    			dietDay = dietService.selectDietDay(params);
    			params.put("dd_no", dietDay.getDd_no());
    		}
    
    		// 일별 식단 내용 가져오기
    
    		// 일별 식단의 식단 번호 가져오기
    		
    
    		// 일별 식단 상세 가져오기
    		// 1 -> 아침꺼
    		params.put("dd_info_division", "1");
    		List<Diet_day_infoVO> dietDayInfoList1 = dietService.dietDayInfoList(params);
    
    		// 2 -> 점심꺼
    		params.put("dd_info_division", "2");
    		List<Diet_day_infoVO> dietDayInfoList2 = dietService.dietDayInfoList(params);
    
    		// 3 -> 저녁
    		params.put("dd_info_division", "3");
    		List<Diet_day_infoVO> dietDayInfoList3 = dietService.dietDayInfoList(params);
    
    		andView.addObject("dietDay", dietDay);
    		andView.addObject("dietDayInfoList1", dietDayInfoList1);
    		andView.addObject("dietDayInfoList2", dietDayInfoList2);
    		andView.addObject("dietDayInfoList3", dietDayInfoList3);
    
    		andView.setViewName("jsonConvertView");
    		return andView;
    	}

     

     

    등록 버튼 클릭시

    $('#insert_diet_day1').on('click', function(){
    		if(month < 10){
    			month = '0'+month;
    		}
    		if(day < 10){
    			day = '0'+day;
    		}
    		$.ajax({
    	 		url : '${pageContext.request.contextPath}/user/diet/insertAllDietDay',
    	 		dataType : 'JSON',
    	 		data : {
    	 			dd_date : year + '-' + month + '-' + day,
    	 			dd_info_division : "1",
    		 		menu_no : selectMenuNo,
    		 		dd_no : dd_no
    	 		},
    	 		error : function(result) {
    	 			alert(result.status);
    	 		},
    	 		success : function(result) {
     	 			$('#menu_search1').val("");
     	 			$('#menu_search_result1').empty();
     	 			dietDayInfoList();
    	 		}
    	 	});
    		
    	})

    댓글

Designed by Tistory.