Hello everyone, I am the pig of cabbage arch.
Demand: The
seat is divided into three states, 1. There is a seat. 2. No seat, 3, damaged.
First store all seats in the list according to the self-study room id in the seat list, that is, List <Seat>. Then according to the three states of the seat, it is displayed in red, white, and not displayed.
Seat table attributes seatId, row (row), col (column), status (status)
controller:
@RequestMapping("query")
public ModelAndView querySeat(int roomId){
ModelAndView mv=new ModelAndView();
mv.setViewName("/manager/seat_update.jsp");
List<Seat> seatList=seatService.querySeat(roomId);
//获得房间的行数以及列数
Room room=roomService.queryRoomById(roomId);
mv.addObject("room", room);
mv.addObject("seatList",seatList);
return mv;
}
Front desk solution
<!-- 控制行 -->
<c:forEach var="i" begin="1" end="${requestScope.room.rows}"
step="1">
<div class="row seat-row" data-row="${i}">
<div class="col-xs-1 text-center">
<span class="row-num">${i}</span>
</div>
<div class="col-xs-11">
<!-- 控制列 -->
<c:forEach var="j" begin="1" end="${requestScope.room.cols}"
step="1">
<c:forEach items="${requestScope.seatList}" var="seat">
<c:if test="${(seat.row==i) && (seat.col==j)&& (seat.status=='有座')}">
<span class="seat-empty" data-col="${j}"></span>
</c:if>
<c:if test="${(seat.row==i) && (seat.col==j)&& (seat.status=='无座')}">
<span class="seat-sold" data-col="${j}"></span>
</c:if>
<c:if test="${(seat.row==i) && (seat.col==j)&& (seat.status=='损坏')}">
<span class="none" data-col="${j}"></span>
</c:if>
</c:forEach>
</c:forEach>
</div>
</div>
</c:forEach>
effect:
This was the original idea, but in the end it was found that the efficiency is too poor, because every cycle has to traverse the list, the efficiency is really poor.
All have another idea, is to store the seats in the List into a two-dimensional array, the subscript of the two-dimensional array is the row and column of the seat, and then obtain the seat according to the subscript, and then determine the seat by the state of the seat Color, so as to avoid traversing the list every time. Increased efficiency. Without further ado, write a util first.
After writing, woc found that it was too good. One line of code solved the problem
public class SeatUtil {
public Seat[][] listToArray(List<Seat> list,int rows,int cols){
Seat[][] seatArray=new Seat[rows][cols];
for(Seat seat:list){
//这行代码妙啊
seatArray[seat.getRow()-1][seat.getCol()-1]=seat;
}
return seatArray;
}
}
Then the controller layer passes this two-dimensional array over
@RequestMapping("query")
public ModelAndView querySeat(int roomId){
ModelAndView mv=new ModelAndView();
mv.setViewName("/manager/seat_update.jsp");
List<Seat> seatList=seatService.querySeat(roomId);
//获得房间的行数以及列数
Room room=roomService.queryRoomById(roomId);
//这行代码仔细品
Seat[][] seatArray=new SeatUtil().listToArray(seatList, room.getRows(), room.getCols());
mv.addObject("room", room);
mv.addObject("seatArray",seatArray);
return mv;
}
Front desk: The main difference is the front desk
<!-- 控制列 -->
<c:forEach var="j" begin="1" end="${requestScope.room.cols}"
step="1">
<c:if test="${requestScope.seatArray[i-1][j-1].status=='有座'}">
<span class="seat-empty" data-col="${j}"></span>
</c:if>
<c:if test="${requestScope.seatArray[i-1][j-1].status=='无座'}">
<span class="seat-sold" data-col="${j}"></span>
</c:if>
<c:if test="${requestScope.seatArray[i-1][j-1].status=='损坏'}">
<span class="none" data-col="${j}"></span>
</c:if>
</c:forEach>
successful! ! !
The difference between the two methods of Pin Yi Pin. The difference in efficiency is that the first kind of query traversal is too low.