JS實(shí)現(xiàn)動態(tài)表格的添加,修改,刪除功能(推薦)
1. 首先在頁面中配置好一個表格框架
<tr> <td>新增參數(shù):</td> <td class="pn-fcontent"><input type="button" value="選擇" onclick="openAppParamsPage();"/></td> <td>參數(shù)列表:</td> <td class="pn-fcontent"><input type="hidden" id="paramslist" name="paramslist" style="width:190%" height="500"/></td> </tr> <tr> <table id="tab" width="100%" cellspacing="1" cellpadding="0" border="0" style="" class="pn-ltable"> <tr> <td height="20" valign="top" align="center"> 參數(shù)名稱: </td> <td height="20" valign="top" align="center"> 參數(shù)編碼: </td> <td height="20" valign="top" align="center"> 參數(shù)值: </td> <td id="pos_1" height="20"> 操作 </td> </tr> <tbody id="sortList"></tbody> </table> </tr> <tr> <td align="center" colspan="4"> <input type="submit" class="btn" value="保存" onclick="setParamslist();"/> <input type="button" class="btn" value="返回"/> </td> </tr>
2. 相關(guān)JS函數(shù)
function setParamslist() {
var tab = document.getElementById("tab");
//表格行數(shù)
var rows = tab.rows.length ;
//表格列數(shù)
var cells = tab.rows.item(0).cells.length ;
//alert("行數(shù)"+rows+"列數(shù)"+cells);
var rowData = "";
for(var i=1;i<rows;i++) {
var cellsData = new Array();
for(var j=0;j<cells-1;j++) {
cellsData.push(tab.rows[i].cells[j].innerText);
}
rowData = rowData + "|" + cellsData;
}
document.getElementById("paramslist").value = rowData;
}
//打開相關(guān)新增應(yīng)用參數(shù)界面
function openAppParamsPage() {
var param = new Object();
//這個參數(shù)一定要傳。
param.win = window;
param.id = 100;
param.name = "test";
param.birthday = new Date();
var result = window.showModalDialog("addParamsItem","dialogWidth:500px;dialogHeight:600px;dialogLeft:200px;dialogTop=200px");
//var temp = document.getElementById("paramslist").value;
//document.getElementById("paramslist").value = temp + result;
addSort(result);
}
// 增加應(yīng)用參數(shù)函數(shù)
function addSort(data) {
var name = data;
if(name == ""||name==undefined ) {
return;
}
console.log(data);
var params = data.split(",");
var paramName = params[0];
var paramCode = params[1];
var paramValue = params[2];
var row = document.createElement("tr");
row.setAttribute("id", paramCode);
var cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramName));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramCode));
row.appendChild(cell);
cell = document.createElement("td");
cell.appendChild(document.createTextNode(paramValue));
row.appendChild(cell);
var deleteButton = document.createElement("input");
deleteButton.setAttribute("type", "button");
deleteButton.setAttribute("value", "刪除");
deleteButton.onclick = function () { deleteSort(paramCode); };
cell = document.createElement("td");
cell.appendChild(deleteButton);
row.appendChild(cell);
document.getElementById("sortList").appendChild(row);
}
// 刪除應(yīng)用參數(shù)函數(shù)
function deleteSort(id) {
if (id!=null){
var rowToDelete = document.getElementById(id);
var sortList = document.getElementById("sortList");
sortList.removeChild(rowToDelete);
}
}
附加表格的修改函數(shù)
//彈出更新界面相關(guān)信息
function updateSort(id) {
if (id!=null){
var row = document.getElementById(id);
//alert("row is " + row.cells[0].innerHTML);
var id = row.cells[0].innerHTML;
var paramName = row.cells[1].innerHTML;
var paramCode = row.cells[2].innerHTML;
var paramValue = row.cells[3].innerHTML;
var param = new Object();
//這個參數(shù)一定要傳。
param.win = window;
param.id = 100;
param.name = "test";
param.birthday = new Date();
var result = window.showModalDialog(baseUrl + "app/updateParamsItem?id=" + id + "¶mName=" + paramName + "¶mCode=" + paramCode + "¶mValue=" + paramValue,
"dialogWidth:500px;dialogHeight:600px;dialogLeft:200px;dialogTop=200px");
var arr = result.split(",");
row.cells[0].innerHTML = arr[0];
row.cells[1].innerHTML = arr[1];
row.cells[2].innerHTML = arr[2];
row.cells[3].innerHTML = arr[3];
}
}
3. 彈出框頁面,新增或者修改參數(shù),并回寫相關(guān)數(shù)據(jù)。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>新增應(yīng)用</title>
<#include "/views/head.html"/>
</head>
<body>
<div class="body-box">
<div class="clear"></div>
<form >
<table width="100%" cellspacing="1" cellpadding="2" border="0" class="pn-ftable">
<tr>
<td>參數(shù)名稱:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramName" name="paramName"/></td>
</tr>
<tr>
<td>參數(shù)編碼:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramCode" name="paramCode" required="true" /></td>
</tr>
<tr>
<td>參數(shù)值:</td>
<td class="pn-fcontent"><input type="text" maxlength="20" class="" required="true" id="paramValue" name="paramValue" required="true" /></td>
</tr>
<tr>
<td align="center" colspan="4">
<input type="submit" value="保存" onclick="returnResult();"/>
<input type="button" value="返回" onclick="closeWindow();"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
<script type="text/javascript">
//直接關(guān)閉窗口
function closeWindow() {
window.close();
}
//獲取值,組裝后返回
function returnResult() {
if(!$('form').valid())
return;
var paramName = document.getElementById("paramName");
var paramCode = document.getElementById("paramCode");
var paramValue = document.getElementById("paramValue");
//alert("value is " + paramName.value + "," + paramCode.value + "," + paramValue.value);
var result = paramName.value + "," + paramCode.value + "," + paramValue.value;
window.returnValue = result;
window.close();
}
</script>
以上所述是小編給大家介紹的JS實(shí)現(xiàn)動態(tài)表格的添加,修改,刪除功能(推薦)全部敘述,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
javascript判斷ie瀏覽器6/7版本加載不同樣式表的實(shí)現(xiàn)代碼
ie6/ie7的兼容問題很讓人苦惱,我們可以針對這兩個版本的瀏覽器單獨(dú)寫?yīng)毩⒌臉邮奖恚瑏斫鉀Q兼容問題。這里的例子以判斷ie6與ie7來加載不同的樣式表2011-12-12
JS實(shí)現(xiàn)分頁瀏覽橫向圖片(類輪播)實(shí)例代碼
這篇文章主要介紹了JS實(shí)現(xiàn)分頁瀏覽橫向圖片(類輪播)實(shí)例代碼,代碼簡單易懂,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
JavaScript的setAttribute兼容性問題解決方法
JavaScript的setAttribute存在兼容性問題,下面與大家分享下具體的解決方法,感興趣的朋友可以參考下2013-11-11
Gulp實(shí)現(xiàn)靜態(tài)網(wǎng)頁模塊化的方法詳解
眾所周知Gulp.js 是一個自動化構(gòu)建工具,開發(fā)者可以使用它在項(xiàng)目開發(fā)過程中自動執(zhí)行常見任務(wù)。下面這篇文章主要給大家介紹了關(guān)于Gulp實(shí)現(xiàn)靜態(tài)網(wǎng)頁模塊化的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2018-01-01
關(guān)于Webpack dev server熱加載失敗的解決方法
下面小編就為大家分享一篇關(guān)于Webpack dev server熱加載失敗的解決方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02
DropDownList控件綁定數(shù)據(jù)源的三種方法
本文給大家分享web 中 DropDownList綁定數(shù)據(jù)源的幾種方式以及DropdownList控件動態(tài)綁定數(shù)據(jù)源的兩種方法,下面通過本文給大家詳細(xì)介紹,感興趣的朋友一起看看2016-12-12

