【问题标题】:How to update the table when same record is inserted using jquery and ajax使用jquery和ajax插入相同记录时如何更新表
【发布时间】:2017-02-14 03:53:27
【问题描述】:

我正在开发 POS 应用程序,我在搜索条形码后检索产品(保存在产品表中的数据库中),有添加购物车按钮,我单击该按钮将在订单表中添加产品详细信息、价格和数量数据库,添加到数据库后会显示,如果我输入相同的产品,那么它的价格和数量应该增加。

当我单击同一产品的添加购物车时,它正在从数据库中检索具有更新数量和价格的记录,但它正在添加新行而不是更新现有行(因为追加)有没有其他方法可以创建表这样每次我都不必追加新行。

jquery代码是

$(document).ready(function(){

$("#submit").click(function(event){
    var barcode=$(this).closest("tr").find("td:nth-child(1)").text();
    var product=$(this).closest("tr").find("td:nth-child(2)").text();
    var price=$(this).closest("tr").find("td:nth-child(3)").text();
    var order={"barcodeid":barcode,"pname":product,"price":price,"qty":1}


    console.log(orderid);
    $.ajax({
        type:'POST',    
        url:'/pos/addproduct',
        dataType: "json",
        data:JSON.stringify(order),
        contentType: "application/json; charset=utf-8",
        success:
            function(data){

            $.each(data,function(index,value){

                var row=$("<tr><td>"+value.oid+"</td>"+"<td>"+value.pname+"</td>"+
                        "<td>"+value.qty+"</td>"+"<td>"+value.price+"</td>"+"<td>"
                        +value.barcodeid+"</td></tr>");
                $("#order").append(row).removeClass("hidden");

            })

        }
    })
});
})

订单表是

<table id="order" class="hidden" align="center">
<tr>
<th>OrderId</th>
<th>ProductName</th>
<th>Quantity</th>
<th>Price</th>
<th>Barcode</th>
</tr>
<tr >
</table>

【问题讨论】:

  • 我建议您在进行 ajax 调用之前先创建表。然后将唯一 id (uuid) 关联到数据库中的每个产品。然后在您的表格中,每一行都有一个与产品自己的 uuid 匹配的 id。在您的成功方法中,首先尝试获取 $(#uuid) :如果它返回一些东西,则表示您之前已经购买过该产品,只需获取数量单元格(您可以通过在数量 td 上放置名称属性来做到这一点) ,否则只需像今天一样创建一个新行。(但在 tr 上使用 id=uuid 属性,在 qty td 上使用 name="qtyCell"。)
  • 我希望只有当有人点击添加购物车按钮时才创建表格
  • 为什么在你的成功函数中使用 $.each ?您的服务器在 POST 调用返回响应中放入了什么?
  • $.each 用于迭代服务器的响应以打印表格并从服务器返回订单详细信息
  • 在 jQuery 中使用 Kendo UI?对于所有这些类型的问题,它都是免费且经过实战考验的。

标签: javascript jquery ajax spring-mvc


【解决方案1】:

首先我假设你从服务器得到的答案是一个 json 对象,它给出了一个 orderId(这对于购物车来说是唯一的,即它不会在会话范围内改变),然后是所需的添加产品: - 它的产品名称 - 仅此产品订单的总价(即单价 * 数量) - 数量 - 条形码id

我创建了一个 jsfiddle 来向您展示我的想法:example

它就像你自己的代码一样工作,除了

  • 显然,因为我没有你的服务器,我使用 jsfiddle AJAX 服务返回我假设你的服务器在 POST 请求中返回的内容
  • 因此,服务器端数据通过 orderId 和订购数量的两个全局变量进行处理(对于每个产品,这就是为什么它是一个对象)
  • 在成功函数中,我寻找一个匹配“数量+条形码”的单元格(td),如果找到,我更新它和总价单元格(id是“价格”+条形码)。如果找不到,我会创建行(这是您的代码),同时为数量和价格单元格设置适当的 ID。

    SO中下面的sn-p不起作用(因为没有AJAX服务)请在上面的jsfiddle链接中运行

var qty={};
var orderId = 12;
$(document).ready(function() {

  $("a[id^=submit]").click(function(event){
    var barcode=$(this).closest("tr").find("td:nth-child(1)").text();
    var product=$(this).closest("tr").find("td:nth-child(2)").text();
    var price=$(this).closest("tr").find("td:nth-child(3)").text();
    var order={"barcodeid":barcode,"pname":product,"price":price,"qty":1}

    if (qty.hasOwnProperty(barcode)) {
      qty[barcode]++;
      qty[barcode] = 1;
    }
    $.ajax({
      type:'POST',    
      url:'/echo/json/',
      dataType: "json",
      data://JSON.stringify(order),
      {
        json: JSON.stringify({
          oid: orderId,
          pname: product,
          price: parseFloat(price)*qty[barcode],
          qty:qty[barcode],
          barcodeid:barcode
        }),
        delay: 1
      },
      contentType: "application/json; charset=utf-8",
      success:function(data) {
          value = data
          var qtyItm = $("#qty"+value.barcodeid);
          if (qtyItm.length) {
            qtyItm.html(qty[barcode]);
            $("#price"+value.barcodeid).html(value.price);
          } else {
            var row=$("<tr><td>"+value.oid+"</td>"+"<td>"+value.pname+"</td>"+
              "<td id=\"qty"+value.barcodeid+"\">"+value.qty+"</td>"+
              "<td id=\"price"+value.barcodeid+"\">"+value.price+"</td>"+
              "<td>"+value.barcodeid+"</td></tr>");
            $("#order").append(row).removeClass("hidden");
          }
        },
      fail:function() {
          alert('problem')
      }
    })
  });
});
table {
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
  <table align="center">
    <tr><th>Barcode</th><th>ProductName</th><th>Price</th><th>ADD</th></tr>
    <tr><td>97</td><td>johson</td><td>44.0</td><td><a href="#" id="submit97">Add</a></td></tr>
    <tr><td>98</td><td>another product</td><td>10.0</td><td><a href="#" id="submit98">Add</a></td></tr>
  </table>

  <table id="order" align="center">
    <tr><th>OrderId</th><th>ProductName</th><th>Quantity</th><th>Price</th><th>Barcode</th></tr>
  </table>

</body>

【讨论】:

  • 如果我从数据库中检索一条记录,您的想法可以正常工作,但如果是记录列表,我必须使用 $.each 来获取不更新的记录
  • 我对所有对象进行了更改,并且成功了
【解决方案2】:

简单的解决方案是将整个表格创建到 JS 中,而不会过多地中断您的代码..

JS代码---

    $(document).ready(function(){

    $("#submit").click(function(event){
        var barcode=$(this).closest("tr").find("td:nth-child(1)").text();
        var product=$(this).closest("tr").find("td:nth-child(2)").text();
        var price=$(this).closest("tr").find("td:nth-child(3)").text();
        var order={"barcodeid":barcode,"pname":product,"price":price,"qty":1}


        console.log(orderid);
        var TableData = "";


         $.ajax({
                type:'POST',    
                url:'/pos/addproduct',
                dataType: "json",
                data:JSON.stringify(order),
                contentType: "application/json; charset=utf-8",
                success:
                    function(data){
TableData = TableData + '<table id="order"  align="center">'
        TableData = TableData + '<tr>';
        TableData = TableData + '<th>OrderId</th>';
        TableData = TableData + '<th>ProductName</th>';
        TableData = TableData + '<th>Quantity</th>';
        TableData = TableData + '<th>Price</th>';
        TableData = TableData + '<th>Barcode</th>';
        TableData = TableData + '</tr>';
                    $.each(data,function(index,value){
                        TableData = TableData + "<tr><td>"+value.oid+"</td><td>"+value.pname+"</td>";
                        TableData = TableData + "<td>"+value.qty+"</td>"+"<td>"+value.price+"</td>";
                        TableData = TableData + "<td>"+value.barcodeid+"</td></tr>";
                    })
 TableData = TableData + '</table>'
                }
            });


        $("#OrderDiv").html("");
        $("#OrderDiv").html(TableData);
});
})

在 HTML 替换时

<table id="order" class="hidden" align="center">
<tr>
<th>OrderId</th>
<th>ProductName</th>
<th>Quantity</th>
<th>Price</th>
<th>Barcode</th>
</tr>
<tr >
</table>

<div id = 'OrderDiv'> </div>

请求成功后应用必要的 CSS。

【讨论】:

    【解决方案3】:

    我遍历数据库中的所有对象并检查 id 是否与价格和条形码相同,如果相同则使用它们的 div 元素更新价格和数量

        function(data){
                $.each(data,function(index,value){
                    var temp = "qty" + data[index].barcodeid.trim();
                    var qtyitm=$("#"+temp);
                    if(qtyitm.length != 0){
                        qtyitm.html(data[index].qty);
                        //("#price"+(data[index].barcodeid.trim())).html(data[index].price);
                        temp = "price" + data[index].barcodeid.trim();
                        qtyitm = $("#"+temp);
                        qtyitm.html(data[index].price);
                    }
                    else{
                        var row=$("<tr><td>"+data[index].oid+"</td>"+"<td>"+data[index].pname.trim()+
                                "</td>"+
                                "<td id=\"qty"+data[index].barcodeid.trim()+"\">"+data[index].qty+"</td>"+
                                "<td id=\"price"+data[index].barcodeid.trim()+"\">"+data[index].price+"</td>"+
                                "<td>"
                                +data[index].barcodeid.trim()+"</td></tr>");
    
                        $("#order").append(row).removeClass("hidden");
                        }
                })
    
            }
    

    【讨论】:

      猜你喜欢
      • 2014-10-11
      • 2022-01-26
      • 2013-12-27
      • 2013-02-06
      • 2023-01-28
      • 2013-07-04
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      相关资源
      最近更新 更多