<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.wrapper {
width: 600px;
margin: 0 auto;
}
.modal {
width: 100%;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
display: none;
}
.modal .form {
width: 500px;
height: 300px;
background-color: #fff;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
padding: 20px;
box-sizing: border-box;
}
.modal .form input {
width: 100%;
height: 35px;
margin-bottom: 20px;
}
.modal div span:nth-of-type(2) {
float: right;
}
.modal .form div:nth-of-type(4) {
width: 100%;
text-align: center;
}
</style>
</head>
<body>
<div class="modal">
<div class="form">
<input type="hidden" value="" class="active">
<div><span>修改记录</span><span class="closeModal">X</span></div>
<hr>
<div><input type="text" class="username" placeholder="用户名"></div>
<div><input type="password" class="pwd" placeholder="密码"></div>
<div><button class="closeModal">关闭</button>  <button class="addInfo">确认</button></div>
</div>
</div>
<div class="wrapper">
<div>
<input type="search" value="">
<button class="search">查询</button>
<button class="add">增加</button>
</div>
<div class="tb">
<table border="1" width="500">
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<script src="./jquery-3.6.0/jquery-3.6.0.js"></script>
<script>
$(function() {
$(".add").click(function() {
showWindow()
})
$("tbody").on("click", ".update", function() {
var id = $(this).parent().parent().children().eq(0).text();
showWindow(id);
})
function showWindow(param) {
$(".modal").fadeIn();
if (param != undefined) {
userArr = localStorage.userList == undefined ? [] : JSON.parse(localStorage.userList);
var updateuser = userArr.find(obj => {
return obj.id == param;
})
$(".username").val(updateuser.username);
$(".pwd").val(updateuser.password);
$(".active").val(updateuser.id);
}
}
$(".closeModal").click(function() {
closeModal();
})
function closeModal() {
$(".modal").fadeOut();
$(".username").val("");
$(".pwd").val("");
$(".active").val("");
}
var nextId = localStorage.nextId == undefined ? 0 : localStorage.nextId * 1;
var userArr = localStorage.userList == undefined ? [] : JSON.parse(localStorage.userList);
function addInfo() {
userArr = localStorage.userList == undefined ? [] : JSON.parse(localStorage.userList);
var username = $(".username").val();
var pwd = $(".pwd").val();
var obj = {
id: nextId++,
username: username,
password: pwd
}
userArr.push(obj);
$(".username").val("");
$(".pwd").val("")
localStorage.userList = JSON.stringify(userArr);
localStorage.nextId = nextId;
show();
}
$(".addInfo").click(function() {
var id = $(".active").val();
if (id == "" || id == undefined) {
addInfo();
} else {
updateInfo(id);
}
closeModal();
})
function updateInfo(param) {
userArr = localStorage.userList == undefined ? [] : JSON.parse(localStorage.userList);
var user = userArr.find(obj => {
return obj.id == param;
})
user.username = $(".username").val();
user.password = $(".pwd").val();
localStorage.userList = JSON.stringify(userArr);
show();
}
show();
function show(filterArr) {
$("tbody").html("");
userArr = localStorage.userList == undefined ? [] : JSON.parse(localStorage.userList);
console.log(filterArr);
if (filterArr != undefined) {
userArr = filterArr;
}
console.log(userArr);
userArr.forEach(obj => {
$("tbody").prepend(`<tr>
<td>${obj.id}</td>
<td>${obj.username}</td>
<td>${obj.password}</td>
<td>
<button class="update">修改</button>
<button class="del">删除</button>
</td>
</tr>`)
})
}
$(".tb").on('click', '.del', function() {
if (confirm("确认删除吗?")) {
userArr = localStorage.userList == undefined ? [] : JSON.parse(localStorage.userList);
var id = $(this).parent().parent().children().eq(0).text();
var index = userArr.findIndex(obj => {
return obj.id == id;
})
userArr.splice(index, 1);
localStorage.userList = JSON.stringify(userArr);
show();
}
})
function filterInfo(param) {
userArr = localStorage.userList == undefined ? [] : JSON.parse(localStorage.userList);
var filterArr = userArr.filter(obj => {
return obj.username.includes(param);
})
show(filterArr);
}
$(".search").click(function() {
var info = $("input[type=search]").val();
filterInfo(info);
$("input[type=search]").val("");
})
})
</script>
</body>
</html>

