使用element ui添加一个用户管理的页面用户数据存储在内存中。一条用户数据包含的字段有用户姓名、电话、邮箱、地址。需要在页面上呈现用户管理数据列表并且完成用户数据的增删改查以及数据分页的处理过程。
以下是一个使用element ui实现用户管理页面的示例代码:
<template>
<div class="user-management">
<el-card>
<div slot="header">
<h2>用户管理</h2>
</div>
<div class="search-bar">
<el-input v-model="search" placeholder="请输入关键词搜索"></el-input>
<el-button type="primary" @click="handleSearch">搜索</el-button>
<el-button type="default" @click="handleReset">重置</el-button>
</div>
<el-table :data="tableData" border stripe>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="phone" label="电话"></el-table-column>
<el-table-column prop="email" label="邮箱"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
<el-table-column label="操作">
<template slot-scope="scope">
<el-button type="text" @click="handleEdit(scope.$index)">编辑</el-button>
<el-button type="text" @click="handleDelete(scope.$index)">删除</el-button>
</template>
</el-table-column>
</el-table>
<div class="pagination">
<el-pagination
v-model="currentPage"
:total="totalCount"
:page-size="pageSize"
layout="prev, pager, next"
@current-change="handlePageChange"
></el-pagination>
</div>
<el-dialog :visible.sync="dialogVisible" title="编辑用户">
<el-form :model="currentData" ref="form" label-width="80px">
<el-form-item label="姓名" prop="name">
<el-input v-model="currentData.name"></el-input>
</el-form-item>
<el-form-item label="电话" prop="phone">
<el-input v-model="currentData.phone"></el-input>
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="currentData.email"></el-input>
</el-form-item>
<el-form-item label="地址" prop="address">
<el-input v-model="currentData.address"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="handleSave">确 定</el-button>
</div>
</el-dialog>
</el-card>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [], // 用户数据列表
search: '', // 搜索关键词
dialogVisible: false, // 编辑用户对话框是否可见
currentData: {}, // 当前编辑的用户数据
totalCount: 0, // 总记录数
currentPage: 1, // 当前页码
pageSize: 10, // 每页显示的记录数
};
},
mounted() {
// 初始化数据
this.getData();
},
methods: {
getData() {
// 模拟从内存中获取数据
const data = [
{
name: '张三',
phone: '13888888888',
email: 'zhangsan@example.com',
address: '北京市海淀区',
},
{
name: '李四',
phone: '13999999999',
email: 'lisi@example.com',
address: '上海市浦东新区',
},
// ...
];
this.tableData = data;
this.totalCount = data.length;
},
handleSearch() {
// 处理搜索操作
// 模拟从内存中搜索数据
const searchData = this.tableData.filter((item) =>
Object.values(item).some((value) =>
String(value).includes(this.search.trim()),
),
);
this.tableData = searchData;
this.totalCount = searchData.length;
},
handleReset() {
// 处理重置操作
this.search = '';
this.getData();
},
handlePageChange(currentPage) {
// 处理页码变化事件
// 模拟从内存中获取数据
const startIndex = (currentPage - 1) * this.pageSize;
const data = this.tableData.slice(startIndex, startIndex + this.pageSize);
this.tableData = data;
},
handleEdit(index) {
// 处理编辑操作
this.currentData = { ...this.tableData[index] };
this.dialogVisible = true;
},
handleDelete(index) {
// 处理删除操作
this.tableData.splice(index, 1);
this.totalCount -= 1;
},
handleSave() {
// 处理保存操作
this.$refs.form.validate((valid) => {
if (valid) {
const index = this.tableData.findIndex(
(item) => item.name === this.currentData.name,
);
if (index >= 0) {
// 编辑用户
this.tableData.splice(index, 1, this.currentData);
} else {
// 添加用户
this.tableData.push(this.currentData);
this.totalCount += 1;
}
this.dialogVisible = false;
}
});
},
},
};
</script>
<style>
.user-management {
padding: 20px;
}
.search-bar {
display: flex;
align-items: center;
justify-content: flex-end;
margin-bottom: 20px;
}
.pagination {
display: flex;
justify-content: center;
margin-top: 20px;
}
.dialog-footer {
display: flex;
justify-content: flex-end;
}
</style>
注意,上述代码仅为示例,实际使用时需要根据具体需求进行修改和调整。此外,由于本示例代码中用户数据是存储在内存中的,因此页面刷新后数据将会消失。若需要长期保存用户数据,需将数据存储在数据库或文件中。
原文地址: https://www.cveoy.top/t/topic/bQEa 著作权归作者所有。请勿转载和采集!