restful 风格前端表格增删改查模板

restful 风格前端表格增删改查模板

基于 vue d2-admin element-ui 配合上篇文章 js 代码生成器,只需要简单的修改即可实现快速页面开发。

vue 模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<template>
<d2-container>
<template slot="header">
<div class="flex justify-between align-center">
<div>
<el-input v-model="search" clearable size="mini" placeholder="输入名称搜索">
<el-button slot="append" icon="el-icon-search" @click="getData"></el-button>
</el-input>
</div>
<div>
<el-button style="margin-bottom: 5px" size="mini" @click="getData">刷新</el-button>
<el-button style="margin-bottom: 5px" size="mini" @click="addDataDialog=true">新增</el-button>
</div>
</div>
</template>
<div style="margin: 15px 0">
<el-table :data="data.data" style="width: 100%" size="mini" stripe v-loading="loading" @sort-change="({ column, prop, order })=>{pagination.order = order === 'ascending' ? prop : (order === 'descending' ? '-' + prop : '');getData()}">
<el-table-column prop="name" label="名称"></el-table-column>
<el-table-column label="创建时间">
<template slot-scope="scope">
{{new Date(scope.row.createdAt)|date_format}}
</template>
</el-table-column>
<el-table-column label="编辑" width="200">
<template slot-scope="scope">
<el-button size="mini" icon="el-icon-edit" @click="()=>{editDataForm=JSON.parse(JSON.stringify(scope.row));editDataDialog=true}">编辑</el-button>
<el-button type="danger" size="mini" icon="el-icon-delete" @click="removeData(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
<el-pagination
style="margin: 15px 0"
background
layout="->,sizes,prev, pager, next, jumper"
:page-size="pagination.pageSize"
:current-page="pagination.currentPage"
:total="pagination.total"
@size-change="(e)=>{pagination.pageSize=e;getData()}"
@current-change="(e)=>{pagination.currentPage=e;getData()}"
>
</el-pagination>
<el-dialog
title="添加"
:visible.sync="addDataDialog"
width="50%">
<el-form ref="addDataForm" :model="addDataForm" :rules="addRules" label-width="80px" size="mini">
<el-form-item label="名称" prop="name">
<el-input v-model="addDataForm.name"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="addDataDialog = false">取 消</el-button>
<el-button type="primary" @click="addData">确 定</el-button>
</span>
</el-dialog>
<el-dialog
title="编辑"
:visible.sync="editDataDialog"
width="50%">
<el-form ref="editDataForm" :model="editDataForm" :rules="editRules" label-width="80px" size="mini">
<el-form-item label="名称" prop="name">
<el-input v-model="editDataForm.name"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="editDataDialog = false">取 消</el-button>
<el-button type="primary" @click="editData">确 定</el-button>
</span>
</el-dialog>
</d2-container>
</template>

<script>
import { get_sys_user_mp as getData, post_sys_user_mp as createData, put_mp_id as updateData, delete_mp_id as removeData } from '@/api/sys_user'
export default {
name: 'demo',
data () {
return {
search: '',
loading: false,
pagination: {
currentPage: 1,
pageSize: 10,
total: 0,
order: ''
},
data: {
data: []
},
addDataDialog: false,
addDataForm: {
name: ''
},
editDataDialog: false,
editDataForm: {
name: ''
},
addRules: {
name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
},
editRules: {
name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
}
}
},
mounted () {
this.getData()
},
methods: {
getData () {
this.loading = true
getData({
currentPage: this.pagination.currentPage,
pageSize: this.pagination.pageSize,
search: this.search,
order: this.pagination.order
}).then(res => {
if (res.errno === 0) {
this.data = res.data
this.pagination.total = res.data.count
} else {
this.$message.warning('获取失败')
}
this.loading = false
}).catch((err) => {
console.log(err)
this.loading = false
})
},
removeData (data) {
this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
removeData({ id: data._id }).then(res => {
if (res.errno === 0) {
this.getData()
} else {
this.$message.warning('删除失败')
}
}).catch(() => {
this.$message.warning('删除失败')
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
})
})
},
addData () {
this.$refs.addDataForm.validate((valid) => {
if (valid) {
createData({
name: this.addDataForm.name
}).then(res => {
if (res.errno === 0) {
this.getData()
this.$message.success('添加成功')
this.addDataDialog = false
} else {
this.$message.warning('添加失败')
}
}).catch(() => {
this.$message.warning('添加失败')
})
} else {
this.$message.warning('填写完整')
return false
}
})
},
editData () {
this.$refs.editDataForm.validate((valid) => {
if (valid) {
updateData({
id: this.editDataForm._id,
name: this.editDataForm.name
}).then(res => {
if (res.errno === 0) {
this.getData()
this.editDataDialog = false
this.$message.success('修改成功')
} else {
this.$message.warning('修改失败')
}
}).catch(() => {
this.$message.warning('修改失败')
})
} else {
this.$message.warning('填写完整')
return false
}
})
}
}
}
</script>

获取分页数据接口参数格式

  • Query:

    1
    { "currentPage": 1, "pageSize": 10, "search": "", "order": "" }
  • 返回数据

    1
    { "errno": 0, "errmsg": "", "data": { "pageSize": 10, "currentPage": 1, "count": 4, "totalPages": 1, "data": [] } }