簡體   English   中英

當復選框已在數據庫中使用時如何將其設置為非活動狀態 - thymeleaf spring 啟動

[英]how to set the checkbox to inactive when it is already used in the database - thymeleaf spring boot

我有一個預訂應用程序,它使用 jpa 將票存儲在 h2 數據庫中。當有人預訂座位時,復選框應該對下一個人無效/禁用,因為現在你可以一遍又一遍地預訂座位 - 用戶應該看到一些座位已經預定了。。怎么解決這個問題? 我必須在 reservation-seat.html 添加一些東西嗎?

預訂座位.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Movies</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
 
</head>
<body>
<div class="container my-2">
    <div class="card">
        <div class="card-body">
            <div class="container my-5">
                <h1 th:text="${movieName}"> Movie Name</h1>

                <form th:action="@{'/reservation/save/' + ${repertoireId}}" th:object="${seatInfo}" method="post">

                    <div class="seatStructure">
                        <center>

                            <table id="seatsBlock">
                                <p id="notification"></p>
                                <tr>
                                    <td colspan="14">
                                        <div class="screen">SCREEN</div>
                                    </td>

                                    <br/>
                                </tr>

                                <tr>
                                    <td></td>
                                    <td>1</td>
                                    <td>2</td>
                                    <td>3</td>
                                    <td>4</td>
                                    <td>5</td>
                                    <td></td>
                                    <td>6</td>
                                    <td>7</td>
                                    <td>8</td>
                                    <td>9</td>
                                    <td>10</td>
                                    <td>11</td>
                                    <td>12</td>
                                </tr>

                                <tr>
                                    <td>A</td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A1"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A2"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A3"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A4"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A5"></td>
                                    <td class="seatGap"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A6"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A7"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A8"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A9"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A10"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A11"></td>
                                    <td><input th:field="*{seatReservation.seat}" type="checkbox" class="seats" value="A12"></td>
                                </tr>

                            </table>

                            <input type = "hidden" th:value="${movieName}">
                            <input type = "hidden" th:value="${repertoireId}">

                            </br>
                            <button type="submit">Order.</button>
                        </center>
                    </div>
                </form>

                <br/><br/>
            </div>
        </div>
    </div>
</div>
</div>

</div>
</body>
</html>

ReservationController.java

@Controller
// @Transactional
public class ReservationController {

    TicketRepo ticketRepo;
    ReservationRepo reservationRepo;
    AppUserRepo appUserRepo;
    MovieRepo movieRepo;
    RepertoireRepo repertoireRepo;

    @Autowired
    public ReservationController(TicketRepo ticketRepo, ReservationRepo reservationRepo, AppUserRepo appUserRepo,
                                 MovieRepo movieRepo, RepertoireRepo repertoireRepo) {
        this.ticketRepo = ticketRepo;
        this.reservationRepo = reservationRepo;
        this.appUserRepo = appUserRepo;
        this.movieRepo = movieRepo;
        this.repertoireRepo = repertoireRepo;
    }


    @GetMapping("/movies/{movieName}/reservation")
    public String reservationPage(Model model, @PathVariable ("movieName") String movieName) {


        Movie movie = movieRepo.findByTitle(movieName);
        List<Repertoire> repertoires = repertoireRepo.findByMovieId(movie.getId());

//        model.addAttribute("seat", new SeatReservation());
//        model.addAttribute("movieName", movirepertoireseName);

        model.addAttribute("repertoires", repertoires);
        return "reservation";
    }

    @GetMapping("/movies/{movieName}/reservation/{repertoireId}")
    public String reservationSeatPage(Model model, @PathVariable("movieName") String movieName,
                                  @PathVariable("repertoireId") Long repertoireId) {

        Testing testing1 = new Testing();
        testing1.setSeatReservation(new SeatReservation());
        model.addAttribute("seatInfo", testing1);
        model.addAttribute("movieName", movieName);
        model.addAttribute("repertoireId", repertoireId);
        return "reservation-seat";
    }

    @PostMapping("/reservation/save/{repertoireId}")
    public String reserve(@ModelAttribute ("seatInfo") Testing testing, Principal principal,
                          @ModelAttribute("repertoireId") Long repertoireId) {

        UUID uuid = UUID.randomUUID();

        Ticket ticket = new Ticket();
        ticket.setSeat(testing.getSeatReservation().getSeat());
        ticket.setPrice(20);
        ticket.setUuid(uuid);
        ticketRepo.save(ticket);


        Reservation reservation = new Reservation();

        reservation.setTicket(ticketRepo.findByUuid(uuid).get());
        Repertoire repertoire = repertoireRepo.findById(repertoireId).get();
        reservation.setMovie(movieRepo.findByTitle(repertoire.getMovie().getTitle()));
        reservation.setRepertoire(repertoire);
        reservation.setAppUser(appUserRepo.findByUsername(principal.getName()));
        reservationRepo.save(reservation);
        return "redirect:/movies/list";
    }
}

SeatReservation.java

import lombok.Data;

@Data
public class SeatReservation {

    private String seat;
    private boolean active;

    public boolean isActive() {
        return active;
    }
}

測試.java

import lombok.Data;

@Data
public class Testing {

    private SeatReservation seatReservation;

    private Long id;
    private String string;
    private boolean active;

    public boolean isActive() {
        return active;
    }

}

工單.java


import lombok.Data;

import javax.persistence.*;
import java.util.UUID;


@Data
@Entity
public class Ticket {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id")
    private Long id;

    private UUID uuid;
    private String seat;
    private Integer price;

    public Ticket() {
    }
}

我從事過類似的工作,因此將發布我的方法。

我曾使用 ajax 提交以發布座位預訂信息,如果響應成功,那么我將禁用已單擊的復選框。

正如您在評論部分所說,您希望在沒有 javascript 的情況下實現此目的,然后 go 用於以下方法。

每次提交表單時,將占用的座位 map(座位 id 鍵,狀態值)保存在 model 屬性中,然后通過檢查座位 id 和狀態(活動/非活動)並使呈現的復選框禁用/未禁用來遍歷 map在國旗上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM