今天实现ROOT层
root.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>管理员页面</title> <style> .form { width: 600px; margin: 0 auto; /*border: 1px solid red;*/ } .form table { margin: 0 auto; } .form table tr td { width: 100px; height: 30px; border: 1px solid #000; text-align: center; } button { display: block; margin-top: 10px; padding: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 3px; cursor: pointer; } </style> </head> <body> <h1 style="text-align: center">管理员系统</h1> <script> // 获取URL中的用户名参数 var urlParams = new URLSearchParams(window.location.search); var username = urlParams.get('username'); console.log(username); </script> <div class="form"> <table border="1px" cellspacing="0" width="600px"> <tr> <th>编号</th> <th>功能</th> </tr> <tr> <td>1</td> <td> <button id="add2">新添员工</button> </td> </tr> <tr> <td>2</td> <td> <button id="update2">修改员工</button> </td> </tr> <tr> <td>3</td> <td> <button id="delete2">删除员工</button> </td> </tr> <tr> <td>4</td> <td> <button id="add1">新添部门</button> </td> </tr> <tr> <td>5</td> <td> <button id="update1">修改部门</button> </td> </tr> <tr> <td>6</td> <td> <button id="delete1">删除部门</button> </td> </tr> <tr> <td>7</td> <td> <button id="set">设置角色</button> </td> </tr> <tr> <td>8</td> <td> <button id="re">密码重置</button> </td> </tr> </table> </div> </body> <script> document.getElementById("add1").addEventListener("click", function () { window.location.href = "add2.html"; }); document.getElementById('update1').addEventListener('click', function () { window.location.href = "update2.html"; }); document.getElementById("delete1").addEventListener("click", function () { window.location.href = "delete2.html"; }); document.getElementById('add2').addEventListener('click', function () { window.location.href = "add1.html"; }); document.getElementById("update2").addEventListener("click", function () { window.location.href = "update1.html"; }); document.getElementById('delete2').addEventListener('click', function () { window.location.href = "delete1.html"; }); document.getElementById("set").addEventListener("click", function () { window.location.href = "set.html"; }); document.getElementById('re').addEventListener('click', function () { window.location.href = "re.html"; }); </script> </html>
add1.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>员工信息添加</title> <style> .reSet { background-color: #4CAF50; color: white; border: none; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .form { width: 600px; margin: 0 auto; /*border: 1px solid red;*/ } .form table { margin: 0 auto; } .form table tr td { width: 100px; height: 30px; border: 1px solid #000; text-align: center; } button { display: block; margin-top: 10px; padding: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 3px; cursor: pointer; } .centered-form { text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } .bordered-form { border: 2px solid #000; /* 边框样式 */ padding: 20px; /* 可选的内边距 */ background-color: #f0f0f0; /* 可选的背景颜色 */ } </style> </head> <script> function generatePaperNumber() { const id = "2019"; const requestUrl = "http://localhost:8080/user/count"; fetch(requestUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' }, }) .then(response => response.json()) .then(data => { if (data != null) { const count = data + 1; const jobID = id + padNumber(count, 4); document.getElementById('jobID').value = jobID; console.log(jobID); } else { alert("查询失败"); } }) .catch(error => { alert("请求失败,请重试"); console.error(error); }); } function padNumber(number, width) { const str = number.toString(); return str.length >= width ? str : new Array(width - str.length + 1).join('0') + str; } </script> <body> <h1 style="text-align: center">添加信息</h1> <div class="centered-form"> <div class="bordered-form"> <div class="form"> <form id="addForm"> <label for="jobID">员工ID:</label> <input type="text" id="jobID" name="jobID" readonly> <br> <br> <button type="button" style="display: block; margin: 0 auto;" onclick="generatePaperNumber()">生成编号 </button> <br> <label for="name">请输入姓名</label> <input type="text" id="name"> <br> <label>性别:</label> <div id="sex"> <label><input type="radio" name="sex" value="男">男</label> <label><input type="radio" name="sex" value="女">女</label> </div> <br> <label for="birthday">请输入出生日期</label> <input type="date" id="birthday"> <br> <div id="container"> </div> <label for="major">请输入所属部门</label> <input type="text" id="major"> <button type="submit" style="display: block; margin: 0 auto;">添加</button> </form> </div> </div> </div> </body> <script> display(); function display() { const requestUrl = 'http://localhost:8080/user/selectCount'; fetch(requestUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' }, }) .then(res => res.json()) .then(data => { if (data.msg === 'success') { generateTable(data.data); console.log(data); } else { alert("查询失败 " + data.msg); } }) .catch(error => { alert("请求失败,请重试"); console.error(error); }); } </script> <script> function generateTable(data) { const tableContainer = document.getElementById("container"); // 清空 tableContainer 中的所有子节点 while (tableContainer.hasChildNodes()) { tableContainer.removeChild(tableContainer.firstChild); } const table = document.createElement("table"); const tableBody = document.createElement("tbody"); let row = document.createElement("tr"); row.innerHTML = '<td>部门编号</td><td>部门名称</td>'; tableBody.appendChild(row); // 查询方式是按姓名查询或多条查询 for (let i = 0; i < data.length; i++) { row = document.createElement("tr"); row.innerHTML = `<td>${data[i].departmentID}</td><td>${data[i].departmentName}</td>`; tableBody.appendChild(row); table.appendChild(tableBody); tableContainer.appendChild(table); } } </script> <script> document.getElementById('addForm').addEventListener('submit', function (e) { e.preventDefault(); const jobID = document.getElementById('jobID'); const name = document.getElementById('name'); const sex = document.querySelectorAll('input[name="sex"]'); let s; sex.forEach(radio => { if (radio.checked) { s = radio.value; } }); const birthday = document.getElementById('birthday'); const major = document.getElementById('major'); console.log(jobID.value + " " + name.value + " " + s + " " + birthday.value + " " + major.value); addStaff(jobID.value, name.value, s, birthday.value, major.value); }); </script> <script> function addStaff(jobID, name, s, birthday, major) { const requestUrl = 'http://localhost:8080/user/addStaff'; fetch(requestUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ jobID: jobID, name: name, sex: s, birthday: birthday, department: major, role: "员工", password: "123456" }) }) .then(res => res.json()) .then(data => { if (data.msg === 'success') { alert("添加员工成功"); console.log(data); } else { alert("添加失败 " + data.msg); } }) .catch(error => { alert("请求失败,请重试"); console.error(error); }); } </script> </html>>
add2.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>新增部门信息</title> <style> .reSet { background-color: #4CAF50; color: white; border: none; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .form { width: 600px; margin: 0 auto; /*border: 1px solid red;*/ } .form table { margin: 0 auto; } .form table tr td { width: 100px; height: 30px; border: 1px solid #000; text-align: center; } button { display: block; margin-top: 10px; padding: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 3px; cursor: pointer; } .centered-form { text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } .bordered-form { border: 2px solid #000; /* 边框样式 */ padding: 20px; /* 可选的内边距 */ background-color: #f0f0f0; /* 可选的背景颜色 */ } </style> </head> <body> <h1 style="text-align: center">添加信息</h1> <div class="centered-form"> <div class="bordered-form"> <div class="form"> <form id="addForm"> <label for="jobID">部门编号:</label> <input type="text" id="jobID" name="jobID" minlength="2" maxlength="2"> <br> <label for="name">请输入名称</label> <input type="text" id="name"> <br> <div id="container"> </div> <button type="submit" style="display: block; margin: 0 auto;">添加</button> </form> </div> </div> </div> </body> <script> display(); function display() { const requestUrl = 'http://localhost:8080/user/selectCount'; fetch(requestUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' }, }) .then(res => res.json()) .then(data => { if (data.msg === 'success') { generateTable(data.data); console.log(data); } else { alert("查询失败 " + data.msg); } }) .catch(error => { alert("请求失败,请重试"); console.error(error); }); } </script> <script> function generateTable(data) { const tableContainer = document.getElementById("container"); // 清空 tableContainer 中的所有子节点 while (tableContainer.hasChildNodes()) { tableContainer.removeChild(tableContainer.firstChild); } const table = document.createElement("table"); const tableBody = document.createElement("tbody"); let row = document.createElement("tr"); row.innerHTML = '<td>部门编号</td><td>部门名称</td>'; tableBody.appendChild(row); // 查询方式是按姓名查询或多条查询 for (let i = 0; i < data.length; i++) { row = document.createElement("tr"); row.innerHTML = `<td>${data[i].departmentID}</td><td>${data[i].departmentName}</td>`; tableBody.appendChild(row); table.appendChild(tableBody); tableContainer.appendChild(table); } } </script> <script> document.getElementById('addForm').addEventListener('submit', function (e) { e.preventDefault(); const jobID = document.getElementById('jobID'); const name = document.getElementById('name'); console.log(jobID.value + " " + name.value); cDep(jobID.value, name.value); }); </script> <script> function cDep(id, name) { const requestUrl = `http://localhost:8080/user/aDep/${id}`; fetch(requestUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' }, }) .then(res => res.json()) .then(data => { if (data === 0) { dDep(id, name); } else { alert("已存在所填编号 "); } }) .catch(error => { alert("请求失败,请重试"); console.error(error); }); } </script> <script> function dDep(id, name) { const requestUrl = `http://localhost:8080/user/bDep/${name}`; fetch(requestUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' }, }) .then(res => res.json()) .then(data => { if (data === 0) { addDep(id, name); } else { alert("已存在所填名称 "); } }) .catch(error => { alert("请求失败,请重试"); console.error(error); }); } </script> <script> function addDep(jobID, name) { const requestUrl = 'http://localhost:8080/user/addDep'; fetch(requestUrl, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ departmentID: jobID, departmentName: name }) }) .then(res => res.json()) .then(data => { if (data.msg === 'success') { alert("添加部门成功"); console.log(data); } else { alert("添加失败 " + data.msg); } }) .catch(error => { alert("请求失败,请重试"); console.error(error); }); } </script> </html>>
delete1.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>删除员工</title> </head> <body> <div id="container"> </div> </body> <script> const requestUrl = 'http://localhost:8080/user/getStaff'; fetch(requestUrl, { method: 'GET', headers: { 'Content-Type': 'application/json' }, }) .then(res => res.json()) .then(data => { if (data.msg === 'success') { console.log(data); generateTable(data.data); } else { alert("查询失败 " + data.msg); } }) .catch(error => { alert("请求失败,请重试"); console.error(error); }); </script> <script> function generateTable(data) { const tableContainer = document.getElementById("container"); // 清空 tableContainer 中的所有子节点 while (tableContainer.hasChildNodes()) { tableContainer.removeChild(tableContainer.firstChild); } const table = document.createElement("table"); const tableBody = document.createElement("tbody"); let row = document.createElement("tr"); row.innerHTML = '<td>工号</td><td>姓名</td><td>性别</td><td>出生日期</td><td>部门</td><td>角色</td><td>密码</td><td>是否删除</td>'; tableBody.appendChild(row); for (let i = 0; i < data.length; i++) { row = document.createElement("tr"); row.innerHTML = `<td>${data[i].jobID}</td><td>${data[i].name}</td><td>${data[i].sex}</td><td>${data[i].birthday}</td><td>${data[i].department}</td><td>${data[i].role}</td><td>${data[i].password}</td><td><button class="deleteButton">删除</button></td>`; tableBody.appendChild(row); table.appendChild(tableBody); tableContainer.appendChild(table); } } </script> <script> const tableContainer = document.getElementById("container"); tableContainer.addEventListener("click", function (event) { // 获取到点击事件的目标元素 let target = event.target; // 向上遍历DOM树,找到具有相应类名的祖先元素 while (target !== tableContainer && ![...target.classList].some(className => ["deleteButton"].includes(className))) { target = target.parentNode; } if (target.classList.contains("deleteButton")) { // 点击了"审批通过"按钮 const row = target.closest("tr"); const cardID = row.querySelector("td:first-child").textContent; deleteStaff(cardID); // display(); // 重新显示数据 } }); </script> <script> function deleteStaff(id) { const requestUrl = `http://localhost:8080/user/deleteStaff/${id}`; fetch(requestUrl, { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, }) .then(res => res.json()) .then(data => { if (data.msg === 'success') { alert("删除成功") } else { alert("删除失败 " + data.msg); } }) .catch(error => { alert("请求失败,请重试"); console.error(error); }); } </script> </html>
标签:function,const,11.24,alert,error,document,data From: https://www.cnblogs.com/zzqq1314/p/17910893.html