有沒有辦法在按鈕 onClick 上將字串“狀態”的值更新為“1”。如果沒有辦法改變它按鈕 onClick 有另一種方法嗎?
我想按 Id 查找行并更新"status"的值。
這是帶有 Thymeleaf 的 Html 檔案
<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>
這是我的控制器類
@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";
}
}
uj5u.com熱心網友回復:
您可以為每個專案使用一個表單,而不是對所有內容使用 1 個表單:
<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="#" th:method="put" th:action="@{/order/{id}(id=${p.id})}">
<input type="submit" value="?"/>
</form>
</td>
</tr>
</table>
為清楚起見:如果您@PutMapping與 Thymeleaf 一起使用,請務必在模板中spring.mvc.hiddenmethod.filter.enabled=true使用th:method。有關更多詳細資訊,請參閱在百里香葉中洗掉或放置方法。
uj5u.com熱心網友回復:
我以不同的方式實作了我的目標,但仍然如此。這是答案:
控制器類
@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";
}
這是我的 Html 檔案
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379059.html
