実現したいこと
tableでの予約カレンダーを実装
- 予約が入っている場合、その開始時間から終了時間までtdが結合するようにする
前提
初学者です。
jspファイルで施設の予約カレンダーを作成しようとしています。
tableで、部屋ごとに列を作成し、一番左のtdに時刻(15分刻み)を表示させています。
予約が入っている時間のtdを結合して、予約情報を表示させたいと考えています。
しかし、今私が作成したソースコードでは、<td>の中で予約があるかどうか調べているため、
rowspanを動的に変える方法が分かりません。
いたらない質問で恐縮ですが、
どうかご教授いただきたく存じます。
該当のソースコード
jsp
1<%@page import="entity.FacilityInfo"%> 2<%@page import="entity.ReservationInfo"%> 3<%@page import="java.util.ArrayList"%> 4<%@page import="java.time.LocalTime"%> 5<%@page import="java.time.temporal.ChronoUnit"%> 6<%@page import="java.util.List"%> 7<%@ page language="java" contentType="text/html; charset=UTF-8" 8 pageEncoding="UTF-8"%> 9<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 10<!DOCTYPE html> 11<html> 12<head> 13<meta charset="UTF-8"> 14<link rel="stylesheet" type="text/css" href="calendar.css" /> 15<title>Insert title here</title> 16</head> 17<body> 18 <% 19 //時間リスト 20 List<LocalTime> timeList = new ArrayList<>(); 21 LocalTime startTime = LocalTime.parse("09:00"); 22 LocalTime endTime = LocalTime.parse("18:00"); 23 while (startTime.isBefore(endTime) || startTime.equals(endTime)) { 24 timeList.add(startTime); 25 startTime = startTime.plusMinutes(15); 26 } 27 28 //施設リスト 29 List<FacilityInfo> facilityList = new ArrayList<>(); 30 //FacilityInfo(施設ID(facility_Id:int)と施設名(facility_Name:String)を持つ) 31 FacilityInfo facilityInfo = null; 32 for (int i = 1; i < 5; i++) { 33 facilityInfo = new FacilityInfo(i, "会議室" + i); 34 facilityList.add(facilityInfo); 35 } 36 37 //予約情報リスト 38 List<ReservationInfo> reservationList = new ArrayList<>(); 39 //ReservationInfo(ID、施設ID、予約開始時間(startTime: LocalTime)、予約終了時間(endTime: LocalTime)を持つ) 40 ReservationInfo reservationInfo = new ReservationInfo(1, 1, LocalTime.parse("09:00"), LocalTime.parse("12:00")); 41 reservationList.add(reservationInfo); 42 reservationInfo = new ReservationInfo(2, 1, LocalTime.parse("15:00"), LocalTime.parse("17:00")); 43 reservationList.add(reservationInfo); 44 reservationInfo = new ReservationInfo(3, 2, LocalTime.parse("10:00"), LocalTime.parse("12:00")); 45 reservationList.add(reservationInfo); 46 reservationInfo = new ReservationInfo(4, 4, LocalTime.parse("10:00"), LocalTime.parse("12:00")); 47 reservationList.add(reservationInfo); 48 %> 49 <%! 50 //tdを結合するときのrowspanを計算する 51 public static long getRowCount(ReservationInfo reservationInfo){ 52 long delta = reservationInfo.getStartTime().until(reservationInfo.getEndTime(), ChronoUnit.MINUTES); 53 long rowCount = delta/15; 54 return rowCount; 55 } 56 %> 57 58<table> 59 <tr> 60 <th></th> 61 <%for(FacilityInfo facility: facilityList){ %> 62 <th><%=facility.getFaciilityName() %></th> 63 <%} %> 64 </tr> 65 <%List<ReservationInfo> notDisplayedList = new ArrayList<>(reservationList);%> 66 <%ReservationInfo reservation; %> 67 <%for(LocalTime time: timeList){ %> 68 <tr> 69 <td><%=time %></td> 70 <%boolean reservationFlag = false;%> 71 <%for(FacilityInfo facility2: facilityList){ %> 72 <td> 73 <% 74 for(int i = 0; i < notDisplayedList.size(); i++){ 75 reservation = notDisplayedList.get(i); 76 LocalTime reservationStartTime = reservation.getStartTime(); 77 LocalTime reservationEndTime = reservation.getEndTime(); 78 //予約の施設IDと開始時間~終了時間がセルの施設列、時間行の値と一致したら予約情報を表示させる 79 if(reservation.getFacilityId() == facility2.getFacilityId() && reservationStartTime.equals(time)){%> 80 <%=reservation.getId()%> 81 <%reservationFlag = true%> 82 <%}%> 83 <%}%> 84 </td> 85 <%} %> 86 <%} %> 87 </tr> 88</table> 89</body> 90</html>
java
1package entity; 2 3import java.util.Objects; 4 5public class FacilityInfo { 6 private int facilityId; 7 private String faciilityName; 8 9 public FacilityInfo(int facilityId, String faciilityName) { 10 super(); 11 this.facilityId = facilityId; 12 this.faciilityName = faciilityName; 13 } 14 15 public int getFacilityId() { 16 return facilityId; 17 } 18 19 public void setFacilityId(int facilityId) { 20 this.facilityId = facilityId; 21 } 22 23 public String getFaciilityName() { 24 return faciilityName; 25 } 26 27 public void setFaciilityName(String faciilityName) { 28 this.faciilityName = faciilityName; 29 } 30 31 @Override 32 public int hashCode() { 33 return Objects.hash(faciilityName, facilityId); 34 } 35 36 @Override 37 public boolean equals(Object obj) { 38 if (this == obj) 39 return true; 40 if (obj == null) 41 return false; 42 if (getClass() != obj.getClass()) 43 return false; 44 FacilityInfo other = (FacilityInfo) obj; 45 return Objects.equals(faciilityName, other.faciilityName) && facilityId == other.facilityId; 46 } 47 48} 49
java
1package entity; 2 3import java.time.LocalTime; 4import java.util.Objects; 5 6public class ReservationInfo { 7 private int id; 8 private int facilityId; 9 private LocalTime startTime; 10 private LocalTime endTime; 11 12 public ReservationInfo(int id, int facilityId, LocalTime startTime, LocalTime endTime) { 13 super(); 14 this.id = id; 15 this.facilityId = facilityId; 16 this.startTime = startTime; 17 this.endTime = endTime; 18 } 19 20 public int getId() { 21 return id; 22 } 23 24 public void setId(int id) { 25 this.id = id; 26 } 27 28 public int getFacilityId() { 29 return facilityId; 30 } 31 32 public void setFacilityId(int facilityId) { 33 this.facilityId = facilityId; 34 } 35 36 public LocalTime getStartTime() { 37 return startTime; 38 } 39 40 public void setStartTime(LocalTime startTime) { 41 this.startTime = startTime; 42 } 43 44 public LocalTime getEndTime() { 45 return endTime; 46 } 47 48 public void setEndTime(LocalTime endTime) { 49 this.endTime = endTime; 50 } 51 52 @Override 53 public int hashCode() { 54 return Objects.hash(endTime, facilityId, id, startTime); 55 } 56 57 @Override 58 public boolean equals(Object obj) { 59 if (this == obj) 60 return true; 61 if (obj == null) 62 return false; 63 if (getClass() != obj.getClass()) 64 return false; 65 ReservationInfo other = (ReservationInfo) obj; 66 return Objects.equals(endTime, other.endTime) && facilityId == other.facilityId && id == other.id 67 && Objects.equals(startTime, other.startTime); 68 } 69 70}

回答1件
あなたの回答
tips
プレビュー