目录
一、页面展示
二、代码分析
html和css的样式 属于基本操作,分析只讲 js开始
说明: 这个案例,减少了dom操作,采取操作数据的形式,增加和删除都是针对于数组的操作,然后根据数据渲染页面。
核心思路:
1. 声明一个空的数组
2.点击录入,根据相关数据,生成对象,追加到数组里面
1. 首先取消表单默认提交事件
2.创建新的对象,里面存储 表单获取过来的数据
3. 追加给数组
4.渲染数据。遍历数组,动态生成tr,里面填写对应的td数据,并追加给tbody
5.重置表单
6.注意防止多次生成多条数据,先清空
3.根据数组数据渲染页面-表格的 行
4.点击删除按钮,删除的是对应数组里面的数据
1. 采用事件委托形式,给tbody 注册点击事件
2.点击链接,要删除的是对应数组里面的这个数据,而不是删除dom节点,如何找到这个数据?
3.前面渲染数据的时候,动态给a链接添加自定义属性 data-id = "0",这样点击当前对象就知道索引号了
4.根据索引号,利用splice 删除这条数据
5.重新渲染
5.再次根据数组的数据,渲染页面
6. 非空判断-- 点击新增需要验证表单
1.获取所有需要填写的表单,他们共同特点都有name属性
2.遍历这些表单,如果有一个值为空,则return 返回提示输入为空中断程序
3.注意书写的位置,应该放到新增数据的前面,阻止默认行为的后面
三、 全部代码
css外部样式表
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color:#721c24;
}
h1 {
text-align: center;
color:#333;
margin: 20px 0;
}
table {
margin:0 auto;
width: 800px;
border-collapse: collapse;
color:#004085;
}
th {
padding: 10px;
background: #cfe5ff;
font-size: 20px;
font-weight: 400;
}
td,th {
border:1px solid #b8daff;
}
td {
padding:10px;
color:#666;
text-align: center;
font-size: 16px;
}
tbody tr {
background: #fff;
}
tbody tr:hover {
background: #e1ecf8;
}
.info {
width: 900px;
margin: 50px auto;
text-align: center;
}
.info input, .info select {
width: 80px;
height: 27px;
outline: none;
border-radius: 5px;
border:1px solid #b8daff;
padding-left: 5px;
box-sizing: border-box;
margin-right: 15px;
}
.info button {
width: 60px;
height: 27px;
background-color: #004085;
outline: none;
border: 0;
color: #fff;
cursor: pointer;
border-radius: 5px;
}
.info .age {
width: 50px;
}
html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>学生信息管理</title>
<link rel="stylesheet" href="css/index.css" />
</head>
<body>
<h1>新增学员</h1>
<form class="info" autocomplete="off">
姓名:<input type="text" class="uname" name="uname" />
年龄:<input type="text" class="age" name="age" />
性别:
<select name="gender" class="gender">
<option value="男">男</option>
<option value="女">女</option>
</select>
薪资:<input type="text" class="salary" name="salary" />
就业城市:<select name="city" class="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="广州">广州</option>
<option value="深圳">深圳</option>
<option value="曹县">曹县</option>
</select>
<button class="add">录入</button>
</form>
<h1>就业榜</h1>
<table>
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>薪资</th>
<th>就业城市</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!--
<tr>
<td>1001</td>
<td>欧阳霸天</td>
<td>19</td>
<td>男</td>
<td>15000</td>
<td>上海</td>
<td>
<a href="javascript:">删除</a>
</td>
</tr>
-->
</tbody>
</table>
<script>
// 获取元素
const uname = document.querySelector(".uname")
const age = document.querySelector(".age")
const gender = document.querySelector(".gender")
const salary = document.querySelector(".salary")
const city = document.querySelector(".city")
const tbody = document.querySelector('tbody')
// 获取所有带有 name 属性 的元素
const items = document.querySelectorAll('[name]')
const arr = [] // 声明一个空的数组,对增加和删除都是对这个数组进行操作
// 1. 录入模块
//1.1 表单提交事件
const info = document.querySelector(".info")
info.addEventListener('submit', function (e) {
// 阻止默认行为 不跳转
e.preventDefault()
// console.log(11)
// 4. 这里进行表单验证 如果不通过,直接中断,不需要添加数据
// 先遍历循环
for (let i = 0; i < items.length; i++) {
if (items[i].value=== '') {
return alert('输入内容不能为空') // 退出函数
}
}
// 创建新的对象
const obj = {
stuId: arr.length + 1,
uname: uname.value,
age: age.value,
gender: gender.value,
salary: salary.value,
city: city.value,
}
// console.log(obj)
// 追加到数组里面
arr.push(obj)
//清空表单 重置
this.reset() // this === info
//调用渲染函数
render()
})
//2. 渲染函数 因为增加和删除都需要渲染
function render() {
// 先清空tbody ,把最新数组里面的数据渲染完毕
tbody.innerHTML = ''
// 遍历arr数组
for (let i = 0; i < arr.length; i++) {
//生成tr
const tr = document.createElement('tr')
tr.innerHTML = `
<td>${arr[i].stuId}</td>
<td>${arr[i].uname}</td>
<td>${arr[i].age}</td>
<td>${arr[i].gender}</td>
<td>${arr[i].salary}</td>
<td>${arr[i].city}</td>
<td>
<a href="javascript: data-id=${i}">删除</a>
</td>
`
// 追加元素 父元素.appendChild(子元素)
tbody.appendChild(tr)
}
}
//3. 删除操作
//3.1 事件委托 tbody
tbody.addEventListener('click', function (e) {
if (e.target.tagName === 'A') {
// 得到当前元素的 自定义属性 data-id
console.log(e.target.dataset.id);
//删除 arr 数组里面对应的数据
arr.splice(e.target.dataset.id, 1)
console.log(arr)
//重新渲染一次
render()
}
})
</script>
</body>
</html>