"使用 Thymeleaf 循环输出商品列表并实现删除功能" "本文介绍如何在 Thymeleaf 模板中使用 th:each 循环输出商品列表,并为每个商品添加删除按钮。点击删除按钮可以获取对应商品信息,并执行删除操作。" "在 Thymeleaf 模板中,可以使用 th:each 循环遍历商品列表,并为每个商品添加一个删除按钮。点击删除按钮时,可以将对应商品的 ID 传递给 JavaScript 函数,该函数可以执行删除操作。" "以下是一个示例代码:" "html\" \"<table>\" \" <tr th:each="product : ${productList}">\" \" <td th:text="${product.name}"></td>\" \" <td th:text="${product.price}"></td>\" \" <td><button th:onclick="'deleteProduct('' + ${product.id} + '')'">删除</button></td>\" \" </tr>\" \"</table>\" \"\" \"<script th:inline="javascript">\" \" function deleteProduct(productId) {\" \" // 根据商品ID执行删除操作\" \" // 可以通过Ajax请求后端接口实现删除逻辑\" \" // 也可以直接修改前端数据,从商品列表中移除对应商品\" \" }\" \"</script>\" \"" "在上述代码中,我们使用 th:each 循环遍历商品列表,并输出商品的名称、价格以及一个删除按钮。删除按钮的点击事件绑定了一个名为 deleteProduct 的 JavaScript 函数,并将对应商品的 ID 作为参数传递给该函数。" "在 deleteProduct 函数中,我们可以根据商品 ID 执行删除操作。具体的实现方式可以根据你的需求来选择,可以通过 Ajax 请求后端接口实现删除逻辑,也可以直接在前端修改商品列表数据,将对应的商品从列表中移除。"


原文地址: https://www.cveoy.top/t/topic/pLi8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录