Home > database >  Thymeleaf database update
Thymeleaf database update

Time:12-08

Is there a way to update value of the String "status" to "1" on button onClick. If there is no way to change it button onClick is there another way to do it?

I want to find row by Id and update the value of the "status".

This is Html File with Thymeleaf

    <form action="#"  method="put" th:action="@{/order}">
    <table>
        <tr th:each="p : ${GetOrder}">
            <td th:text="${p.getName()}"> <button> </button></td>
            <td th:text="${p.getSecondName()}"></td>
            <td th:text="${p.getAddress()}"></td>
            <td th:text="${p.getApartment()}"></td>
            <td th:text="${p.getPhoneNumber()}"></td>
            <td th:text="${p.getOrderPrice()}"></td>
            <td th:text="${p.getCity()}"></td>
            <td>
                <span style="color: green" th:if="${p.getStatus() == '1'}">Delivered</span>
                <span style="color: red" th:if="${p.getStatus() == '0'}">Not Delivered</span>

            </td>
            <td>
                <input type="submit" value="✓"/>
            </td>

        </tr>
    </table>
    </form>

This is my Controller Class

@Controller
@RequestMapping("/managment")
public class Managment {

    @Autowired
    private PurchaseDataService purchaseDataService;

    @Autowired
    private OrderInterface orderInterface;


    @GetMapping("/order")
    public String goToOrderList(Model model){


        List<Purchases> allStats = purchaseDataService.getAllStats();


        int status = 0;
        model.addAttribute("GetOrder", allStats);
        model.addAttribute("notDelivered", status);



        return "OrderManagment";
    }

    @PutMapping("/order/{id}")
    public String updateInfo(@PathVariable(value = "id") Integer orderId, Model model){


        return "OrderManagment";
    }




}

CodePudding user response:

I achieved my goal, in a different way, but still. Here is the answer:

Controller Class



    @GetMapping(path = "/order/{getOrderId}")
    public String updateStudent(@PathVariable("getOrderId") Integer getOrderId, Model model){

        Purchases purchases =
                orderInterface.findByOrderId(getOrderId);



        model.addAttribute("Delivered", purchases);
        System.out.println(purchases);
        purchases.setStatus("1");

        orderInterface.save(purchases);

        return "redirect:/managment/order";
    }

Here is my Html file

    <table>

        <tr>
            <th>Id</th>
            <th>Imie</th>
            <th>Nazwsiko</th>
            <th>Adres</th>
            <th>Mieszkanie</th>
            <th>Numer telefonu</th>
            <th>Cena</th>
            <th>Miasto</th>
            <th>Delivered</th>
            <th>Finish</th>

        </tr>



            <tr th:each="p : ${GetOrder}">
                <td th:text="${p.getOrderId()}"></td>
                <td th:text="${p.getName()}"></td>
                <td th:text="${p.getSecondName()}"></td>
                <td th:text="${p.getAddress()}"></td>
                <td th:text="${p.getApartment()}"></td>
                <td th:text="${p.getPhoneNumber()}"></td>
                <td th:text="${p.getOrderPrice()}"></td>
                <td th:text="${p.getCity()}"></td>

                <td>
                    <span style="color: green" th:if="${p.getStatus() == '1'}">Delivered</span>

                    <span style="color: red"  th:if="${p.getStatus() == '0'}">NotDelivered </span>

                </td>
                <td>

                    <a th:href="@{/managment/order/{getOrderId}(getOrderId = ${p.getOrderId()})}">Click!</a>

                </td>


            </tr>
        </table>

CodePudding user response:

Instead of 1 form for everything, you can use a form per item:

    <table>
        <tr th:each="p : ${GetOrder}">
            <td th:text="${p.getName()}"> <button> </button></td>
            <td th:text="${p.getSecondName()}"></td>
            <td th:text="${p.getAddress()}"></td>
            <td th:text="${p.getApartment()}"></td>
            <td th:text="${p.getPhoneNumber()}"></td>
            <td th:text="${p.getOrderPrice()}"></td>
            <td th:text="${p.getCity()}"></td>
            <td>
                <span style="color: green" th:if="${p.getStatus() == '1'}">Delivered</span>
                <span style="color: red" th:if="${p.getStatus() == '0'}">Not Delivered</span>

            </td>
            <td>
              <form action="#"  method="put" th:action="@{/order/{id}(id=${p.id})}">
                <input type="submit" value="✓"/>
              </form>
            </td>

        </tr>
    </table>
    

  • Related