序号 | 姓名 | 操作 |
---|
1 | zs | 行添加 | 行删除 |
2 | ls | 行添加 | 行删除 |
3 | ww | 行添加 | 行删除 |
<script>
$(function(){
//加载选中行变色方法
loadCheckColor();
})
//选中行变色
function loadCheckColor() {
$("#dictTbl tbody tr").mousedown(function () {
$('#dictTbl tr').each(function () {
$(this).removeClass("checkColor");
});
$(this).addClass("checkColor");
var tr = $(this)[0];
var rowIndex = tr.rowIndex;
//赋值当前选中行号
$("#rowIndex").val(rowIndex);
});
}
//行添加
function insertRows() {
//当前选中行号
var currentRowIndex = $("#rowIndex").val();
var tbody = $("#tbody"+ currentRowIndex);
var tbodyHtml = "<tbody><tr><td></td><td></td><td><a>行添加</a> | <a>行删除</a></td></tr></tbody>";
var tr = $(tbodyHtml);
tbody.after(tr);//拼接到选中行之后
//加载选中行变色方法
loadCheckColor();
//遍历table,更改序号
$("tbody tr").each(function(){
var tr = $(this)[0];
var rowIndex = tr.rowIndex;
$(this).parent().attr("id","tbody"+rowIndex);
$(this).find("td").eq(0).text(rowIndex);//改已有数据的序号
});
}
//行删除
function delTableRow() {
//当前选中行号
var currentRowIndex = $("#rowIndex").val();
var tbody = $("#tbody"+ currentRowIndex);
tbody.remove();
//移除行后,给行号设置为0
$("#rowIndex").val(0);
//加载选中行变色方法
loadCheckColor();
//遍历table,更改序号
$("tbody tr").each(function(){
var tr = $(this)[0];
var rowIndex = tr.rowIndex;
$(this).parent().attr("id","tbody"+rowIndex);
$(this).find("td").eq(0).text(rowIndex);//改已有数据的序号
});
}
$("#additem").click(function () {
var rowIndex = $("#rowIndex").val();
if(rowIndex == 0){
alert("请先选中一行。。。");
return;
}
//console.log("当前选中行号 -> " + rowIndex);
insertRows();
})
$("#deleteitem").click(function () {
var rowIndex = $("#rowIndex").val();
if(rowIndex == 0){
alert("请先选中一行。。。");
return;
}
delTableRow();
})
$("#reset").click(function() {
window.location.reload();
})
</script>
</body>