MokeJs

新建 mock\myUser.js

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
const Mock = require("mockjs");

const data = Mock.mock({
"items|120": [
{
id: "@id",
"row|+1": 1,
username: "@cname()",
date: '@date("yyyy-MM-dd")',
author: '@img("900x200","red","#000","你好啊")',
email: "@email()",
desc: "@paragraph()",
ip: "@ip()",
"status|1": ["未审核", "审核中", "已审核"],
},
],
});

module.exports = [
{
url: "/vue-admin-template/list/myUser",
type: "get",
response: (config) => {
const { page, limit, search, orderBy, orderByColumn } = config.query;
let items = [...data.items];

if (search !== undefined) {
items = items.filter((item, index) => {
return item.username.includes(search) || item.email.includes(search);
});
}

if (orderBy !== undefined) {
items =
orderBy === "ascending"
? items.sort((a, b) =>
a[orderByColumn] > b[orderByColumn] ? 0 : -1
)
: items.sort((a, b) =>
b[orderByColumn] > a[orderByColumn] ? 0 : -1
);
}

return {
code: 20000,
data: {
total: items.length,
items: items.slice((page - 1) * limit, page * limit),
},
};
},
},
];

编辑 mock\index.js

1
2
3
4
5
6
7
const myUser = require('./myUser')

const mocks = [
...user,
...table,
...myUser
]

新增 src\api\myUser.js

1
2
3
4
5
6
7
8
9
10
import request from '@/utils/request'

export function getUserList(params) {
return request({
url: '/vue-admin-template/list/myUser',
method: 'get',
params
})
}

列表和表单页面

新建 src\views\list-and-form\list.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
<template>
<div class="app-container">
<!-- 搜索区域 -->
<el-form :inline="true" size="mini" class="filter-container">
<el-form-item>
<el-input v-model="pageData.search" placeholder="请输入搜索内容..." size="mini">
<el-button slot="append" icon="el-icon-search" @click="onSearch" />
</el-input>
</el-form-item>
</el-form>

<!-- 列表 -->
<el-table
ref="singleTable"
v-loading="isloading"
:data="pageData.data"
:border="true"
:highlight-current-row="true"
:size="'mini'"
row-key="id"
:header-cell-style="{background:'#f5f7fa'}"
:height="`calc(100vh - ` + bottomOffset + `px)`"
:default-sort="{prop: 'id', order: 'descending'}"
element-loading-background="rgba(255, 255, 255, .85)"
element-loading-text="拼命加载中..."
element-loading-spinner="el-icon-loading"
@sort-change="sortChange"
>
<template>
<el-table-column type="index" label="序号" align="center" width="50" :resizable="false" />
<el-table-column prop="row" label="流水号" width="80" :show-overflow-tooltip="true" />
<el-table-column prop="id" label="用户编号" width="180" :sortable="'true'" :show-overflow-tooltip="true" />
<el-table-column prop="username" label="姓名" width="80" :show-overflow-tooltip="true" />
<el-table-column prop="date" label="日期" align="center" :sortable="'true'" width="120" :show-overflow-tooltip="true" />
<el-table-column prop="email" label="邮箱" width="200" :show-overflow-tooltip="true" />
<el-table-column prop="ip" label="ip" width="140" :show-overflow-tooltip="true" />
<el-table-column prop="status" label="状态" width="80" :show-overflow-tooltip="true" />
<el-table-column prop="desc" label="描述" min-width="300" :show-overflow-tooltip="true" />

<el-table-column label="操作" width="100" class-name="small-padding fixed-width" fixed="right" :resizable="false">
<template slot-scope="{row,$index}">
<router-link type="primary" to="/list-and-form/form" target="_blank">
<el-link type="primary" :underline="false" style="font-size:12px">编辑</el-link>
</router-link>
<span v-html="'&nbsp;'" />
<el-link type="primary" :underline="false" style="font-size:12px" @click="del(row,$index)">删除</el-link>
<span v-html="'&nbsp;'" />
<router-link type="primary" to="/list-and-form/form" target="_blank">
<el-link type="primary" :underline="false" style="font-size:12px">详情</el-link>
</router-link>
</template>
</el-table-column>
</template>
</el-table>

<!-- 分页 -->
<el-pagination
:pager-count="7"
:layout="paginationInfo.layout"
:page-sizes="paginationInfo.pageSizes"
:background="paginationInfo.background"
:hidden="paginationInfo.hidden"
:disabled="paginationInfo.disabled"
:page-size="pageData.limit"
:current-page="pageData.page"
:total="pageData.total"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>

<script>
import { getUserList } from '@/api/myUser'
export default {
name: 'List',
data() {
return {
pageData: {
search: '',
orderByColumn: 'id',
orderBy: 'descending',
data: [],
page: 1,
limit: 20,
total: 0
},
paginationInfo: {
background: true,
layout: 'total, ->, sizes, prev, pager, next, jumper, slot',
pageSizes: [10, 20, 50, 100, 200],
hidden: false,
disabled: false
},
bottomOffset: 185, // 列表铺满全屏时其他容器高度
isloading: false // 是否显示加载动画
}
},
updated() {
this.initRefreshBtn()
},
created() {
this.getList()
},
methods: {
initRefreshBtn() {
const rightFixedPatch = this.$refs.singleTable.$refs.rightFixedPatch
rightFixedPatch.style.lineHeight = '47px'
rightFixedPatch.style.textAlign = 'center'
rightFixedPatch.style.cursor = 'pointer'
rightFixedPatch.innerHTML = '<i class="el-icon-refresh" title="刷新"></i>'
this.$nextTick(() => { rightFixedPatch.addEventListener('click', this.getList) })
},
sortChange(column) {
this.pageData.orderByColumn = column.prop
this.pageData.orderBy = column.order
this.pageData.page = 1
this.getList()
},
onSearch() {
this.pageData.page = 1
this.getList()
},
getList() {
const me = this
me.isloading = true
me.paginationInfo.disabled = true

getUserList({
page: me.pageData.page,
limit: me.pageData.limit,
search: me.pageData.search,
orderBy: me.pageData.orderBy,
orderByColumn: me.pageData.orderByColumn
}).then(function(response) {
const data = response.data
setTimeout(() => {
me.pageData.total = data.total
me.pageData.data = data.items

me.isloading = !true
me.paginationInfo.disabled = !true
}, 500)
})
},
handleSizeChange(val) {
this.pageData.limit = val
this.getList()
},
handleCurrentChange(val) {
this.pageData.page = val
this.getList()
}
}
}
</script>

<style>
.filter-container{
height: 40px;
}
.filter-container .el-input {
width: 280px !important;
}

.el-pagination {
padding: 15px 15px 0px 15px;
}
.el-pagination.hidden {
display: none;
}

.el-table__body {
width: 100% !important;
}

</style>

新建 src\components\AppGrid\index.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
<template>
<div>
<!-- 搜索区域 -->
<div class="filter-container">
<el-input v-model="pageData.search" placeholder="请输入搜索内容..." size="mini" style="width:360px">
<el-button slot="append" icon="el-icon-search" @click="onSearch" />
</el-input>
</div>

<!-- 列表 -->
<el-table
ref="singleTable"
v-loading="pageData.loading"
row-key="id"
:data="pageData.data"
:border="true"
:size="'mini'"
:highlight-current-row="true"
:header-cell-style="{background:'#f5f7fa'}"
:height="`calc(100vh - ` + bottomOffset + `px)`"
element-loading-text="拼命加载中"
@sort-change="sortChange"
@header-dragend="headerDragend"
>
<slot />
</el-table>

<!-- 分页 -->
<el-pagination
ref="singlePagination"
:pager-count="7"
:background="true"
:layout="'total, ->, sizes, prev, pager, next, jumper, slot'"
:page-sizes="[10, 20, 50, 100, 200]"
:page-size="pageData.limit"
:current-page="pageData.page"
:total="pageData.total"
:disabled="pageData.loading"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>

<script>
export default {
name: 'AppGrid',
props: {
bottomOffset: {
type: Number,
default: 185
},
getData: {
type: Function,
default: function() {},
require: true
},
limit: {
type: Number,
default: 20
},
searchBy: {
type: Array,
default: new Array(0),
require: true
}
},
data() {
return {
pageData: {
loading: false,
search: '',
data: [],
page: 1,
limit: this.limit,
total: 0
}
}
},
updated() { this.initRefreshBtn() },
created() { this._getData() },
methods: {
onSearch() {
this._getData()
},
sortChange() {
},
handleSizeChange(val) {
this.pageData.limit = val
this._getData()
},
handleCurrentChange(val) {
this.pageData.page = val
this._getData()
},
_getData() {
this.pageData.loading = true
const param = (({ search, page, limit }) => ({ search, page, limit }))(this.pageData)
this.getData(param).then((res) => {
this.pageData.data = res.data
this.pageData.total = res.total
this.pageData.loading = !true
})
},
initRefreshBtn() {
return
// const rightFixedPatch = this.$refs.singleTable.$refs.rightFixedPatch
// rightFixedPatch.style.lineHeight = '47px'
// rightFixedPatch.style.textAlign = 'center'
// rightFixedPatch.style.cursor = 'pointer'
// rightFixedPatch.innerHTML = '<i class="el-icon-refresh" style="font-weight:900" title="刷新"></i>'
// this.$nextTick(() => { rightFixedPatch.addEventListener('click', this._getData) })
},
headerDragend() {
this.$nextTick(() => {
// 对 Table 进行重新布局。当 Table 或其祖先元素由隐藏切换为显示时,可能需要调用此方法
this.$refs.singleTable.doLayout()
})
}
}
}
</script>

<style scoped>
.filter-container{
height: 40px;
}
.filter-container .el-input {
width: 280px !important;
}

.el-pagination {
padding: 15px 15px 0px 15px;
}
.el-pagination.hidden {
display: none;
}

.el-table__body {
width: 100% !important;
}
</style>

新建 src\views\list-and-form\list1.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
<template>
<div class="app-container">
<appGrid ref="appGrid1" :search-by="[]" :get-data="getData">
<template>
<el-table-column type="index" label="序号" align="center" width="50" :resizable="false" />
<el-table-column prop="row" label="流水号" width="80" :show-overflow-tooltip="true" />
<el-table-column prop="id" label="用户编号" width="180" :sortable="'true'" :show-overflow-tooltip="true" />
<el-table-column prop="username" label="姓名" width="80" :show-overflow-tooltip="true" />
<el-table-column prop="date" label="日期" align="center" :sortable="'true'" width="120" :show-overflow-tooltip="true" />
<el-table-column prop="email" label="邮箱" width="200" :show-overflow-tooltip="true" />
<el-table-column prop="ip" label="ip" width="140" :show-overflow-tooltip="true" />
<el-table-column prop="status" label="状态" width="80" :show-overflow-tooltip="true" />
<el-table-column prop="desc" label="描述" min-width="600" :show-overflow-tooltip="true" />

<el-table-column label="操作" width="100" class-name="small-padding fixed-width" fixed="right" :resizable="false">
<template slot-scope="{row,$index}">
<router-link type="primary" to="/list-and-form/form" target="_blank">
<el-link type="primary" :underline="false" style="font-size:12px">编辑</el-link>
</router-link>
<span v-html="'&nbsp;'" />
<el-link type="primary" :underline="false" style="font-size:12px" @click="del(row,$index)">删除</el-link>
<span v-html="'&nbsp;'" />
<router-link type="primary" to="/list-and-form/form" target="_blank">
<el-link type="primary" :underline="false" style="font-size:12px">详情</el-link>
</router-link>
</template>
</el-table-column>
</template>
</appGrid>
</div>
</template>

<script>
import appGrid from '@/components/AppGrid'
import { getUserList } from '@/api/myUser'
export default {
name: 'List1',
components: { appGrid },
data() {
return {

}
},
mounted() {
this.$nextTick(function() {
// 仅在整个视图都被渲染之后才会运行的代码
})
},
methods: {
getData1(search, page, limit) {
return new Promise(function(resolve, reject) {
getUserList({ page, limit, search }).then(function(response) {
const data = response.data
setTimeout(() => {
resolve(data)
}, 500)
})
})
},
async getData(param) {
const data = (await getUserList(param)).data
return new Promise(function(resolve, reject) {
setTimeout(() => {
resolve(data)
}, 500)
})
}
}
}
</script>

<style>
</style>

新建 src\views\list-and-form\form.vue