博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js实现配置菜品规格时,向后台传一个json格式字符串
阅读量:5058 次
发布时间:2019-06-12

本文共 11237 字,大约阅读时间需要 37 分钟。

由于本公司做的是订餐平台,那么在上传菜品时,需要配置菜品规格,比如份量(大中小),味道(猛辣,中辣,微辣) 由于这些数据,在表的设计时 没有保存到菜品表,那么在点击保存菜品数据时,配置规格这块数据,我就封装好一个json 格式字符串传给后台再把json转换成对象,这样后台就好处理多了。

一,》》》》》》jsp页面

删除
添加规格
删除
删除
附加属性
添加附加分类

二,》》》》》js 代码

$(function(){        var standardType = '${standardType}';//1单一规格 2多种规格 0无规格        if(standardType !=null && standardType !=""){            if(standardType==1){                $("#singleDiv").show();                $("#variedDiv").hide();            }else if(standardType==2){                $("#singleDiv").hide();                $("#variedDiv").show();            }else if(standardType==0){                $("#singleDiv").show();                $("#variedDiv").hide();            }        }else{            $("#singleDiv").show();            $("#variedDiv").hide();        }                $("#subUpdate").click(function(){            var singleList = assembleClassName();//附加菜品属性            var standardList = assembleStandard();//规格            $("#singleList").val(singleList);            $("#standardList").val(standardList);            var newStandardType = $("#newStandardType").val();            if(newStandardType==2){                if(standardList==null || standardList=="" || standardList=="[]"){                    alert("类型为选多规格时候规格参数必填");                    return;                }            }            $.ajax({                url:"${requestScope['struts.actionMapping'].name}",                data:$("#form").serialize(),                type:"POST",                success:function(){                    alert("保存成功");                    parent.closeWin();                }            });        });    //动态添加附加属性    $('body').on('click','.operty_add',function(){        var dishesParam = '
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
元' +'
' +'
' +'
删除' +'
' +'
' +'
'; $(this).prev().append(dishesParam); }); //删除附加属性 $('body').on('click','.dell_dishesItem',function(){ var current = $(this); var itemId = $(this).attr("itemId"); if(itemId !=null && itemId !=""){ $.ajax({ type : "post", url : "<%=basePath %>book/dishes/backstageversion/update!deleteParamItemById", data : { "itemId" : itemId }, success : function(data){ if(data.success){ current.parent().parent().remove(); }else{ alert('抱歉删除失败') } } }); }else{ $(this).parent().parent().remove(); } }); //动态添加附加分类 $('body').on('click','.add_assortment_all',function(){ var assortment='
' +'
' +'
' +'
' +'
' +'
' +'
' +'
删除' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
元' +'
' +'
' +'
删除' +'
' +'
' +'
' +'
' +'
' +'
附加属性' +'
' +'
' +'
'; $(this).prev().append(assortment); }); //删除附加分类 $('body').on('click','.dell_dishesParam',function(){ var current = $(this); var paramId = $(this).attr("paramId"); if(paramId !=null && paramId !=""){ $.ajax({ type : "post", url : "<%=basePath %>book/dishes/backstageversion/update!deleteDishesParamById", data : { "paramId" : paramId }, success : function(data){ if(data.success){ current.parent().parent().remove(); }else{ alert('抱歉删除失败'); } } }); }else{ $(this).parent().parent().remove(); } }); //组装附加菜品属性 var assembleClassName = function(){ var dishesId=$("#id").val();//菜品ID var dataParam = new Array(); //遍历分类 $(".assortment_small").each(function(){ var dishesParam = new Object(); var id = $(this).find("input[name='paramId']").val(); var name = $(this).find("input[name='paramName']").val(); dishesParam.id=id; dishesParam.name=name; dishesParam.dishesId=dishesId; //遍历分类里面的附加属性 var subItem = new Array(); $(this).find(".activity_class").each(function(){ var dishesParamItem = new Object(); var itemId = $(this).find("input[name='itemId']").val(); var itemName = $(this).find("input[name='itemName']").val(); var itemAdditionPrice = $(this).find("input[name='itemAdditionPrice']").val(); dishesParamItem.id=itemId; dishesParamItem.name=itemName; dishesParamItem.additionPrice=itemAdditionPrice; dishesParamItem.dishesParamId=dishesParam.id; dishesParamItem.dishesId=dishesId; subItem.push(dishesParamItem); }); dishesParam.dishesParamItemList=subItem; dataParam.push(dishesParam); }); var singleList = JSON.stringify(dataParam); return singleList; }; //动态添加多种规格 $('body').on('click','.standard_add',function(){ var standard ='
' +'
' +'
' +'
' +'
' +'
' +'
' +'
' +'
元' +'
' +'
' +'
删除' +'
' +'
' +'
'; $(this).prev().append(standard); }); //动态删除多种规格 $('body').on('click','.dell_dishesStandard',function(){ var current = $(this); var standardId = $(this).attr("standardId"); if(standardId !=null && standardId !=""){ $.ajax({ type : "post", url : "<%=basePath %>book/dishes/backstageversion/update!deleteDishesStandardById", data : { "standardId" : standardId }, success : function(data){ if(data.success){ current.parent().parent().remove(); }else{ alert('抱歉删除失败'); } } }); }else{ $(this).parent().parent().remove(); } }); //组装多规格 var assembleStandard = function(){ var dishesId=$("#id").val();//菜品ID var dataParam = new Array(); $(".standard_class").each(function(){ var dishesStandard = new Object(); var id = $(this).find("input[name='standardId']").val(); var name = $(this).find("input[name='formName']").val(); var price = $(this).find("input[name='standardPrice']").val(); dishesStandard.id = id; dishesStandard.name = name; dishesStandard.price = price; dishesStandard.dishesId = dishesId; dataParam.push(dishesStandard); }); var standardList = JSON.stringify(dataParam); return standardList; }; });

三,页面效果

四,》》》》json 格式数据

规格

[{"id":"","name":"大份","price":"15","dishesId":"4629"},{"id":"","name":"中份","price":"12","dishesId":"4629"},{"id":"","name":"小份","price":"10","dishesId":"4629"}]

属性

[{"id":"3","name":"味道","dishesId":"4629","dishesParamItemList":[{"id":"7","name":"加糖","additionPrice":"1.0","dishesParamId":"3","dishesId":"4629"},{"id":"8","name":"加酸","additionPrice":"2.0","dishesParamId":"3","dishesId":"4629"}]},{"id":"4","name":"温度","dishesId":"4629","dishesParamItemList":[{"id":"9","name":"加热","additionPrice":"0.5","dishesParamId":"4","dishesId":"4629"},{"id":"10","name":"加冰","additionPrice":"0.2","dishesParamId":"4","dishesId":"4629"}]}]

其中dishesId 菜品ID 表的设计和 和对象代码我就不贴出来了。可以看json 格式数据去设计表。

 

转载于:https://www.cnblogs.com/SHMILYHP/p/9154610.html

你可能感兴趣的文章
Primary definition
查看>>
第二阶段冲刺-01
查看>>
BZOJ1045 HAOI2008 糖果传递
查看>>
发送请求时params和data的区别
查看>>
JavaScript 克隆数组
查看>>
eggs
查看>>
一步步学习微软InfoPath2010和SP2010--第七章节--从SP列表和业务数据连接接收数据(4)--外部项目选取器和业务数据连接...
查看>>
如何增强你的SharePoint 团队网站首页
查看>>
FZU 1914 Funny Positive Sequence(线性算法)
查看>>
oracle 报错ORA-12514: TNS:listener does not currently know of service requested in connec
查看>>
基于grunt构建的前端集成开发环境
查看>>
MySQL服务读取参数文件my.cnf的规律研究探索
查看>>
java string(转)
查看>>
__all__有趣的属性
查看>>
BZOJ 5180 [Baltic2016]Cities(斯坦纳树)
查看>>
写博客
查看>>
利用循环播放dataurl的视频来防止锁屏:NoSleep.js
查看>>
python3 生成器与迭代器
查看>>
java编写提升性能的代码
查看>>
ios封装静态库技巧两则
查看>>