管理用户界面包含增删改查功能
时间: 2023-07-12 07:28:24 浏览: 98
jAVAfx的增删改查demo文件
好的,以下是一个简单的 HTML 用户管理界面的示例代码,包含增删改查功能:
```
<!DOCTYPE html>
<html>
<head>
<title>用户管理</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background-color: #f1f1f1;
}
table {
background-color: #fff;
border-radius: 10px;
box-shadow: 0px 0px 10px 0px rgba(0,0,0,0.2);
padding: 20px;
width: 80%;
margin: 50px auto;
border-collapse: collapse;
}
th, td {
padding: 12px;
text-align: center;
border: 1px solid #ddd;
}
th {
background-color: #4CAF50;
color: white;
}
input[type=text], input[type=password], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>操作</th>
</tr>
<tr>
<td>1</td>
<td>张三</td>
<td>男</td>
<td>20</td>
<td>
<button onclick="editUser(1)">编辑</button>
<button onclick="deleteUser(1)">删除</button>
</td>
</tr>
<tr>
<td>2</td>
<td>李四</td>
<td>女</td>
<td>22</td>
<td>
<button onclick="editUser(2)">编辑</button>
<button onclick="deleteUser(2)">删除</button>
</td>
</tr>
<tr>
<td colspan="5">
<button onclick="addUser()">新增用户</button>
</td>
</tr>
</table>
<div id="userModal" style="display:none;">
<form>
<h2 id="modalTitle">新增用户</h2>
<label for="name">姓名</label>
<input type="text" id="name" name="name" placeholder="请输入姓名">
<label for="gender">性别</label>
<select id="gender" name="gender">
<option value="男">男</option>
<option value="女">女</option>
</select>
<label for="age">年龄</label>
<input type="text" id="age" name="age" placeholder="请输入年龄">
<button type="button" onclick="saveUser()">保存</button>
<button type="button" onclick="closeModal()">取消</button>
</form>
</div>
<script>
var modal = document.getElementById("userModal");
var title = document.getElementById("modalTitle");
var name = document.getElementById("name");
var gender = document.getElementById("gender");
var age = document.getElementById("age");
var currentId = 0;
function addUser() {
title.innerHTML = "新增用户";
name.value = "";
gender.value = "男";
age.value = "";
currentId = 0;
modal.style.display = "block";
}
function editUser(id) {
title.innerHTML = "编辑用户";
name.value = document.getElementsByTagName("tr")[id].getElementsByTagName("td")[1].innerHTML;
gender.value = document.getElementsByTagName("tr")[id].getElementsByTagName("td")[2].innerHTML;
age.value = document.getElementsByTagName("tr")[id].getElementsByTagName("td")[3].innerHTML;
currentId = id;
modal.style.display = "block";
}
function saveUser() {
if (name.value == "") {
alert("姓名不能为空!");
return;
}
if (age.value == "") {
alert("年龄不能为空!");
return;
}
if (currentId == 0) {
var table = document.getElementsByTagName("table")[0];
var newRow = table.insertRow(-1);
var newId = table.getElementsByTagName("tr").length - 2;
newRow.innerHTML = "<td>" + newId + "</td><td>" + name.value + "</td><td>" + gender.value + "</td><td>" + age.value + "</td><td><button onclick=\"editUser(" + newId + ")\">编辑</button><button onclick=\"deleteUser(" + newId + ")\">删除</button></td>";
} else {
document.getElementsByTagName("tr")[currentId].getElementsByTagName("td")[1].innerHTML = name.value;
document.getElementsByTagName("tr")[currentId].getElementsByTagName("td")[2].innerHTML = gender.value;
document.getElementsByTagName("tr")[currentId].getElementsByTagName("td")[3].innerHTML = age.value;
}
closeModal();
}
function deleteUser(id) {
if (confirm("确定要删除该用户吗?")) {
document.getElementsByTagName("table")[0].deleteRow(id);
}
}
function closeModal() {
modal.style.display = "none";
}
</script>
</body>
</html>
```
这段代码会生成一个包含用户列表的表格,每个用户行都包含一个编辑按钮和一个删除按钮。点击编辑按钮会弹出一个模态框,可以编辑该用户的信息。点击删除按钮会弹出一个确认对话框,确认删除该用户。在表格底部还有一个新增用户按钮,点击会弹出一个模态框,可以新增一个用户。
阅读全文