jquery 实战 两表级联

Code:
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <%@include file="/common/common.jsp" %>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. </head>  
  7. <body>  
  8. <script src="/pay/js/jquery.js" type="text/javascript" charset="utf-8"></script>   
  9. <script type="text/javascript">  
  10. $(document).ready(function(){  
  11.       
  12.     //列出所有的产品类别,添加产品时需要选择产品类别  
  13.     var url="/pay/productionCategory.do?from=add"  
  14.     $.get(url,null,function (date){  
  15.         objs=eval(date);  
  16.         for(var i in objs) {  
  17.             var obj=objs[i];  
  18.             $("#productionCategory_id").append("<option value="+obj.id+">"+obj.id+"   "+obj.name+"</option>");  
  19.         }  
  20.     });  
  21.     //添加产品类别.同时在选择框中加入新添加的产品类别选项  
  22.     $("#addProductionCategory").click(function(){  
  23.         openWin("/pay/productionCategory/add.jsp","addProductionCategory",600,400);  
  24.     })  
  25.       
  26.     //列出所有的工序,添加产品工序时需要选择工序  
  27.     var url="/pay/process.do?from=add"  
  28.     $.get(url,null,  
  29.         function (date){  
  30.             objs=eval(date);  
  31.             $("#process_id").empty();  
  32.             $("#process_id").append("<option disabled=disabled>ID    编号    工序</option>")  
  33.             for(var i in objs) {  
  34.                 var obj=objs[i];  
  35.                 $("#process_id").append("<option value="+obj.id+">"+obj.id+" "+obj.name+" "+obj.num+"</option>");  
  36.             }  
  37.     });  
  38.     //添加工序.同时在选择框中加入新添加的工序选项  
  39.     $("#addProcess").click(function(){  
  40.         openWin("/pay/process/add.jsp","addProcess",600,400);  
  41.     })  
  42.       
  43.     //双击产品行,然后在右边的table中显示此产品的所有工序信息,同事高亮此行  
  44.     $("#inBody1 tr").dblclick(function(){  
  45.         //覆盖旧的选定行的背景颜色  
  46.         $("#inBody1 tr").css("background","white");  
  47.         //设定选定行的背景色  
  48.         $(this).css("background","red");  
  49.         //获得选定行的产品的ID,此ID的值是该行tr的ID值  
  50.         var findProductProByProduction_id=$(this).attr("id");  
  51.         //根据产品的ID,查找该产品的所有工序  
  52.         var url="/pay/production.do?findProductProByProduction_id="+findProductProByProduction_id;  
  53.         $.get(url,null,function(date){  
  54.             objs=eval(date);  
  55.             $("#inBody2 tr").remove();  
  56.             if(objs == "" || objs==null)  
  57.                 alert("该产品还未有设定工序");  
  58.             for(var i in objs){  
  59.                 var obj=objs[i];  
  60.                 //把找到的工序按一定的格式显示到右边的table中,同时也要添加复选框  
  61.                 $("#inBody2").append("<tr height=30px style='background-color: white;border: 1px solid #C7DB51;font-size: 8pt;padding: 4px;'><td><input name=selId2 type=checkbox id="+obj.id+"></td><td>"  
  62.                         +obj.id+"</td><td>"+obj.num+"</td><td>"+obj.processName+"</td><td>"+obj.price+"</td><td>"+obj.waste+"</td><td>"+obj.productionName+"</td></tr>");  
  63.             }  
  64.         })  
  65.         //设定要添加工序中产品的ID为选定的产品ID  
  66.         $("#production_id").val($(this).attr("id"));  
  67.     }).mouseover(function(){  
  68.         $(this).css("cursor","pointer");  
  69.     });  
  70.       
  71.     //添加一个产品工序  
  72.     $("#addProcessBut").click(function(){  
  73.         var ID=$("#id").val();  
  74.         var id=$.trim(ID);  
  75.         var productionID=$("#production_id").val();  
  76.         var production_id=$.trim(productionID);  
  77.         var processID=$("#process_id").val();  
  78.         var process_id=$.trim(processID);  
  79.         var NUM=$("#num").val();  
  80.         var num=$.trim(NUM);  
  81.         var PRICE=$("#price").val();  
  82.         var price=$.trim(PRICE);  
  83.         var WASTE=$("#waste").val();  
  84.         var waste=$.trim(WASTE);  
  85.          if(process_id=="0" || num=="" || price=="" || waste==""){  
  86.              alert("表单未填写完整,请重新填写完整");  
  87.              return false;  
  88.         }  
  89.         //double验证正则表达式  
  90.         var reg =/^/d*/.?/d{0,2}$/;  
  91.         if(!reg.test(price)){  
  92.              alert("请在工序单价栏填写数字");  
  93.              $("#price").get(0).select();  
  94.              return false;  
  95.         }  
  96.         if(!reg.test(waste)){  
  97.              alert("请在废品扣价栏填写数字");  
  98.              $("#waste").get(0).select();  
  99.              return false;  
  100.         }  
  101.         //id为0时表示添加操作,不为0,则表示更新,因为ID属性是hidden的,所以他只能是程序设定  
  102.         if(id=="" || id==0 || id==null){  
  103.             var url="/pay/productionProcess.do?command=addProductionProcess&from=add";  
  104.             $.post(url,{production_id:production_id,process_id:process_id,num:num,price:price,waste:waste},  
  105.                 function (date){  
  106.                     obj=eval(date);  
  107.                     //添加到数据库后,在右table中,我们在他的最后添加上我们添加的一行,BUG:但是它的复选框的值是无效的,虽然可以显示出复选框  
  108.                     $("#inBody2").append("<tr height=30px style='background-color: white;border: 1px solid #C7DB51;font-size: 8pt;padding: 4px;'><td><input name=selId2 type=checkbox id="+obj.id+"></td><td>"  
  109.                         +obj.id+"</td><td>"+obj.num+"</td><td>"+obj.processName+"</td><td>"+obj.price+"</td><td>"+obj.waste+"</td><td>"+obj.productionName+"</td></tr>");  
  110.                     $("#num").val("");  
  111.                     $("#price").val("");  
  112.                     $("#waste").val("");  
  113.                     $("#process_id").val("0");  
  114.                     alert("添加成功");  
  115.                 });  
  116.         }  
  117.         //更新  
  118.         else{  
  119.             var url="/pay/productionProcess.do?command=updateProductionProcess";  
  120.             $.post(url,{id:id,production_id:production_id,process_id:process_id,num:num,price:price,waste:waste},  
  121.                 function (date){  
  122.                     alert("更新成功");  
  123.                     $("#text").load("/pay/production.do");   
  124.                 });  
  125.         }  
  126.     });  
  127.       
  128.     //添加修改产品  
  129.     $("#addProductionBut").click(function(){  
  130.         //为避免与productionProcess的ID属性冲突,将id改为P_id  
  131.         var ID=$("#P_id").val();  
  132.         var id=$.trim(ID);  
  133.         var NAME=$("#name").val();  
  134.         var name=$.trim(NAME);  
  135.         var UNIT=$("#unit").val();  
  136.         var unit=$.trim(UNIT);  
  137.         var NUM=$("#productionNum").val();  
  138.         var num=$.trim(NUM);  
  139.         var PC_id=$("#productionCategory_id").val();  
  140.         var productionCategory_id=$.trim(PC_id);  
  141.          if(productionCategory_id=="0" || num=="" || name=="" || unit==""){  
  142.              alert("表单未填写完整,请重新填写完整");  
  143.              return false;  
  144.         }  
  145.         if(id=="" || id==0 || id==null){  
  146.             var url="/pay/production.do?command=addProduction&from=add";  
  147.             $.post(url,{name:name,unit:unit,num:num,productionCategory_id:productionCategory_id},  
  148.                 function (date){  
  149.                     alert("添加成功");  
  150.                     $("#text").load("/pay/production.do");   
  151.                 });  
  152.         }  
  153.         //更新  
  154.         else{  
  155.             var url="/pay/production.do?command=updateProduction";  
  156.             $.post(url,{id:id,name:name,unit:unit,num:num,productionCategory_id:productionCategory_id},  
  157.                 function (date){  
  158.                     alert("更新成功");  
  159.                     $("#text").load("/pay/production.do");   
  160.                 });  
  161.         }  
  162.     });  
  163.       
  164.       
  165.     //产品更新查询操作  
  166.     $("#updateProduction").click(function(){   
  167.         var count=0;  
  168.         var updateId=0;  
  169.         //是复选框1的选项值  
  170.         $("[name='selId1']").each(function(){        
  171.             if($(this).attr("checked")){  
  172.                 count++;  
  173.                 updateId=$(this).attr("id");  
  174.             }   
  175.         });  
  176.         if(count>1 || count<1) {  
  177.             alert("更新操作,请选择一个");  
  178.             return false;  
  179.         } else {  
  180.             var url="/pay/production.do?command=updateInput&id="+updateId;  
  181.             $.get(url,null,function (date){  
  182.                 var obj=eval(date);  
  183.                 $("#P_id").val(obj.id);  
  184.                 $("#name").val(obj.name);  
  185.                 $("#productionNum").val(obj.num);  
  186.                 $("#unit").val(obj.unit);  
  187.                 var url="/pay/productionCategory.do?from=add";  
  188.                 $.get(url,null,function (date){  
  189.                     objs=eval(date);  
  190.                     $("#productionCategory_id").empty();  
  191.                     for(var i in objs) {  
  192.                         var obj2=objs[i];  
  193.                         if(obj2.id==obj.productionCategory_id)  
  194.                             $("#productionCategory_id").append("<option value="+obj2.id+" selected=selected>"+obj2.id+"   "+obj2.name+"</option>");  
  195.                         $("#productionCategory_id").append("<option value="+obj2.id+">"+obj2.id+"   "+obj2.name+"</option>");  
  196.                     }  
  197.                 });  
  198.             });  
  199.         }     
  200.     });  
  201.       
  202.     //产品工序更新查询操作  
  203.     $("#updateProductionProcess").click(function(){   
  204.         var count=0;  
  205.         var updateId=0;  
  206.         $("[name='selId2']").each(function(){        
  207.             if($(this).attr("checked")){  
  208.                 count++;  
  209.                 updateId=$(this).attr("id");  
  210.             }   
  211.         });  
  212.         if(count>1 || count<1) {  
  213.             alert("更新操作,请选择一个");  
  214.             return false;  
  215.         } else {  
  216.             var url="/pay/productionProcess.do?command=updateInput&id="+updateId;  
  217.             $.get(url,null,function (date){  
  218.                 var obj=eval(date);  
  219.                 $("#id").val(obj.id);  
  220.                 $("#waste").val(obj.waste);  
  221.                 $("#num").val(obj.num);  
  222.                 $("#price").val(obj.price);  
  223.                 $("#production_id").val(obj.production_id);  
  224.                   
  225.                 var url="/pay/process.do?from=add"  
  226.                 $.get(url,null,function (date){  
  227.                     objs=eval(date);  
  228.                     $("#process_id").empty();  
  229.                     $("#process_id").append("<option disabled=disabled>ID    编号    工序</option>")  
  230.                     for(var i in objs) {  
  231.                         var obj2=objs[i];  
  232.                         if(obj2.id==obj.process_id)  
  233.                             $("#process_id").append("<option value="+obj2.id+" selected=selected>"+obj2.id+" "+obj2.name+" "+obj2.num+"</option>");  
  234.                         $("#process_id").append("<option value="+obj2.id+" >"+obj2.id+" "+obj2.name+" "+obj2.num+"</option>");  
  235.                     }  
  236.                 });  
  237.             });  
  238.         }     
  239.     });  
  240.       
  241.     //删除操作  
  242.     $("[name=del]").click(function(){  
  243.         var count=0;  
  244.         var delIds="";  
  245.         //因为有div所以是.parent().parent()  
  246.         if($(this).parent().parent().attr("id")=="td1_2"){  
  247.             $("[name='selId1']").each(function(){        
  248.                 if($(this).attr("checked")){  
  249.                     count++;  
  250.                     delIds=delIds+$(this).attr("id")+",";  
  251.                 }   
  252.             });  
  253.         }else{  
  254.             $("[name='selId2']").each(function(){        
  255.                 if($(this).attr("checked")){  
  256.                     count++;  
  257.                     delIds=delIds+$(this).attr("id")+",";  
  258.                 }   
  259.             });  
  260.         }  
  261.         delIds=delIds.substring(0,delIds.length-1);  
  262.         if(count<1){  
  263.             alert("删除操作,请选择一个");  
  264.             return false;  
  265.         } else {  
  266.             if(confirm('确定要删除{'+delIds+'}记录吗?>')){  
  267.                 var url="";  
  268.                 if($(this).parent().parent().attr("id")=="td1_2")  
  269.                     url="/pay/production.do?command=delProduction&ids="+delIds;  
  270.                 else  
  271.                     url="/pay/productionProcess.do?command=delProductionProcess&ids="+delIds;  
  272.                 $.get(url,null,  
  273.                     function (date){  
  274.                         alert("删除ID为{"+delIds+"}记录成功");  
  275.                         //实现局部刷新  
  276.                         $("#text").load("/pay/production.do");  
  277.                 });  
  278.             } else {  
  279.                 //取消原来的选择  
  280.                 $("[name=cancel]").click();  
  281.                 return false;  
  282.             }  
  283.         }     
  284.     });  
  285.       
  286.     //全选  
  287.     $("[name=selectAll]").click(  
  288.         function(){  
  289.             if($(this).parent().attr("id")=="td1_1")  
  290.                 $("[name='selId1']").attr("checked",'true');  
  291.             if($(this).parent().attr("id")=="td2_1")  
  292.                 $("[name='selId2']").attr("checked",'true');  
  293.     });  
  294.     //取消全选  
  295.     $("[name=cancel]").click(  
  296.         function(){  
  297.             if($(this).parent().attr("id")=="td1_1")  
  298.                 $("[name='selId1']").removeAttr("checked");  
  299.             if($(this).parent().attr("id")=="td2_1")  
  300.                 $("[name='selId2']").removeAttr("checked");  
  301.     });  
  302.     //反选  
  303.     $("[name=inverse]").click(  
  304.         function(){  
  305.             if($(this).parent().attr("id")=="td1_1")  
  306.                 $("[name='selId1']").each(function(){  
  307.                     this.checked = !this.checked;   
  308.                 });  
  309.             if($(this).parent().attr("id")=="td2_1")  
  310.                 $("[name='selId2']").each(function(){  
  311.                     this.checked = !this.checked;   
  312.                 });  
  313.     });  
  314.       
  315. });  
  316.   
  317. function openWin(url,name,w,h,s){  
  318.     sb = s == "1" ? "1" : "0";  
  319.     l = (screen.width - w)/2;  
  320.     t = (screen.height - h)/2;  
  321.     sFeatures = "left="+ l +",top="+ t +",height="+ h +",width="+ w  
  322.             + ",center=1,scrollbars=" + sb + ",status=0,directories=0,channelmode=0";  
  323.     openwin = window.open(url , name , sFeatures );  
  324.     if (!openwin.opener)  
  325.         openwin.opener = self;  
  326.     openwin.focus();  
  327.     return openwin;  
  328. }  
  329. </script>  
  330.     <table id="myTable1" border="0" cellpadding="0" cellspacing="1" width="46%" style="font-family:arial;background-color: #C8D7E3;margin:3px 15px 3px 4px;font-size: 8pt;float:left;">  
  331.         <thead>  
  332.         <tr height="30px" style="font-size: 8pt;padding: 4px;border: 1px solid #C7DB51;background-color: #9FC9E5;">  
  333.             <th></th>  
  334.             <th>I D值</th>  
  335.             <th>编号</th>  
  336.             <th>名称</th>  
  337.             <th>单位</th>  
  338.             <th>产品类别</th>  
  339.         </tr>  
  340.         </thead>  
  341.         <tbody id="inBody1">  
  342.         <c:forEach items="${list }" var="list">  
  343.         <tr id="${list.id }" height="30px" style="background-color: white;border: 1px solid #C7DB51;font-size: 8pt;padding: 4px;">        
  344.             <td><input name="selId1" type="checkbox" id=${list.id } ></td>  
  345.             <td>${list.id } </td>  
  346.             <td>${list.num } </td>  
  347.             <td>${list.name }</td>  
  348.             <td>${list.unit }</td>  
  349.             <td>${list.productionCategory.name }</td>  
  350.               
  351.         </tr>  
  352.         </c:forEach>  
  353.         </tbody>  
  354.         <tr bgcolor="white" height="30px">  
  355.             <td colspan="4" width="50%" id="td1_1"><a name="selectAll" href="#">全选</a><a name="cancel" href="#">重置</a><a name="inverse" href="#">反选</a></td>  
  356.             <td colspan="2" width="50%" id="td1_2"><div align="right"><a id="updateProduction" href="#" >更新</a><a name="del" href="#" >删除</a></div></td>  
  357.         </tr>  
  358.         <tr bgcolor="#ECE9D8" height="30px">  
  359.             <td colspan="6" width="100%" style="text-align:center;font-size: 15pt;color:#6BC629;">增加产品</td>  
  360.             <td><input type="hidden" id="P_id" name="P_id"/></td>  
  361.         </tr>  
  362.         <tr bgcolor="#ECE9D8">  
  363.             <td colspan="3" width="30%">产品类别:</td>  
  364.             <td colspan="3" width="70%"> <select id="productionCategory_id" name="productionCategory_id">   
  365.                 <option disabled="disabled">ID    类别</option>  
  366.             </select>  
  367.             <a id="addProductionCategory" href="#">添加产品类别</a></td>  
  368.         </tr>  
  369.         <tr bgcolor="#ECE9D8">  
  370.             <td colspan="3">产品编号:</td>  
  371.             <td colspan="3"><input name="productionNum" id="productionNum" type="text"></td>  
  372.         </tr>  
  373.         <tr bgcolor="#ECE9D8">  
  374.             <td colspan="3">产品名称:</td>  
  375.             <td colspan="3"><input name="name" id="name" type="text"></td>  
  376.         </tr>  
  377.         <tr bgcolor="#ECE9D8">  
  378.             <td colspan="3">计量单位:</td>  
  379.             <td colspan="3"><input name="unit" id="unit" type="text"></td>  
  380.         </tr>  
  381.         <tr bgcolor="#ECE9D8">  
  382.             <td colspan="3"></td>  
  383.             <td colspan="3"><input type="button" id="addProductionBut" size="20" name="addProductionBut" value="添加产品"/></td>  
  384.         </tr>  
  385.     </table>  
  386.     <table id="myTable2" border="0" cellpadding="0" cellspacing="1" width="50%" style="font-family:arial;background-color: #C8D7E3;margin:3px 0pt 3px;font-size: 8pt;">  
  387.         <thead>  
  388.         <tr height="30px" style="font-size: 8pt;padding: 4px;border: 1px solid #C7DB51;background-color: #9FC9E5;">  
  389.             <th></th>  
  390.             <th>I D值</th>  
  391.             <th>工序编号</th>  
  392.             <th>工序名称</th>  
  393.             <th>工序单价</th>  
  394.             <th>废品扣费</th>  
  395.             <th>所属产品</th>  
  396.               
  397.         </tr>  
  398.         </thead>  
  399.         <tbody id="inBody2">  
  400.               
  401.         </tbody>  
  402.         <tr bgcolor="white" height="30px">  
  403.             <td colspan="4" width="50%" id="td2_1"><a name="selectAll" href="#">全选</a><a name="cancel" href="#">重置</a><a name="inverse" href="#">反选</a></td>  
  404.             <td colspan="3" width="50%" id="td2_2"><div align="right"><a id="updateProductionProcess" href="#" >更新</a><a name="del" href="#" >删除</a></div></td>  
  405.         </tr>  
  406.         <tr bgcolor="#ECE9D8" height="30px">  
  407.             <td colspan="7" width="100%" style="text-align:center;font-size: 15pt;color:#6BC629;">添加所选产品的工序</td>  
  408.             <td><input type="hidden" id="production_id" name="production_id"/><input type="hidden" id="id" name="id"/></td>  
  409.         </tr>  
  410.         <tr bgcolor="#ECE9D8">  
  411.             <td colspan="3" width="30%">选择工序:</td>  
  412.             <td colspan="4" width="70%"><select id="process_id" name="process_id">   
  413.                     <option disabled="disabled">ID    编号    工序</option>  
  414.                 </select>  
  415.             <a id="addProcess" href="#">添加一个工序</a> </td>  
  416.         </tr>  
  417.         <tr bgcolor="#ECE9D8">  
  418.             <td colspan="3">工序编号:</td>  
  419.             <td colspan="4"><input type="text" size="40" id="num" name="num"/></td>  
  420.         </tr>  
  421.         <tr bgcolor="#ECE9D8">  
  422.             <td colspan="3">工序单价:</td>  
  423.             <td colspan="4"><input type="text" size="40" id="price" name="price"/></td>  
  424.         </tr>  
  425.         <tr bgcolor="#ECE9D8">  
  426.             <td colspan="3">废品扣费:</td>  
  427.             <td colspan="4"><input type="text" size="40" id="waste" name="waste"/></td>  
  428.         </tr>  
  429.         <tr bgcolor="#ECE9D8">  
  430.             <td colspan="3"></td>  
  431.             <td colspan="4"><input type="button" id="addProcessBut" size="20" name="addProcessBut" value="添加产品工序"/></td>  
  432.         </tr>  
  433.     </table>  
  434. </body>  
  435. </html>  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值