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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<template>
<div class="app-container calendar-list-container">
<!-- 过滤 -->
<div class="filter-container">
<!-- 过滤条件 -->
<span v-for="filter in filters" class="hm-complex-table__filter-span">
<el-input @keyup.enter.native="handleFilter"
style="width: 200px;"
class="filter-item"
:placeholder="filter.placeholder"
v-model="listQuery.filters[schema['modelUnderscore']][getFilterColumn(filter)][getFilterOper(filter)]">
</el-input>
</span>
<!-- end 过滤条件 -->
<el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
<el-button class="filter-item" type="primary" :loading="downloadLoading" v-waves icon="el-icon-download" @click="handleDownload">导出</el-button>
</div>
<!-- end 过滤 -->
<!-- 表格 -->
<el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
style="width: 100%">
<el-table-column v-for="(column,index) in showColumns" :key="index" align="center" :label="column.name">
<template slot-scope="scope">
<span>{{ scope.row[column.code] }}</span>
</template>
</el-table-column>
</el-table>
<!-- end 表格 -->
<!-- 翻页 -->
<div class="pagination-container">
<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page_no"
:page-sizes="[10,20,50]" :page-size="listQuery.page_size" layout="total, sizes, prev, pager, next, jumper" :total="total">
</el-pagination>
</div>
<!-- end翻页 -->
<!-- 弹窗 -->
<!-- @TODO 补充弹窗 -->
<!-- end 弹窗 -->
</div>
</template>
<script>
import _ from 'lodash'
import request from '@/utils/request'
import waves from '@/directive/waves' // 水波纹指令
import { parseTime } from '@/utils'
import * as excel from '@/vendor/Export2Excel'
import { Button, Table, TableColumn, Pagination, Loading } from 'element-ui'
/**
* 毫末科技的表格组件.
*
* demo地址: factory.haomo-studio.com/vue-element/#/haomo/components/table
* @author 胡小根
*/
export default {
name: 'HmComplexTable',
// 集成其他组件
extends: {
},
// 使用其它组件
components: {
'el-button': Button,
'el-table': Table,
'el-table-column': TableColumn,
'el-pagination': Pagination
},
// 混入公共对象
mixins: [],
props: {
/**
* 组件所使用的表定义schema。表定义schema,请使用 model2codejs 从pdm文件生成schema。
* 对于所有毫末科技的组件,必须传schema,以完成数据的交互
*/
schema: {
type: Object,
required: true
},
/**
* 搜索过滤选项。默认没有过滤功能。完整的示例为:
* {
* "column1": {
* like: '%abc%', 模糊查询,包含字符”abc”
* notLike: '' 模糊查询,不包含字符
* between: [1, 10], 取值在[1,10]之间,包含1与10
* notBetween: [1, 10], 取值小于1大于10
* isNull: true, // 只能为true 判断字段是否为空
* isNotNull: true, // 只能为true 判断字段是否不为空
* equalTo: "abc", 相等于
* notEqualTo: "abc", 不等于
* greaterThan: 10, 大于
* greaterThanOrEqualTo: Í10, 大于等于
* lessThan: 10, 小于
* lessThanOrEqualTo: 10, 小于等于
* in: [], 包含[]中字段
* notIn: [] 不包含[]中字段
* }
* }
*/
filters: {
type: Array,
required: false
},
/**
* 指定要显示的列。默认为根据schema得到的所有列。完整示例为:
* [
* {
* "name": "姓名",
* "code": "username",
* "render": function(value){
* return "<a href='value'></a>"
* }
* },
* "mobile",
* "sexual"
* ]
*/
columns: {
type: Array,
required: false,
validator: function(value) {
if (typeof value !== 'object') {
console.warn(`传入的columns不符合要求,必须是数组`)
return false
}
return true
}
},
/**
* 修改行数据的Hook函数。参数为行数据 rowData
*/
rowHook: {
type: Function,
required: false
},
/**
* 表格的选项,包括:page_size。完整的示例为:
* {
* "page_size": 10, // 默认为10条数据/页
* "showExport": false, // 默认为不显示导出按钮
* "sort_item": "create_time", // 默认为create_time字段的desc排序
"sort_order": "desc"
* }
*/
options: {
type: Object,
required: false
}
},
directives: {
waves,
Loading
},
data() {
return {
list: null,
total: null,
listLoading: true,
listQuery: {
page_no: 1,
page_size: 20,
sort_item: 'create_time',
sort_order: 'desc',
filters: {}
},
downloadLoading: false,
showColumns: [] // 要显示的列数据
}
},
computed: {
filterParams: function() {
const self = this
const ret = JSON.parse(JSON.stringify(self.listQuery.filters))
if (!ret) {
return ret
}
if (!ret[self.schema['modelUnderscore']]) {
return ret
}
_.each(Object.keys(ret[self.schema['modelUnderscore']]), function(column) {
const operValue = ret[self.schema['modelUnderscore']][column]
if (Object.keys(operValue)[0] === 'like') {
ret[self.schema['modelUnderscore']][column]['like'] = '%' + ret[self.schema['modelUnderscore']][column]['like'] + '%'
}
})
return ret
}
},
created() {
// this.validate()
this.init()
this.getList()
},
methods: {
validate() {
const self = this
// this.columns数组元素本身必须是string或者object. 且必须是schema中定义的列
// 由于vue中不允许通过其他的props来验证当前props,只能在created里进行调用
_.each(self.columns, function(item) {
if (!item) {
return 0
}
if (typeof item !== 'string' && typeof item !== 'object') {
console.error(`传入的columns不符合要求,数组元素必须是字符串或对象`)
}
if (typeof item === 'string' && !_.keyBy(self.schema['columns'], 'code')[item.toUpperCase()]) {
console.error(`传入的columns不符合要求,字符串元素[${item}]必须是schema中定义的列[code]`)
}
if (typeof item === 'object' && !_.keyBy(self.schema['columns'], 'code')[item['code'].toUpperCase()]) {
console.error(`传入的columns不符合要求,元素的code属性[${item['code']}]必须是schema中定义的列[code]`)
}
})
},
init() {
const self = this
// 处理要显示的列
if (!self.columns || !self.columns.length) {
_.each(self.schema['columns'], function(column) {
const tmp = JSON.parse(JSON.stringify(column))
self.$set(tmp, 'code', tmp.code.toLowerCase())
self.showColumns.push(tmp)
})
console.log(self.showColumns)
} else {
self.showColumns = JSON.parse(JSON.stringify(self.columns))
console.log('1111111')
console.log(self.showColumns)
// 将字符串对象进行替换处理
_.each(self.showColumns, function(column, index) {
if (typeof column === 'string') {
const tmp = _.keyBy(self.schema['columns'], 'code')[column.toUpperCase()]
console.log(tmp)
self.$set(tmp, 'code', tmp.code.toLowerCase())
self.$set(self.showColumns, index, tmp)
}
})
console.log('2222222')
console.log(self.showColumns)
}
// 处理过滤条件
if (self.filters) {
const tableName = self.schema['modelUnderscore']
const filters = {}
filters[tableName] = {}
_.each(self.filters, function(filter) {
filters[tableName] = Object.assign(filters[tableName], filter)
})
delete filters[tableName]['placeholder']
self.$set(self.listQuery, 'filters', filters)
}
if (!request.defaults.baseURL) {
request.defaults.baseURL = '/org/api'
}
console.log(request.defaults)
console.log(`request.defaults.baseURL: ${request.defaults.baseURL}`)
},
getList() {
const self = this
self.listLoading = true
// 处理过滤条件
const params = JSON.parse(JSON.stringify(self.listQuery))
params.filters = self.filterParams
request(self.schema.modelUnderscorePlural, {
params: params
}).then(resp => {
self.list = resp.data
self.total = parseInt(resp.headers.total)
self.listLoading = false
})
},
handleFilter() {
this.getList()
},
handleSizeChange(val) {
this.listQuery.page_size = val
this.getList()
},
handleCurrentChange(val) {
this.listQuery.page_no = val
this.getList()
},
handleDelete(row) {
this.$notify({
title: '成功',
message: '删除成功',
type: 'success',
duration: 2000
})
const index = this.list.indexOf(row)
this.list.splice(index, 1)
},
handleDownload() {
this.downloadLoading = true
// @TODO 修改下载excel的功能,请求所有的数据
const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
const data = this.formatJson(filterVal, this.list)
excel.export_json_to_excel(tHeader, data, 'table-list')
this.downloadLoading = false
},
formatJson(filterVal, jsonData) {
return jsonData.map(v => filterVal.map(j => {
if (j === 'timestamp') {
return parseTime(v[j])
} else {
return v[j]
}
}))
},
getFilterColumn(filter) {
const keys = Object.keys(filter)
let column = null
_.each(keys, function(key) {
if (key !== 'placeholder') {
column = key
return 0
}
})
return column.toLowerCase()
},
getFilterOper(filter) {
return Object.keys(filter[this.getFilterColumn(filter)])[0]
}
}
}
</script>
<style>
.hm-complex-table__filter-span {
margin-right: 5px;
}
</style>