📄 init.js
字号:
//页面初始加载,从数据库中取出数据
window.onload=function(){
Salary_infoDAOForDWR.queryAllSalary_info(init);
// alert("rowSpan ");
};
//页面初始化,把数据显示出来
//var buttonNum=0;
function init(Salary_info){
var tbody=document.getElementById("rows");
// tbody.removeChild(tbody.firstChild);
removeAllChildren(tbody);
var tdCount=8;
var i=0;
while(i<Salary_info.length){
var tr=document.createElement("tr");
//创建td
var td=new Array(tdCount);
for(var index=0;index<tdCount;index=index+1){
td[index]=document.createElement("td");
}
//创建span以及给它赋值
var newSpans=new Array(tdCount);
for(index=0;index<tdCount;index=index+1){
newSpans[index]=document.createElement("span");
}
var value0=document.createTextNode(i+1);
var value1=document.createTextNode(Salary_info[i].emp_name);
var value2=document.createTextNode(Salary_info[i].month);
var value3=document.createTextNode(Salary_info[i].basic_sal);
var value4=document.createTextNode(Salary_info[i].jintie);
var value5=document.createTextNode(Salary_info[i].bonus);
var value6=document.createTextNode(Salary_info[i].tax_rate);
var value7=document.createTextNode(Salary_info[i].sum_sal);
newSpans[0].appendChild(value0);
newSpans[1].appendChild(value1);
newSpans[2].appendChild(value2);
newSpans[3].appendChild(value3);
newSpans[4].appendChild(value4);
newSpans[5].appendChild(value5);
newSpans[6].appendChild(value6);
newSpans[7].appendChild(value7);
//td添加span/input
for(index=0;index<tdCount;index=index+1){
td[index].appendChild(newSpans[index]);
}
//tr添加td
for(index=0;index<tdCount;index=index+1){
tr.appendChild(td[index]);
}
tbody.appendChild(tr);
i=i+1;
}//while
}
//点击“添加”按钮后,添加一行输入文本框
function addNewRow(tdCount){
if(hasCreated()||hasEdit){
alert("请先保存您输入的记录!");
}else{
hasEdit=true;
var tbody=document.getElementById("rows");
var tr=document.createElement("tr");
//在编号栏添加一个空的td
var value=document.createTextNode("");
var newSpan=document.createElement("span");
newSpan.appendChild(value);
var td=document.createElement("td");
td.appendChild(newSpan);
tr.appendChild(td);
//创建输入文本框、并添加进td里边
for(index=0;index<tdCount;index=index+1){
var newInput=document.createElement("input");
newInput.type="text";
newInput.value="";
newInput.size="5";
newInput.id="text"+index;
var td=document.createElement("td");
td.appendChild(newInput);
tr.appendChild(td);
}
//创建”保存“和”取消“按钮
newInput=document.createElement("input");
newInput.type="button";
newInput.value="保存";
newInput.onclick=function(){
var datas=new Array();
for(index=0;index<tdCount;index=index+1){
var text=document.getElementById("text"+index);
datas[index]=text.value;
}
if(datas[0]==""){
alert("请输入姓名!");
}else{
saveRow(datas,tdCount);
}
};
td.appendChild(newInput);
tr.appendChild(td);
newInput=document.createElement("input");
newInput.type="button";
newInput.value="取消";
newInput.onclick=function(){
cancelRow();
hasEdit=false;
};
td.appendChild(newInput);
tr.appendChild(td);
tbody.appendChild(tr);
}//if
}//addNewRow
//编辑改行的值
var hasEdit=false;
function editRow(tdCount){
if(hasEdit){
alert("请先保存您输入的记录!");
}else{
//取得选择的那一行
var rowId=document.getElementById("rowId");
var tbody=document.getElementById("rows");
if(rowId.value>0){
var child=tbody.firstChild;
}else{
alert("请输入要操作的编号");
}
for(var index=0;index<rowId.value-1;index=index+1){
child=child.nextSibling;
}
var thisRow=child;
//存储数据
var datas=new Array();
//得到第一列
var td=thisRow.firstChild.nextSibling;
for(index=0;index<tdCount;index=index+1){
//把每一列的值放到数组里边
datas[index]=td.firstChild.firstChild.nodeValue;
//把原来的列删掉,换成文本输入框,并把原来的数据放入文本输入框里
td.removeChild(td.firstChild);
var newInput=document.createElement("input");
newInput.id="text"+index;
newInput.type="text";
newInput.size="5";
newInput.value=datas[index];
td.appendChild(newInput);
td=td.nextSibling;
}
//主键不能更改
thisRow.firstChild.nextSibling.firstChild.readOnly="true";
//创建”编辑“和”取消“按钮
newInput=document.createElement("input");
newInput.type="button";
newInput.value="保存";
newInput.onclick=function(){
//重新获取更改后的值
for(index=0;index<tdCount;index=index+1){
var text=document.getElementById("text"+index);
datas[index]=text.value;
}
//更新数据
updateRow(datas,tdCount);
};
td=document.createElement("td");
td.appendChild(newInput);
thisRow.appendChild(td);
//创建”取消“按钮
newInput=document.createElement("input");
newInput.type="button";
newInput.value="取消";
newInput.onclick=function(){
//取消编辑
cancelEdit();
hasEdit=false;
};
td.appendChild(newInput);
thisRow.appendChild(td);
hasEdit=true;
}//if-else
}
//保存输入的值
function saveRow(datas,tdCount){
Salary_infoDAOForDWR.createSalary_info(datas,confirm);
}
//保存编辑后的数据
function updateRow(datas,tdCount){
Salary_infoDAOForDWR.updateSalary_info(datas,confirm);
}
//删除选择的行
function deleteRow(){
//取得选择的那一行
var rowId=document.getElementById("rowId");
var tbody=document.getElementById("rows");
if(rowId.value>0){
var child=tbody.firstChild;
}else{
alert("请输入要操作的编号");
}
for(var index=0;index<rowId.value-1;index=index+1){
child=child.nextSibling;
}
var thisRow=child;
var emp_name=thisRow.firstChild.nextSibling.firstChild.firstChild.nodeValue;
Salary_infoDAOForDWR.deleteSalary_info(emp_name,confirm);
}
//取消添加行
function cancelRow(){
var tbody=document.getElementById("rows");
tbody.removeChild(tbody.lastChild);
}
//取消编辑
function cancelEdit(){
Salary_infoDAOForDWR.queryAllSalary_info(init);
}
//判断是否已经点击了“添加”的按钮,如果还没保存就不让继续添加。
function hasCreated(){
var tbody=document.getElementById("rows");
var tagName=tbody.lastChild.firstChild.firstChild.nodeName;
if(tagName.toLowerCase()=="input"){
return true;
}else{
return false;
}
}
//判断是否插入(保存)成功
function confirm(okOrNot){
if(okOrNot==true){
alert("操作已成功!");
hasEdit=false;
var tbody=document.getElementById("rows");
removeAllChildren(tbody);
Salary_infoDAOForDWR.queryAllSalary_info(init);
}else{
alert("操作失败!");
}
}
//移去所有孩子结点
function removeAllChildren(parent){
while (parent.hasChildNodes()){
parent.removeChild(parent.firstChild);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -