3 Star 2 Fork 10

xpzll/keshihua96

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
student.html 21.84 KB
一键复制 编辑 原始数据 按行查看 历史
xpzll 提交于 2023-04-11 17:51 . 关闭重置表单
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>学生</title>
<link rel="stylesheet" href="./bootstrap/bootstrap.min.css" />
<link rel="stylesheet" href="./bootstrap/bootstrap-icons.css" />
<link rel="stylesheet" href="./css/common.css" />
<link rel="stylesheet" href="./css/student.css" />
<style>
.card .table td,
.card .table th {
padding-left: 0;
padding-right: 0;
}
</style>
</head>
<body class="application application-offset">
<div class="container-fluid container-application">
<div class="sidenav show" id="sidenav-main">
<!-- Sidenav header -->
<div class="sidenav-header d-flex align-items-center">
<a class="navbar-brand" href="./index.html">
<span class="logo">·ITCAST·</span>
</a>
</div>
<!-- User mini profile -->
<div
class="sidenav-user d-flex flex-column align-items-center justify-content-between text-center"
>
<!-- Avatar -->
<div>
<a href="#" class="avatar rounded-circle avatar-xl">
<img
alt="Image placeholder"
src="https://yanxuan-item.nosdn.127.net/8b27deb1670c53e67c42ca3e1ed6fd12.jpg"
class=""
/>
</a>
<div class="mt-5">
<h5 class="mb-0 text-white">黑马前端</h5>
<span class="d-block text-sm text-white opacity-8 mb-3"
>数据可视化</span
>
<a
href="javascript:;"
class="btn btn-sm btn-white btn-icon rounded-pill shadow hover-translate-y-n3"
>
<span class="btn-inner--text">学前端来黑马</span>
</a>
</div>
</div>
</div>
<!-- Application nav -->
<div class="nav-application clearfix">
<a href="./index.html" class="btn btn-square text-sm">
<span class="btn-inner--icon d-block"
><i class="bi bi-house bi-2x"></i
></span>
<span class="btn-inner--icon d-block pt-2">首页</span>
</a>
<a href="./student.html" class="btn btn-square text-sm active">
<span class="btn-inner--icon d-block"
><i class="bi bi-people bi-2x"></i
></span>
<span class="btn-inner--icon d-block pt-2">学生</span>
</a>
<a href="javascript:;" class="btn btn-square text-sm">
<span class="btn-inner--icon d-block"
><i class="bi bi-columns bi-2x"></i
></span>
<span class="btn-inner--icon d-block pt-2">排版</span>
</a>
<a href="javascript:;" class="btn btn-square text-sm">
<span class="btn-inner--icon d-block"
><i class="bi bi-files bi-2x"></i
></span>
<span class="btn-inner--icon d-block pt-2">资料</span>
</a>
<a href="javascript:;" class="btn btn-square text-sm">
<span class="btn-inner--icon d-block"
><i class="bi bi-receipt bi-2x"></i
></span>
<span class="btn-inner--icon d-block pt-2">就业</span>
</a>
<a href="javascript:;" class="btn btn-square text-sm">
<span class="btn-inner--icon d-block"
><i class="bi bi-gear bi-2x"></i
></span>
<span class="btn-inner--icon d-block pt-2">设置</span>
</a>
</div>
<!-- Misc area -->
<div class="card bg-gradient-warning">
<div class="card-body">
<h5 class="text-white">哈喽, 朋友!</h5>
<p class="text-white mb-4">
为什么不现在开始学习前端,创造一些令人惊叹的东西呢?
</p>
<a
href="http://itcast.cn"
class="btn btn-sm btn-block btn-white rounded-pill"
target="_blank"
>Get started</a
>
</div>
</div>
</div>
<div class="main-content position-relative">
<nav
class="navbar navbar-main navbar-expand-lg navbar-dark navbar-border"
id="navbar-main"
>
<div class="container-fluid">
<!-- Navbar nav -->
<div
class="collapse navbar-collapse navbar-collapse-fade"
id="navbar-main-collapse"
>
<ul class="navbar-nav align-items-lg-center">
<!-- Home -->
<li class="nav-item">
<a class="nav-link pl-lg-0" href="./index.html"> 首页 </a>
</li>
<li class="nav-item">
<a class="nav-link pl-lg-0" href="./index.html"> 传智教育 </a>
</li>
<li class="nav-item">
<a class="nav-link pl-lg-0" href="./index.html">
黑马程序员
</a>
</li>
<li class="nav-item">
<a class="nav-link pl-lg-0" href="./index.html"> 文档 </a>
</li>
</ul>
<!-- Right menu -->
<ul
class="navbar-nav ml-lg-auto align-items-center d-none d-lg-flex"
>
<li class="nav-item dropdown dropdown-animate">
<a
class="nav-link pr-lg-0"
href=".dropdown-menu"
role="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
<div class="media media-pill align-items-center">
<span class="avatar rounded-circle">
<img
alt="Image placeholder"
src="https://yanxuan-item.nosdn.127.net/8b27deb1670c53e67c42ca3e1ed6fd12.jpg"
/>
</span>
<div class="ml-2 d-none d-lg-block">
<span class="mb-0 text-sm font-weight-bold">Admin</span>
</div>
</div>
</a>
</li>
<li class="nav-item">
<a class="nav-link pl-lg-0" id="logout" href="javascript:;">
退出
</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="page-content">
<div class="page-title mb-3">
<div class="row justify-content-between align-items-center">
<div class="col-md-6 mb-3 mb-md-0">
<h5 class="h3 font-weight-400 mb-0 text-white">Students</h5>
<span class="text-sm text-white opacity-8"
>一共有 <b class="total">0</b> 位学员</span
>
</div>
<div
class="col-md-6 d-flex align-items-center justify-content-between justify-content-md-end"
>
<a
id="openModal"
href="javascript:;"
class="btn btn-sm btn-white btn-icon-only rounded-circle ml-4"
>
<span class="btn-inner--icon"
><i class="bi bi-plus bi-2x"></i
></span>
</a>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="card card-fluid">
<div class="table-responsive" style="min-height: 1000px">
<table class="table align-items-center">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>组号</th>
<th>期望薪资</th>
<th>就业薪资</th>
<th>籍贯</th>
<th>操作</th>
</tr>
</thead>
<tbody class="list">
<!-- <tr>
<td>张杰</td>
<td>20</td>
<td>男</td>
<td>第2组</td>
<td>10000</td>
<td>13000</td>
<td>北京北京市东城区</td>
<td>
<a href="javascript:;" class="text-success mr-3"><i class="bi bi-pen"></i></a>
<a href="javascript:;" class="text-danger"><i class="bi bi-trash"></i></a>
</td>
</tr> -->
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- footer -->
<div class="footer pt-5 pb-4 footer-light" id="footer-main">
<div class="row text-center text-sm-left align-items-sm-center">
<div class="col-sm-6">
<p class="text-sm mb-0">
© 2022
<a href="https://itcast.cn" class="h6 text-sm" target="_blank"
>前端学科</a
>. All rights reserved.
</p>
</div>
<div class="col-sm-6 mb-md-0">
<ul class="nav justify-content-center justify-content-md-end">
<li class="nav-item">
<a class="nav-link" href="#">Support</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Terms</a>
</li>
<li class="nav-item">
<a class="nav-link pr-0" href="#">Privacy</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- add Modal -->
<div class="modal fade" id="modal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">添加学员</h5>
<button
type="button"
class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
></button>
</div>
<div class="modal-body">
<form id="form" class="g-3 row" autocomplete="off" novalidate>
<div class="col-sm-6">
<div class="form-group">
<label class="input-group-label">姓名:</label>
<input
type="text"
name="name"
class="form-control"
placeholder="请输入姓名"
/>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="input-group-label">性别:</label>
<div class="input">
<div class="form-check d-inline-block">
<input
value="0"
checked
id="cb01"
class="form-check-input"
type="radio"
name="gender"
/>
<label for="cb01" class="form-check-label"></label>
</div>
<div class="form-check d-inline-block">
<input
value="1"
id="cb02"
class="form-check-input"
type="radio"
name="gender"
/>
<label for="cb02" class="form-check-label"></label>
</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="input-group-label">年龄:</label>
<input
type="text"
name="age"
class="form-control"
placeholder="请输入年龄"
/>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="input-group-label">组号:</label>
<input
type="text"
name="group"
class="form-control"
placeholder="请输入1-8组号"
/>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="input-group-label">期望薪资:</label>
<input
type="text"
name="hope_salary"
class="form-control"
placeholder="请输入期望薪资"
/>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="input-group-label">就业薪资:</label>
<input
type="text"
name="salary"
class="form-control"
placeholder="请输入就业薪资"
/>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label class="input-group-label">籍贯:</label>
<div class="input pl-0">
<select class="form-select custom-select" name="province">
<option value="">--省份--</option>
</select>
<select class="form-select custom-select" name="city">
<option value="">--城市--</option>
</select>
<select class="form-select custom-select" name="area">
<option value="">--地区--</option>
</select>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-secondary"
data-bs-dismiss="modal"
>
取消
</button>
<button type="button" class="btn btn-blue" id="submit">确认</button>
</div>
</div>
</div>
</div>
<!-- toast -->
<div
class="position-fixed top-0 start-50 pt-4"
style="z-index: 999999; transform: translateX(-50%)"
>
<div id="myToast" class="toast bg-rgba">
<div class="toast-body">提示消息</div>
</div>
</div>
<script src="./bootstrap/bootstrap.min.js"></script>
<script src="./lib/axios.js"></script>
<script src="./lib/form-serialize.js"></script>
<script src="./js/common.js"></script>
<script>
// 找到tbody
const tb = document.querySelector('.list')
// 加载学生数据的函数
async function loadData () {
const res = await axios({
url: '/students'
})
// console.log(res)
tb.innerHTML = res.data
.map(
v => `
<tr>
<td>${v.name}</td>
<td>${v.age}</td>
<td>${v.gender ? '' : ''}</td>
<td>第${v.group}组</td>
<td>${v.hope_salary}</td>
<td>${v.salary}</td>
<td>${v.province}${v.city}${v.area}</td>
<td>
<a href="javascript:;" class="text-success mr-3"><i data-id="${
v.id
}" class="bi bi-pen"></i></a>
<a href="javascript:;" class="text-danger"><i data-id="${
v.id
}" class="bi bi-trash"></i></a>
</td>
</tr>
`
)
.join('')
}
loadData()
// 委托给tbody
tb.addEventListener('click', async function (e) {
if (e.target.classList.contains('bi-trash')) {
// 1. 拿到被点的id发请求做删除
await axios({
url: `/students/${e.target.dataset.id}`,
method: 'delete'
})
// 2. 删除后重新渲染页面
loadData()
}
})
// 获取省的下拉框
const selPro = document.querySelector('[name=province]')
// 获取市的下拉框
const selCity = document.querySelector('[name=city]')
// 获取区的下拉框
const selArea = document.querySelector('[name=area]')
// 给省加change事件
selPro.addEventListener('change', async function () {
await loadCity()
await loadArea()
})
// 给市加change事件
selCity.addEventListener('change', async function () {
await loadArea()
})
async function loadProvince () {
// 获取省
const res = await axios({
url: '/api/province'
})
selPro.innerHTML = res.data.map(v => `<option>${v}</option>`).join('')
}
// 获取市
async function loadCity () {
const res = await axios({
url: '/api/city',
params: {
pname: selPro.value
}
})
selCity.innerHTML = res.data.map(v => `<option>${v}</option>`).join('')
}
// 获取区
async function loadArea () {
const res = await axios({
url: '/api/area',
params: {
pname: selPro.value,
cname: selCity.value
}
})
selArea.innerHTML = res.data.map(v => `<option>${v}</option>`).join('')
}
// 加载省市区
async function loadSelectData () {
await loadProvince()
await loadCity()
await loadArea()
}
loadSelectData()
// 找到模态框
const modal = new bootstrap.Modal('#modal')
// 给新增按钮加点击事件
document
.querySelector('#openModal')
.addEventListener('click', function () {
// 修改标题
document.querySelector('.modal-title').innerHTML = '添加学员'
modal.show()
})
const form = document.querySelector('#form')
// 给模态框里的确定加点击事件
document
.querySelector('#submit')
.addEventListener('click', async function () {
// 1. 获取表单里所有内容
const data = serialize(form, { hash: true })
if (!data.name) return tip('姓名不能为空')
if (data.name.length < 1 || data.name.length > 30)
return tip('姓名在1-30之间')
// 在界面中输入的内容,我们拿到时永远拿到的是字符串
data.age = +data.age
data.gender = +data.gender
data.hope_salary = +data.hope_salary
data.salary = +data.salary
data.group = +data.group
if (document.querySelector('.modal-title').innerHTML == '添加学员') {
// 2. 发请求给服务器
const res = await axios({
url: '/students',
method: 'post',
data
})
} else {
// 修改
const res = await axios({
url: `/students/${id}`,
method: 'put',
data
})
}
// 3. 关闭模态框
modal.hide()
// 重置表单
form.reset()
// 4. 加载最新数据
loadData()
})
// 全局变量
let id
// 给tbody加事件委托 - 修改的点击事件
tb.addEventListener('click', async function (e) {
if (e.target.classList.contains('bi-pen')) {
// 先把id存到全局变量
id = e.target.dataset.id
// 发请求拿到当前的数据详情
const res = await axios({
url: `/students/${e.target.dataset.id}`
})
// 修改标题
document.querySelector('.modal-title').innerHTML = '修改学员'
// 回显给模态框
// document.querySelector('[name=name]').value = res.data.name
// document.querySelector('[name=age]').value = res.data.age
for (const key in res.data) {
const dom = document.querySelector(`[name=${key}]`)
if (dom) {
// 判断属性是否为gender
if (key === 'gender') {
// 如果gender的值是1,就让女的单选框被选中
res.data[key] == 1
? (document.querySelector('#cb02').checked = true)
: (document.querySelector('#cb01').checked = true)
} else {
dom.value = res.data[key]
}
}
}
// 省市区单独做处理
document.querySelector('[name=province]').value = res.data.province
// 我就可以得到最新的市
await loadCity()
document.querySelector('[name=city]').value = res.data.city
await loadArea()
document.querySelector('[name=area]').value = res.data.area
// 弹出模态框
modal.show()
}
})
// 模态框的隐藏事件
document.querySelector('#modal').addEventListener('hidden.bs.modal',async function () {
// 清空表单
form.reset()
selPro.value = '北京'
await loadCity()
await loadArea()
})
</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xpzll/keshihua96.git
git@gitee.com:xpzll/keshihua96.git
xpzll
keshihua96
keshihua96
master

搜索帮助