对表格的一系列操作(增删改)

时间:2021-01-15 23:34:05
<script type="text/javascript" src="/ks4/js/jquery-1.2.6.js"></script>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<script type='text/javascript' src='/ks4/dwr/interface/myjs.js'></script>
<script type='text/javascript' src='/ks4/dwr/engine.js'></script>
<script type='text/javascript' src='/ks4/dwr/util.js'></script>
<style>
<!--
 .tt{border:0}
-->
</style>
<h2 align="center">销售出库单</h2>
<table border=0 align=center style="text-align: center;width: 50%;">
 <tr>
  <td style="text-align: right;">单&nbsp;&nbsp;&nbsp;号:</td>
  <td><input type="text" name="txtNum" id="txtNum"></td>
  <td style="text-align: right;">日&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;期:</td>
  <td><input type="text" name="txtDate" id="txtDate"></td>
 </tr>
 <tr>
  <td style="text-align: right;">客&nbsp;&nbsp;&nbsp;户:</td>
  <td><input type="text" name="txtGuest" id="txtGuest"></td>
  <td style="text-align: right;">业务员:</td>
  <td><input type="text" name="txtSale" id="txtSale"></td>
 </tr>
</table><br>
<div><input type="button" value="明细" style="width: 60;margin-left: 310" onclick="dispaly(this)">
<input type="button" value="备注" style="width: 60" onclick="dispaly(this)"></div>
<div id="detail" style="display: block">
 <table border=1 cellpadding="0" cellspacing="0" style="text-align: center;width: 50%" align=center>
  <thead>
   <tr>
    <th>序号</th>
    <th>商品名称</th>
    <th>数量</th>
    <th>单价</th>
    <th>金额</th>
    <th>操作</th>
   </tr>
  </thead>
  <tbody id="mybody">
   <tr>
    <td><input type="text" name="txtNo" size=2 readonly="readonly" class="tt" value=1></td>
    <td><input type="text" name="txtName" size=4 class="tt"></td>
    <td><input type="text" name="txtNum" size=2 class="tt" onkeyup="total(this)"></td>
    <td><input type="text" name="txtPrice" size=2 class="tt"></td>
    <td><input type="text" name="txtTotal" size=2 class="tt"></td>
    <td><a href="javascript:addRow()">添加</a>|<a href="javascript:void(0)" onclick="delRow(this)">删除</a></td>
   </tr>
  </tbody>
  <tfoot>
  <tr><td colspan="6" style="text-align: left">
   <input type="button" value="保存" onclick="save(this)">
   <input type="button" value="关闭" onclick="window.close()">
  </td></tr>
  </tfoot>
 </table>
</div>
<div id="remark" style="text-align: center;display: none">
 <textarea style="width: 50%;" rows="6" id="txtRemark"></textarea>
</div>
<script>
 dispaly=function(btn){
  if(btn.value=="明细"){
   document.getElementById("detail").style.display="block";
   document.getElementById("remark").style.display="none";
  }
  if(btn.value=="备注"){
   document.getElementById("remark").style.display="block";
   document.getElementById("detail").style.display="none";
  }
 }
 addRow=function(){
  var tr=jQuery("#mybody>tr:first").clone();
  tr.find("input[@name=txtName]").val("");
  tr.find("input[@name=txtNum]").val("");
  tr.find("input[@name=txtPrice]").val("");
  tr.find("input[@name=txtTotal]").val("");
  tr.appendTo(jQuery("#mybody"));
  initRow();
 }
 
 delRow=function(a){
  jQuery(a).parent().parent().remove();
  initRow();
 }
 
 initRow=function(){
  var tab=document.getElementById("mybody");
  for(var i=0;i<tab.rows.length;i++){
   tab.rows[i].cells[0].firstChild.value=i+1;
  }
 }
 
 total=function(txt){
   var num=jQuery(txt).val();
   if(isNaN(num)){
    alert("必须是数字!");
    return;
   }
   if(num<=0){
    alert("不能为负!");
    return;
   }
   var price=txt.parentNode.parentNode.cells[3].firstChild.value;
   txt.parentNode.parentNode.cells[4].firstChild.value=num*price;
 }
 
 save=function(btn){
  var no=jQuery("#txtNum").val();
  var date=jQuery("#txtDate").val();
  var dt1=date.replace(/-/ig,"/");
  var dt=new Date(dt1);
  var guest=jQuery("#txtGuest").val();
  var sale=jQuery("#txtSale").val();
  var remark=jQuery("#txtRemark").val();
  var json={FSaleformNo:no,FSaleformDate:dt,FCustomer:guest,FSales:sale,FRemark:remark};
  alert(json);
  var tab=document.getElementById("mybody");
  var array=[];
  for(var i=0;i<tab.rows.length;i++){
   var lineid=tab.rows[i].cells[0].firstChild.value;
   alert(lineid);
   var goodsname=tab.rows[i].cells[1].firstChild.value;
   var goodsnum=tab.rows[i].cells[2].firstChild.value;
   var goodsprice=tab.rows[i].cells[3].firstChild.value;
   var goodstotal=tab.rows[i].cells[4].firstChild.value;
   alert(goodstotal);
   var jsons={id:{TSaleform:json,FLineId:lineid},FGoods:goodsname,FQty:goodsnum,FPrice:goodsprice,FMoneyAmt:goodstotal};
   alert(jsons);
   array.push(jsons);
  }
  if(btn.value=="保存"){
   myjs.add(json,array,function(){});
   btn.value="修改";
  }else{
   myjs.update(json,array,function(){});
  }
  
 }
 
 onload=function(){
  myjs.getId(function(x){
   jQuery("#txtNum").val(x);
  });
 }
</script>