编写crud功能实现。
主要有如下功能:
在router/index.js中添加路由
{
path: '/post',
component: Layout,
redirect: '/post',
children: [{
path: 'post',
name: 'Post',
component: () => import('@/views/post/list'),
meta: { title: '岗位管理', icon: 'el-icon-menu' }
}]
},
页面:
再api下创建post.js文件
import request from '@/utils/request'
export function findPage(page) {
return request({
url: '/post/page',
method: 'post',
data: page
})
}
export function getById(id) {
return request({
url: '/post/getById/' + id,
method: 'get'
})
}
export function save(post) {
return request({
url: '/post/add',
method: 'post',
data: post
})
}
export function updateById(post) {
return request({
url: '/post/updateById',
method: 'put',
data: post
})
}
export function delById(id) {
return request({
url: '/post/del/' + id,
method: 'delete'
})
}
创建post文件夹,创建list.vue文件
<template>
<div class="app-container">
<div>
<el-button type="primary" class="add-button" size="normal" @click="openAddDialog">添加</el-button>
<br><br>
</div>
<el-table
v-loading="listLoading"
:data="page.list"
element-loading-text="Loading"
border
fit
highlight-current-row
>
<el-table-column align="center" label="ID" width="95">
<template slot-scope="scope">
{{ scope.$index + 1 }}
</template>
</el-table-column>
<el-table-column label="岗位编码" align="center">
<template slot-scope="scope">
<span>{{ scope.row.postCode }}</span>
</template>
</el-table-column>
<el-table-column label="岗位名字" align="center">
<template slot-scope="scope">
<span>{{ scope.row.postName }}</span>
</template>
</el-table-column>
<el-table-column label="排序" align="center">
<template slot-scope="scope">
<span>{{ scope.row.postSort }}</span>
</template>
</el-table-column>
<el-table-column align="center" prop="created_at" label="创建时间">
<template slot-scope="scope">
<span>{{ scope.row.createTime }}</span>
</template>
</el-table-column>
<el-table-column fixed="right" label="操作" width="200">
<template slot-scope="scope">
<el-button
type="primary"
size="mini"
@click="handleEdit(scope.row.postId)"
>
编辑
</el-button>
<el-button
type="danger"
size="mini"
@click="handleDelete(scope.row.postId)"
>
删除
</el-button>
</template>
</el-table-column>
</el-table>
<br><br>
<!-- 分页 -->
<el-pagination
class="pagination"
:current-page="page.currentPage"
:page-sizes="[10,20,50,100]"
:page-size="page.pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="page.totalCount"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
<!-- 添加弹窗 -->
<el-dialog title="添加" :visible.sync="addDialog">
<el-form ref="form" :model="post" label-width="200px" size="normal">
<el-form-item label="岗位编码">
<el-input v-model="post.postCode"/>
</el-form-item>
<el-form-item label="岗位名字">
<el-input v-model="post.postName"/>
</el-form-item>
<el-form-item label="排序">
<el-input v-model="post.postSort"/>
</el-form-item>
<el-form-item>
<el-button type="primary" size="normal" @click="onSubmit">提交</el-button>
<el-button size="normal" @click="onCancel">取消</el-button>
</el-form-item>
</el-form>
</el-dialog>
<!-- 修改弹窗-->
<el-dialog title="修改" :visible.sync="updateDialog">
<el-form ref="form" :model="post" label-width="200px" size="normal">
<el-form-item label="岗位编码">
<el-input v-model="post.postCode"/>
</el-form-item>
<el-form-item label="岗位名字">
<el-input v-model="post.postName"/>
</el-form-item>
<el-form-item label="排序">
<el-input v-model="post.postSort"/>
</el-form-item>
<el-form-item>
<el-button type="primary" size="normal" @click="onSubmitUpdate">提交</el-button>
<el-button size="normal" @click="onCancel">取消</el-button>
</el-form-item>
</el-form>
</el-dialog>
</div>
</template>
<script>
import { save, delById, findPage, getById, updateById } from '@/api/post'
export default {
components: {
// eslint-disable-next-line vue/no-unused-components
},
data() {
return {
list: null,
listLoading: true,
// 控制添加弹窗
addDialog: false,
// 控制修改弹窗
updateDialog: false,
post: {},
page: {
currentPage: 1, // 当前页
pageSize: 10, // 每页显示条数
totalPage: 0, // 总页数
totalCount: 0, // 总条数
params: {}, // 查询参数对象
list: [] // 数据
}
}
},
created() {
this.fetchData()
},
methods: {
fetchData() {
this.listLoading = true
findPage(this.page).then(response => {
this.page = response.page
// console.log(response.data)
this.listLoading = false
})
},
handleEdit(id) {
// 修改
getById(id).then(res => {
// eslint-disable-next-line no-empty
if (res.code !== 20000) {
this.$message.success(res.error)
}
this.post = res.data
this.updateDialog = true
})
},
closeUpdateDialog() {
// 关闭修改弹窗
this.updateDialog = false
},
openAddDialog() {
// 控制添加弹窗打开
this.addDialog = true
},
closeAddDialog() {
// 控制添加弹窗关闭
this.addDialog = false
},
handleDelete(id) {
// 删除
this.$confirm('是否删除?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'error'
}).then(() => {
delById(id).then(res => {
this.$message.success(res.message)
this.fetchData()
})
})
},
// 分页切换
handleSizeChange(val) {
this.page.pageSize = val
this.fetchData()
},
handleCurrentChange(val) {
this.page.currentPage = val
this.fetchData()
},
// 新增调用方法
onSubmit() {
save(this.post).then(res => {
// console.log(res)
if (res.code !== 20000) {
this.$message.error(res.message)
this.fetchData()
this.addDialog = false
this.post = {}
} else {
this.$message.success(res.message)
this.fetchData()
this.addDialog = false
this.post = {}
}
})
},
onCancel() {
this.addDialog = false
this.updateDialog = false
},
onSubmitUpdate() {
updateById(this.post).then(res => {
this.$message.success(res.message)
this.fetchData()
this.updateDialog = false
})
}
}
}
</script>
腾云先锋(TDP,Tencent Cloud Developer Pioneer)是腾讯云GTS官方组建并运营的技术开发者群体。这里有最专业的开发者&客户,能与产品人员亲密接触,专有的问题&需求反馈渠道,有一群志同道合的兄弟姐妹。来加入属于我们开发者的社群吧!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。