表格批量修改數據

//須要操做的表格  點擊更新購物車時 把數據所有傳入到後臺 
 <table width="960" border="0" cellspacing="0" cellpadding="0" class="tab2">
          <tr>
            <th width="39">序號</th>
            <th width="80">貨品編號</th>
            <th width="83">貨品名稱</th>
            <th width="87">貨品規格</th>
            <th width="68">數量</th>
            <th width="63">單位</th>
            <th width="83">單價</th>
            <th width="87">公/私</th>
            <th width="83">採購單位</th>
            <th width="66">姓名</th>
            <th width="100">備註</th>
            <th width="70">操做</th>
          </tr>
          <c:forEach items="${cartVos}" var="p"  varStatus="status">
              <tr class="itemlist">
               <td>${status.index+1}</td>
                <td name="productId" class="productId">${p.value.productid }</td>
                <td class="productname">${p.value.productname }</td>
                <td class="spec">${p.value.spec }</td>
                <td>
                    <div class="num">
                        <p class="shu1" aa="${p.value.productid}" ee="${p.value.id}" name="aa">${p.value.num }</p>
                        <div class="num_btn">
                            <img src="${ctx}/static/common/images/2/num_add.png" name="add1"  bb="${p.value.productid}" dd="${p.value.id }"/>
                            <img src="${ctx}/static/common/images/2/num_jian.png" cc="${p.value.productid}" ff="${p.value.id }"name="jian1"/>
                        </div>
                    </div>
                </td>
                <td class="unit">${p.value.unit }</td>
                <td name="price" class="price" value="${p.value.productid }" price="${p.value.productprice }">${p.value.productprice }</td>
                <td><select class="privatePublic" id="${p.value.productid }_pp"><option  value="公" <c:if test="${ p.value.privatePublic eq '公'}">  selected</c:if>>公</option><option value="私" <c:if test="${ p.value.privatePublic eq '私'}">selected</c:if>>私</option></select></td>
                <td class="productplace">${p.value.productplace }</td>
                <td>aa</td>
                <td><input class="remark" id="${p.value.productid }_remark" type="text" class="txt2" value="${p.value.remark }" /></td>
                <td><input type="hidden" name="id" value="${ p.value.id}"class="txt3"/></td>
                <td>
                    <a style="width:52px;" href="#" onclick="deleteItem('${p.value.productid }')">刪除</a>
                </td>
              </tr>
          </c:forEach>
        </table>
        
        <div class="gw_btn">更新購物車</div>
        </form>
        
        <div class="gw_cont">
            <p>運輸方式:
                 <select id="trasportWay">
                    <option>海運</option>
                    <option>空運</option>
                    <option>陸運</option>
                    <option>自帶</option>
                </select>
            </p>
            <p>付款方式:
                <select id="payWay">
                    <option>轉財務</option>
                    <option>轉我倌</option>
                    <option>現金</option>
                    <option>其餘</option>
                </select>
            </p>
            <p>訂單日期:${nowDate }</p>
            <p>總參考金額:<span class="totalPrice">${totalPrice }</span></p>
        </div>
        
        <div class="dinggou">
            <p>
                <span>訂購留言:</span><textarea id="orderRemark" rows="3" cols="80"></textarea>
            </p>
        </div>
        
        <div class="gw_btn2">
            <p ><input type="button" onclick="saveOrder()" class="baocun" value="保存訂單" /></p>
            <p class="qingkong">清空購物車</p>
        </div>
        
      </div>
    //js 代碼  用jquery遍歷循環每一行數據而且拼接成json的格式,這樣就每一行就是一個對象,傳入後臺一個json串
            $(".gw_btn").click(function(){
                 var dd="";
                 var datas ="";  
                var checked = true;
                if($('#tab').attr('style')=='display: table;'&& $("#tab").find("tr").length>2){
                    checked = form.check(false);
                    var data1="[";
                        $(".item2list").each(function(){
                            data1+='{"productname":\"'+$(this).find('input[name=productname]').val()+"\"";
                            data1+=',"spec":\"'+$(this).find('input[name=spec]').val()+"\"";
                            data1+=',"num":'+$(this).find('input[name=num]').val();
                            data1+=',"unit":\"'+$(this).find('input[name=unit]').val()+"\"";
                            data1+=',"privatePublic":\"'+$(this).find('.privatePublic option:selected').text()+"\"";
                            data1+=',"productplace":\"'+$(this).find('input[name=productplace]').val()+"\"";
                            data1+=',"remark":\"'+$(this).find('input[name=remark]').val()+"\"";
                            data1+='},'
                        });
                        if(data1.length>1)
                            data1=data1.substring(0,data1.length-1);
                        data1+="]";
                        dd=data1;
                }

                if(checked){
                    var data="[";
                    $(".itemlist").each(function(){
                        data+='{"productid":'+$(this).find('.productId').html()+',"num":'+$(this).find('.shu1').html();
                        data+=',"privatePublic":\"'+$(this).find('.privatePublic option:selected').text()+"\"";
                        data+=',"remark":\"'+$(this).find(".remark").val()+"\"";
                        data+=',"id":'+$(this).find('input[name=id]').val();
                        data+= "},";
                    });
                    if(data.length>1)
                        data=data.substring(0,data.length-1);
                    data+="]";
                    datas =data; 
                    
                    
                 var url = "${ctx }/cart/updateCart?";
                 $.ajax({
                    type: "post",
                    url: url,
                    data:'datas='+datas+'&dd='+dd,
                    dataType: 'json',
                    success: function (data) {
                        if(data.msg == true){
                            alert("更新購物車成功"); 
                            window.location.href="${ctx}/cart/showCart";   
                        }else{
                            alert("更新購物車失敗");
                        }
                    },
                    error: function (data) {
                        alert("調用ajax失敗");
                    }
                    });
                }
             })
    //後臺用gson 把json串解析成你想要的數據,根據vo,放入對象裏
    @ResponseBody
    @RequestMapping(value="updateCart")
    public Map<String,Object>  updateCart(ModelMap modelMap,@RequestParam Map<String, Object> map,HttpServletRequest request,HttpServletResponse response){
        //現session中已有的數據
        String datas = request.getParameter("datas");
        //手動輸入訂單的數據
        String dd = request.getParameter("dd");
            Gson gson=new Gson();
            List<CartVo> list = gson.fromJson(datas, new TypeToken<List<CartVo>>(){}.getType());
            HttpSession session = request.getSession(true);
            //從session中取出已有的產品信息
            Map<Integer,CartVo> sessionCartVo = (HashMap<Integer,CartVo>)session.getAttribute("cartVo");
            Double price = 0.0;
            Double totalPrice =0.0;
            Double sum = 0.0;
            for (CartVo cartVo : list) {
                if(sessionCartVo.containsKey(cartVo.getProductid()) ||sessionCartVo.containsKey(cartVo.getId())){
                    CartVo     cartItem=null;
                    //若是id 不等於0 說明是手動錄入的訂單
                    if(cartVo.getId() != 0){
                        cartItem = (CartVo)sessionCartVo.get(cartVo.getId());
                    }else{
                        cartItem = (CartVo)sessionCartVo.get(cartVo.getProductid());
                    }
                    cartItem.setNum(cartVo.getNum());
                    cartItem.setPrivatePublic(cartVo.getPrivatePublic());
                    cartItem.setRemark(cartVo.getRemark());
                    if(!StringUtil.isNullOrBlank(cartItem.getProductprice())){
                        price = Double.valueOf(cartItem.getProductprice());
                    }else{
                        price = 0.0;
                    }
                    totalPrice = price * cartVo.getNum();
                }
                sum += totalPrice;
            }
            map.put("totalPrice",sum);
            //修改購物車成功後 查看是否有添加數據 有就在現有的session map裏進行更新
            if(!StringUtil.isNullOrBlank(dd)){
                List<CartVo> addList = gson.fromJson(dd, new TypeToken<List<CartVo>>(){}.getType());
                //存放session中沒有記錄的map
                Map<Integer,CartVo> newMap= new HashMap<Integer, CartVo>();
                Random random = new Random();
                for(CartVo vo:addList){
                    //生成隨機數
                    int num = random.nextInt();
                    Long date = new Date().getTime();
                    int id = new Long(date).intValue()+num;
                    vo.setProductid(id);
                    if(sessionCartVo!=null){
                        sessionCartVo.put(vo.getProductid(),vo);
                    }else{
                        newMap.put(vo.getProductid(),vo);
                    }
                }
                if(sessionCartVo!=null){
                    session.setAttribute("cartVo",sessionCartVo);
                }else{
                    session.setAttribute("cartVo",newMap);
                }
            }
        map.put("msg",true);
        return map;
    }
相關文章
相關標籤/搜索