JS :点击按钮table增加一行,删除一行

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>update_data.jsp</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<!--
	<link rel="stylesheet" href="<%=path%>/css/data_text.css" type="text/css" />
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
		//给table增加一行   
		function addTableRow() {   
		    var table1 = document.getElementById('table1');   
		    var cloneTab = document.getElementById('table2');   
		    //alert(cloneTab.firstChild.firstChild.innerHTML);   
		    //alert(cloneTab.firstChild.firstChild.cloneNode(true).innerHTML);   
		    table1.firstChild.appendChild(cloneTab.firstChild.firstChild.cloneNode(true));   
		  
		    var v= table1.firstChild.childNodes;   
		    var len = v.length;   
		    for(var i=1;i<len;i++){   
		        v[i].childNodes[0].firstChild.id=i;//给第一个单元格id赋值   
		    }   
		}   
		  
		//给table删除一行   
		function delTableRow(){   
		    var table1 = document.getElementById('table1');   
		    var isChecked = document.getElementsByName('isChecked');   
		    var len = isChecked.length;   
		    for(var i=len-1;i>=0;i--){   
		        if(isChecked[i].checked==true){   
		             table1.firstChild.removeChild(isChecked[i].parentNode.parentNode);   
		            //alert(isChecked[i].id);   
		            //alert(isChecked[i].parentNode.parentNode.innerHTML);   
		        }   
		    }   
		}
	</script>
  </head>
  
  <body>
  	<table border="0" cellpadding="0" cellspacing="0" class="datalist" id="table1">   
    <tr>   
      <th width="38" nowrap="nowrap" style="width: 5%">0</th>   
      <th width="38" nowrap="nowrap" >00</th>   
      <th width="79" nowrap="nowrap" scope="col">1</th>   
      <th width="70" nowrap="nowrap" scope="col">2</th>   
      <th width="69" nowrap="nowrap" scope="col">3</th>   
      <th width="66" nowrap="nowrap" scope="col">4</th>   
      <th width="94" nowrap="nowrap" scope="col">5</th>   
      <th width="107" nowrap="nowrap" scope="col">6</th>   
    </tr>   
</table>   
  
<!--控制table的按钮-->   
  <table>   
      <tr align="center">   
        <td colspan="10">   
          <input type="button"  value="增加" οnclick= "addTableRow();"/>    
          <input type="button"  value="删除" οnclick="delTableRow();"/>   
        </td>   
     </tr>   
  </table>   
  
<!--模板table也叫做clone table style = "display:none"-->   
  <table id='table2' style="display: none">   
  <tr>   
    <th><input type="checkbox" name="isChecked" /><input type="hidden" size="6" value=""/></th>   
    <th width="38" nowrap="nowrap" style="width: 5%"><input type="text" size="16" maxlength="50" value=""/></th>   
    <th width="79" nowrap="nowrap" scope="col"><input type="text"  size="16" maxlength="50" value=""/></th>   
    <th width="70" nowrap="nowrap" scope="col"><input type="text"  size="6" maxlength="10" value=""/></th>   
    <th width="69" nowrap="nowrap" scope="col">   
          <select size="1">   
              <option value="">请选择...</option>   
              <option value="1">1</option>   
              <option value="2">1</option>   
         </select>   
        </th>   
    <td width="100" nowrap="nowrap" scope="col"><input type="text"  class="date150"/></td>   
    <th width="94" nowrap="nowrap"  scope="col"><input type="text"  size="16" maxlength="50" value=""/></th>   
    <th width="71" nowrap="nowrap"  scope="col"><input type="text"  size="16" maxlength="50" value=""/></th>   
  </tr>   
  </table>  
  	
  </body>
</html>





`el-table` 是 Element UI 中的一个表格组件,用于展示数据列表,并提供了丰富的功能和交互选项。如果你想在点击按钮后向表格中动态添加一行,你可以按照以下步骤操作: 1. 首先,在 Vue 项目中引入 `element-ui` 并注册 `el-table` 组件。 2. 定义一个数组来存储表格的数据,初始时可以为空或包含一些默认值。 ```html <template> <div> <button @click="addRow">新增一行</button> <el-table :data="tableData" ref="table"> <!-- 表格列定义 --> <el-table-column prop="name" label="姓名"></el-table-column> <!-- 其他列... --> </el-table> </div> </template> <script> export default { data() { return { tableData: [] // 初始数据数组 }; }, methods: { addRow() { const newRow = { name: '新行', // 根据实际需求填充属性 } this.tableData.push(newRow); // 向数组末尾添加新行 // 如果需要更新视图,也可以调用 this.$refs.table.updateTableData(this.tableData); } } }; </script> ``` 3. 在 `methods` 对象中创建 `addRow` 方法,每当点击按钮时,会触发这个方法,生成新的数据行并将其添加到 `tableData` 数组里。 4. 可能的话,还可以为表格配置一个事件监听器,比如 `@row-add`,这样可以在新行添加后执行某些操作,但这是可选的。 5. 更新表格数据时,可以使用 `this.$refs.table` 的 `updateTableData` 方法(如果有的话),否则直接操作数据数组即可,Vue 会在下一次渲染时自动更新视图。 相关问题--: 1. 如何在 Vue 中使用 `ref` 关联 DOM 元素? 2. 如何在 Vue 中响应式地更新表格数据? 3. `el-table` 提供哪些内置的表单事件?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值