b站视频演示效果:
效果图:
完整代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.0"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f9;
}
.header {
background-color: #4CAF50;
color: white;
padding: 15px;
display: flex;
justify-content: space-between;
align-items: center;
}
.btn-add {
padding: 10px 15px;
background-color: #fff;
color: #4CAF50;
border: none;
border-radius: 5px;
cursor: pointer;
}
.main-content {
margin: 20px;
}
.user-management {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
.search-bar {
margin-bottom: 15px;
padding: 8px;
width: 100%;
border: 1px solid #ccc;
border-radius: 5px;
}
table {
width: 100%;
border-collapse: collapse;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 10px;
text-align: center;
}
th {
background-color: #4CAF50;
color: white;
cursor: pointer;
}
button {
padding: 5px 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
}
.modal-content {
background-color: white;
padding: 20px;
border-radius: 8px;
width: 300px;
text-align: center;
}
.btn-cancel {
background-color: #f44336;
}
</style>
</head>
<body>
<div id="app">
<header class="header">
<h1>后台管理系统</h1>
<button class="btn-add" @click="openAddModal">新增用户</button>
</header>
<main class="main-content">
<section class="user-management">
<h2>用户管理</h2>
<input type="text" placeholder="搜索用户..." v-model="searchQuery" class="search-bar">
<table>
<thead>
<tr>
<th @click="sortBy('name')">姓名</th>
<th>邮箱</th>
<th>角色</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="user in filteredUsers" :key="user.id">
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
<td>{{ user.role }}</td>
<td>
<button @click="openEditModal(user)">编辑</button>
<button @click="deleteUser(user.id)">删除</button>
</td>
</tr>
</tbody>
</table>
</section>
</main>
<div v-if="showModal" class="modal">
<div class="modal-content">
<h3>{{ modalTitle }}</h3>
<label>姓名: <input type="text" v-model="userForm.name" required></label><br>
<label>邮箱: <input type="email" v-model="userForm.email" required></label><br>
<label>角色: <input type="text" v-model="userForm.role" required></label><br>
<button @click="saveUser">{{ isEditing ? '保存修改' : '保存新增' }}</button>
<button class="btn-cancel" @click="showModal = false">取消</button>
</div>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
showModal: false,
isEditing: false,
modalTitle: '',
userForm: {
id: null,
name: '',
email: '',
role: ''
},
searchQuery: '',
users: [
{ id: 1, name: '张三', email: 'zhangsan@example.com', role: '管理员' },
{ id: 2, name: '李四', email: 'lisi@example.com', role: '编辑' },
{ id: 3, name: '王五', email: 'wangwu@example.com', role: '用户' }
],
sortKey: '',
sortAsc: true
},
computed: {
filteredUsers() {
return this.users.filter(user =>
user.name.includes(this.searchQuery) ||
user.email.includes(this.searchQuery) ||
user.role.includes(this.searchQuery)
).sort((a, b) => {
if (this.sortKey) {
let modifier = this.sortAsc ? 1 : -1;
return (a[this.sortKey] > b[this.sortKey] ? 1 : -1) * modifier;
}
return 0;
});
}
},
methods: {
sortBy(key) {
if (this.sortKey === key) {
this.sortAsc = !this.sortAsc;
} else {
this.sortKey = key;
this.sortAsc = true;
}
},
openAddModal() {
this.isEditing = false;
this.modalTitle = '新增用户';
this.userForm = { id: null, name: '', email: '', role: '' };
this.showModal = true;
},
openEditModal(user) {
this.isEditing = true;
this.modalTitle = '编辑用户';
this.userForm = Object.assign({}, user);
this.showModal = true;
},
saveUser() {
if (this.isEditing) {
let index = this.users.findIndex(u => u.id === this.userForm.id);
if (index !== -1) {
this.users.splice(index, 1, Object.assign({}, this.userForm));
}
alert('用户信息已修改');
} else {
let newId = this.users.length ? Math.max(...this.users.map(u => u.id)) + 1 : 1;
this.userForm.id = newId;
this.users.push(Object.assign({}, this.userForm));
alert('新增用户成功');
}
this.showModal = false;
},
deleteUser(id) {
this.users = this.users.filter(user => user.id !== id);
alert('删除用户: ' + id);
}
}
});
</script>
</body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
各位同学们:还有啥想看的功能或者特效不?欢迎在评论区留言哦!
本人承接网网站开发,如有需要,欢迎私信咨询!
如果您感觉文章对您有帮助~
那就打赏一下,请笔者喝杯咖啡吧~
给新手学习前端开发的建议:
-
了解基础知识:
- 学习HTML、CSS和JavaScript的基础知识。它们是前端开发的核心,构成了网页的基本结构和样式,以及交互功能。
- 掌握HTML的标签和语义化,了解CSS的选择器和布局技巧,熟悉JavaScript的基本语法和DOM操作。
-
实践项目:
- 不要仅仅停留在理论学习上,通过实践项目来巩固和应用所学知识。
- 可以从简单的静态页面开始,逐渐尝试添加交互效果和动态数据。
- 参与开源项目或自己动手创建一个个人网站,将所学知识应用到实际场景中。
-
学习工具和框架:
- 了解并学习前端开发中常用的工具和框架,如构建工具(Webpack、Gulp等)、版本控制工具(Git)、前端框架(React、Vue、Angular等)。
- 这些工具和框架能够提高开发效率,简化开发流程,是前端开发的重要组成部分。
-
关注前端趋势:
- 前端开发是一个快速发展的领域,新的技术和工具不断涌现。
- 关注前端社区、博客和会议,了解最新的技术趋势和发展方向,保持学习的热情和动力。
-
培养解决问题的能力:
- 前端开发常常会遇到各种问题和挑战,学会独立思考和解决问题是非常重要的。
- 遇到问题时,可以先尝试自己解决,通过查阅文档、搜索资料和社区讨论来找到答案。
- 如果实在无法解决,可以向同事或社区求助,但也要学会总结和分享自己的经验和教训。
-
不断学习和提升:
- 前端开发是一个不断学习和提升的过程,要保持对知识的渴望和追求。
- 可以通过阅读书籍、参加培训、参与开源项目等方式来不断提升自己的技能水平。
- 同时,也要关注自己的职业发展,了解行业的需求和趋势,规划自己的职业道路。
-
注重代码质量和可维护性:
- 编写高质量的代码是前端开发的基本要求之一。
- 学习并遵循代码规范,使用适当的命名和注释来提高代码的可读性。
- 注重代码的结构和逻辑,避免过度嵌套和复杂的逻辑。
- 考虑代码的可维护性,尽量编写可复用和可扩展的代码。
-
参与社区和交流:
- 加入前端开发的社区和论坛,与其他开发者进行交流和分享。
- 通过参与社区活动、回答问题、分享经验等方式,不仅可以提升自己的技能水平,还可以结识更多的同行和朋友。
总之,学习前端开发需要耐心和毅力,要保持对技术的热情和兴趣,不断学习和提升自己。通过实践项目、学习工具和框架、关注前端趋势等方式,你可以逐渐成为一名优秀的前端开发者。
加油吧!