编写前台数据展示页
1.在src/views目录下创建一个目录,目录名称为系统名称todo,该目录下面以后就存放该系统的页面文件
2.在src/views/todo目录下创建 todo-add-or-update.vue 文件,并将 src/views/basic/examples/template/ 中对应的文件内容复制进来即可
<template>
<div>
<!--这是表单-->
<el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
<el-form-item>
<el-input v-model="dataForm.searchKey" placeholder="请输入查询条件" clearable :autofocus="true">
<el-button slot="append" icon="el-icon-search" @click="getDataList()"></el-button>
</el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="addOrUpdateHandle()" icon="el-icon-plus" v-if="isAuth('todo:save')">
新增
</el-button>
<el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0"
icon="el-icon-delete">批量删除
</el-button>
</el-form-item>
</el-form>
<!--这是表格-->
<el-table
:data="dataList"
border
v-loading="dataListLoading"
@selection-change="selectionChangeHandle"
style="width: 100%;">
<el-table-column
type="selection"
header-align="center"
align="center"
width="50">
</el-table-column>
<el-table-column
prop="name"
header-align="center"
align="center"
label="待办事项">
</el-table-column>
<el-table-column
prop="status"
header-align="center"
align="center"
label="状态">
<template slot-scope="scope">
<el-tag v-if="scope.row.status === 1" size="small">已完成</el-tag>
<el-tag v-else size="small" type="danger">未完成</el-tag>
</template>
</el-table-column>
<el-table-column
fixed="right"
header-align="center"
align="center"
width="150"
label="操作">
<template slot-scope="scope">
<el-button type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
<el-button type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
</template>
</el-table-column>
</el-table>
<!--这是分页-->
<el-pagination
@size-change="sizeChangeHandle"
@current-change="currentChangeHandle"
:current-page="pageIndex"
:page-sizes="[10, 20, 50, 100]"
:page-size="pageSize"
:total="totalPage"
background
layout="total, prev, pager, next">
</el-pagination>
<!-- 弹窗, 新增 / 修改 -->
<add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
</div>
</template>
<script>
import AddOrUpdate from './todo-add-or-update'
import {pageMixins} from '@/mixins/page'
export default {
data() {
return {
dataForm: {
searchKey: ''
},
dataList: [],
dataListLoading: false,
dataListSelections: [],
addOrUpdateVisible: false
}
},
mixins: [pageMixins],
components: {
AddOrUpdate
},
activated() {
this.getDataList()
},
methods: {
// 获取数据列表
getDataList() {
this.dataListLoading = true
this.$get('/todo/list', {
'page': this.pageIndex,
'limit': this.pageSize,
'searchKey': this.dataForm.searchKey
}).then(data => {
// 查询成功后,将获取的数据放入dataList中进行页面渲染
this.dataListLoading = false
this.dataList = data.list
this.totalPage = data.totalCount
})
},
// 多选触发、传入的是多选的数组对象
selectionChangeHandle(val) {
this.dataListSelections = val
},
// 新增 / 修改
addOrUpdateHandle(id) {
this.addOrUpdateVisible = true
// 保证在下一次页面渲染完成后执行
this.$nextTick(() => {
this.$refs.addOrUpdate.init(id)
})
},
// 删除
deleteHandle(id) {
// 批量删除
var ids = id ? [id] : this.dataListSelections.map(item => {
return item.id
})
this.$confirm(`确定进行${id ? '删除' : '批量删除'}操作?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
// 确定后调用接口进行批量删除
this.$post('/todo/delete', ids).then(data => {
this.$message.success('操作成功')
// 删除成功后页面数据手动刷新
this.getDataList()
})
}).catch(() => {
})
}
}
}
</script>
编写前台数据修改页
在src/views/todo目录下创建 todo-add-or-update.vue 文件,并将 src/views/basic/examples/template/ 中对应的文件内容复制进来即可
<template>
<el-dialog
:title="!dataForm.id ? '新增待办': '修改待办'"
:close-on-click-modal="false"
:visible.sync="visible"
top="10vh"
>
<el-form
:model="dataForm"
:rules="dataRule"
ref="dataForm"
@keyup.enter.native="dataFormSubmit()"
label-width="80px">
<el-form-item label="待办事项" prop="name">
<el-input v-model="dataForm.name" placeholder="待办事项"></el-input>
</el-form-item>
<el-form-item label="状态" prop="status">
<el-radio-group v-model="dataForm.status">
<el-radio :label="1">已完成</el-radio>
<el-radio :label="-1">未完成</el-radio>
</el-radio-group>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="visible = false" icon="el-icon-close">取消</el-button>
<el-button type="primary" @click="dataFormSubmit()" icon="el-icon-check">确定</el-button>
</span>
</el-dialog>
</template>
<script>
export default {
data() {
return {
visible: false,
dataForm: {
id: 0,
name: '',
status: 0
},
dataRule: {
name: [{required: true, message: '待办事项不能为空', trigger: 'blur'}]
}
}
},
methods: {
init(id) {
this.dataForm.id = id || 0
this.$get(`/todo/info/${this.dataForm.id}`).then(data => {
this.visible = true
this.dataForm.id = data.id
this.dataForm.name = data.name
this.dataForm.status = data.status
})
},
// 表单提交
dataFormSubmit() {
this.$refs['dataForm'].validate((valid) => {
if (valid) { // 校验通过
this.$post(`/todo/${!this.dataForm.id ? 'save' : 'update'}`, {
'id': this.dataForm.id || undefined,
'name': this.dataForm.name,
'status': this.dataForm.status
}).then(data => {
this.visible = false
// 让父组件刷新列表数据
this.$emit('refreshDataList')
this.$message.success('操作成功')
})
}
})
}
}
}
</script>