//需要操作的表格 点击更新购物车时 把数据全部传入到后台
<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;
}
转载于:https://my.oschina.net/u/1998885/blog/374815