Thymeleaf计算总价,如何让后台数据库数据和前台数据相乘

在总价的th:text中我要怎么拿到单价和数量的乘积,数量是前端计算得出,单价是后台从数据库拿出的数据

<div class="prize_bar">
        <div class="num_name fl">单价:</div>
    <span class="show_pirze">¥<em id="price" th:text="${product.price}"></em></span>
    <span class="show_unit">单  位:个</span>
</div>
<div class="goods_num clearfix">
    <div class="num_name fl">数 量:</div>
    <div class="num_add fl">
        <input id="quantity" name="quantity" type="text" class="num_show fl" th:value="1">
        <a type="button"  class="add fr" onclick="valueAdd()">+</a>
        <a type="button"  class="minus fr" onclick="valueSub()">-</a>
    </div> 
</div>
<div class="total">总价:<em id="totalPrice" name="totalPrice" th:text="" th:value=""></em></div>



【以下回答由 GPT 生成】

首先,为了实现这个功能,我们需要通过后台传递商品单价和数量到前台页面中。我们可以在后台使用Spring MVC框架,并使用Thymeleaf模板引擎来进行数据的传递和展示。

步骤如下:

  1. 创建一个Spring MVC的控制器类,用于处理请求和返回视图。
@Controller
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/product")
    public String getProduct(Model model) {
        // 从数据库中获取商品的单价
        double price = productService.getProductPrice();

        // 将单价传递给前台页面
        model.addAttribute("price", price);

        return "product";
    }
}
  1. 创建一个商品服务类,用于从数据库中获取商品的单价。
@Service
public class ProductService {

    public double getProductPrice() {
        // 从数据库中获取商品的单价并返回
    }
}
  1. 创建一个Thymeleaf模板页面,用于展示商品信息和计算总价。
<div class="prize_bar">
    <div class="num_name fl">单价:</div>
    <span class="show_price">¥<em id="price" th:text="${price}"></em></span>
    <span class="show_unit">单位:个</span>
</div>
<div class="goods_num clearfix">
    <div class="num_name fl">数量:</div>
    <div class="num_add fl">
        <input id="quantity" name="quantity" type="text" class="num_show fl" th:value="1">
        <a type="button" class="add fr" onclick="valueAdd()">+</a>
        <a type="button" class="minus fr" onclick="valueSub()">-</a>
    </div>
</div>
<div class="total_price">
    <span>Total Price: </span>
    <span id="totalPrice" th:text="${#numbers.formatDecimal(price * 1, 2)}"></span>
</div>

在这个示例代码中,我们通过Thymeleaf的表达式${price}${#numbers.formatDecimal(price * 1, 2)}来分别展示商品的单价和总价。#numbers.formatDecimal(price * 1, 2)用于将价格格式化为两位小数。

同时,我们还需要根据数量的增减来实时更新总价。为了实现这一点,我们可以编写一个JavaScript函数来监听数量的变化并重新计算总价。

  1. 编写JavaScript函数,用于更新总价。
function valueAdd() {
    var quantity = parseInt($("#quantity").val());
    quantity++;
    $("#quantity").val(quantity);
    calculateTotalPrice();
}

function valueSub() {
    var quantity = parseInt($("#quantity").val());
    if (quantity > 1) {
        quantity--;
        $("#quantity").val(quantity);
    }
    calculateTotalPrice();
}

function calculateTotalPrice() {
    var price = parseFloat($("#price").text());
    var quantity = parseInt($("#quantity").val());
    var totalPrice = price * quantity;
    $("#totalPrice").text(totalPrice.toFixed(2));
}

在这个示例代码中,我们定义了三个函数。valueAdd()函数用于增加数量,valueSub()函数用于减少数量,calculateTotalPrice()函数用于计算总价并更新到页面上。

这样,当用户点击加号或减号按钮时,页面会根据数量和单价自动更新总价。

综上所述,通过上述的代码修改,我们能够实现在Thymeleaf中计算总价并将结果显示在页面上。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^