@ddwl/ddwl-ui 1.1.3 → 1.1.5-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,458 +1,458 @@
1
- <!-- 表格 -->
2
- <template>
3
- <div
4
- :key="key"
5
- v-loading="loading"
6
- class="d-table"
7
- :class="draggable && 'drag-query-table'"
8
- >
9
- <el-table
10
- ref="table"
11
- :row-key="rowKey"
12
- style="width: 100%"
13
- :data="list"
14
- :border="border"
15
- v-bind="$attrs"
16
- v-on="$listeners"
17
- @select="selectChange"
18
- @select-all="selectAllChange"
19
- @selection-change="selectionChange"
20
- >
21
- <el-table-column
22
- v-if="selection || asyncSelection"
23
- key="selection"
24
- type="selection"
25
- align="center"
26
- width="50"
27
- />
28
- <el-table-column
29
- v-if="index"
30
- key="index"
31
- label="序号"
32
- align="center"
33
- width="55"
34
- >
35
- <template slot-scope="scope">
36
- {{ (pageNum - 1) * pageSize + scope.$index + 1 }}
37
- </template>
38
- </el-table-column>
39
- <el-table-column
40
- v-for="column in visibleColumns"
41
- :key="column.prop"
42
- v-bind="column"
43
- v-on="$listeners"
44
- >
45
- <template slot-scope="scope">
46
- <component
47
- :is="column.component"
48
- v-if="column.component"
49
- v-bind="{ ...scope.row, ...column }"
50
- :row="scope.row"
51
- :index="scope.$index"
52
- />
53
- <column-render
54
- v-else-if="column.render"
55
- :scope="scope"
56
- :render="column.render"
57
- />
58
- <template v-else>
59
- {{ scope.row[column.prop] }}
60
- </template>
61
- </template>
62
- </el-table-column>
63
- <el-table-column
64
- v-if="draggable"
65
- label=""
66
- width="55"
67
- align="center"
68
- >
69
- <img
70
- class="table-drag-icon"
71
- src="./drag.png"
72
- >
73
- </el-table-column>
74
- </el-table>
75
- <!-- 分页器 -->
76
- <div
77
- v-if="pagination"
78
- class="d-table-footer"
79
- >
80
- <div>
81
- <slot name="footer" />
82
- </div>
83
- <el-pagination
84
- class="pagination"
85
- :background="background"
86
- :layout="layout"
87
- :total="total"
88
- :page-sizes="pageSizes"
89
- :page-size="pageSize"
90
- :current-page="pageNum"
91
- @size-change="handleSizeChange"
92
- @current-change="handleCurrentChange"
93
- />
94
- </div>
95
- </div>
96
- </template>
97
-
98
- <script>
99
- import Sortable from 'sortablejs'
100
- import { cloneDeep } from 'lodash'
101
- import modules from '../../lib/slots'
102
- import ColumnRender from '../render'
103
-
104
- export default {
105
- name: 'DTable',
106
- components: { ...modules, ColumnRender },
107
- props: {
108
- data: {
109
- type: Array,
110
- default: () => []
111
- },
112
- rowKey: {
113
- type: String,
114
- default: ''
115
- },
116
- border: {
117
- type: Boolean,
118
- default: true
119
- },
120
- // request config
121
- requestConfig: {
122
- type: Object,
123
- require: false,
124
- default: () => ({
125
- api: () => {}, // 接口
126
- params: {}, // 默认参数
127
- autoRequest: true, // 是否自动请求
128
- method: null // 自定义请求方法
129
- })
130
- },
131
- layout: {
132
- type: String,
133
- default: 'total, sizes, prev, pager, next, jumper'
134
- },
135
- pageSizes: {
136
- type: Array,
137
- default: () => ([10, 20, 30, 50])
138
- },
139
- size: {
140
- type: Number,
141
- default: 10
142
- },
143
- page: {
144
- type: Number,
145
- default: 1
146
- },
147
- background: {
148
- type: Boolean,
149
- default: true
150
- },
151
- // 是否需要分页
152
- pagination: {
153
- default: true,
154
- type: Boolean
155
- },
156
- // 是否拖拽
157
- draggable: {
158
- default: false,
159
- type: Boolean
160
- },
161
- // 是否展示多选框
162
- selection: {
163
- default: false,
164
- type: Boolean
165
- },
166
- // 是否跨页勾选
167
- asyncSelection: {
168
- default: false,
169
- type: Boolean
170
- },
171
- // 默认勾选项,必须设置rowKey
172
- defaultCheckList: {
173
- default: () => [],
174
- type: Array
175
- },
176
- // 是否展示排序列
177
- index: {
178
- default: false,
179
- type: Boolean
180
- },
181
- // 列表项配置
182
- columns: {
183
- default: () => [],
184
- type: Array
185
- },
186
- // 开启静态分页
187
- staticPaging: {
188
- default: false,
189
- type: Boolean
190
- }
191
- },
192
- data () {
193
- return {
194
- pageSize: 10,
195
- pageNum: 1,
196
- total: 0,
197
- loading: false,
198
- checkedList: [],
199
- ids: [],
200
- key: 0,
201
- list: [],
202
- totalList: []
203
- }
204
- },
205
- computed: {
206
- visibleColumns () {
207
- return this.columns.filter(item => !item.hide)
208
- }
209
- },
210
- watch: {
211
- data: {
212
- handler (val) {
213
- if (JSON.stringify(val) === JSON.stringify(this.list)) {
214
- return
215
- }
216
- this.handleRequestData(val)
217
- if (this.draggable) {
218
- this.$nextTick(() => {
219
- this.initSortable()
220
- })
221
- }
222
- },
223
- deep: true,
224
- immediate: true
225
- },
226
- defaultCheckList: {
227
- handler () {
228
- this.handleSelectionData()
229
- },
230
- deep: true
231
- }
232
- },
233
- mounted () {
234
- this.checkedList = this.defaultCheckList
235
- // 如果不需要分页器,每页显示条目数设置最大
236
- if (!this.pagination) {
237
- this.pageSize = 9999999
238
- }
239
- // 注册表格拖拽
240
- if (this.draggable) {
241
- this.initSortable()
242
- }
243
- // 是否开启自动请求
244
- if (!this.staticPaging && (this.requestConfig.autoRequest || this.requestConfig.autoRequest === undefined)) {
245
- this.search()
246
- }
247
- },
248
- methods: {
249
- /**
250
- * @description: 表格查询方法包装
251
- * @param {*} param 自定义查询条件
252
- */
253
- async search (param = {}) {
254
- this.loading = true
255
- try {
256
- if (param.pageNum) {
257
- this.pageNum = param.pageNum
258
- }
259
- if (param.pageSize) {
260
- this.pageSize = param.pageSize
261
- }
262
- const { total, data } = await (this.requestConfig.method || this.getData)({
263
- pageSize: this.pageSize,
264
- pageNum: this.pageNum,
265
- ...this.requestConfig.params,
266
- ...param
267
- })
268
- this.total = total
269
- this.handleRequestData(data)
270
- // eslint-disable-next-line no-empty
271
- } catch (e) {}
272
- this.loading = false
273
- },
274
- async getData (params) {
275
- const defaultParams = typeof this.requestConfig.params === 'function' ? this.requestConfig.params() : this.requestConfig.params
276
- const { data } = await this.requestConfig.api({ ...defaultParams, ...params })
277
- this.total = data.total
278
- let listData = data.list || []
279
- if (this.requestConfig.props) {
280
- if (this.requestConfig.props.list === null) {
281
- listData = data
282
- } else if (this.requestConfig.props.list) {
283
- listData = data[this.requestConfig.props.list]
284
- }
285
- }
286
- return {
287
- total: data.total,
288
- data: listData
289
- }
290
- },
291
- /**
292
- * @description: 处理表格接口获取的数据
293
- */
294
- handleRequestData (data) {
295
- if (this.staticPaging) {
296
- this.totalList = cloneDeep(data)
297
- this.total = this.totalList.length
298
- } else {
299
- this.list = cloneDeep(data)
300
- }
301
-
302
- // 静态分页
303
- if (this.staticPaging) {
304
- this.pageNum = 1
305
- this.calcStaticPageData()
306
- return
307
- }
308
-
309
- this.$emit('loaded', this.list)
310
-
311
- // 如果当前页码大于1且当前数据列表为空,自动返回上一页并查询
312
- if (this.list.length === 0 && this.pageNum > 1) {
313
- this.pageNum = this.pageNum - 1
314
- this.search()
315
- return
316
- }
317
- // 跨页勾选逻辑
318
- if (this.asyncSelection || this.selection) {
319
- this.key++
320
- this.handleSelectionData()
321
- }
322
- },
323
- /**
324
- * @description: 处理表格默认勾选
325
- */
326
- handleSelectionData () {
327
- this.checkedList = this.defaultCheckList
328
- this.$refs.table && this.$refs.table.clearSelection()
329
- if (this.checkedList.length) {
330
- const ids = this.checkedList.map(item => item[this.rowKey])
331
- const rows = this.list.filter(item => ids.includes(item[this.rowKey]))
332
- this.$nextTick(() => {
333
- rows.forEach(row => {
334
- this.$refs.table.toggleRowSelection(row, true)
335
- })
336
- })
337
- }
338
- },
339
- calcStaticPageData () {
340
- const _list = JSON.parse(JSON.stringify(this.totalList))
341
- this.list = _list.splice((this.pageNum - 1) * this.pageSize, this.pageSize)
342
- },
343
- handleSizeChange (val) {
344
- this.pageSize = val
345
- this.pageNum = 1
346
- this.staticPaging ? this.calcStaticPageData() : this.search()
347
- this.$emit('page-change', { size: this.pageSize, page: this.pageNum })
348
- },
349
- handleCurrentChange (val) {
350
- this.pageNum = val
351
- this.staticPaging ? this.calcStaticPageData() : this.search()
352
- this.$emit('page-change', { size: this.pageSize, page: this.pageNum })
353
- },
354
- /**
355
- * @description: 勾选改变事件
356
- * @param {Array} list
357
- * @return {Array}
358
- */
359
- selectChange (list, row) {
360
- if (!this.asyncSelection) {
361
- this.$emit('select-change', list)
362
- } else {
363
- if (list.some(i => i[this.rowKey] === row[this.rowKey])) {
364
- this.checkedList.push(row)
365
- } else {
366
- this.checkedList = this.checkedList.filter(item => item[this.rowKey] !== row[this.rowKey])
367
- }
368
- this.$emit('select-change', cloneDeep(this.checkedList))
369
- }
370
- },
371
- selectAllChange (list) {
372
- if (!this.asyncSelection) {
373
- this.$emit('select-change', list)
374
- } else {
375
- if (list.length) {
376
- const checkedKeys = this.checkedList.map(item => item[this.rowKey])
377
- const newData = list.filter(item => !checkedKeys.includes(item[this.rowKey]))
378
- this.checkedList = this.checkedList.concat(newData)
379
- } else {
380
- const listKeys = this.list.map(item => item[this.rowKey])
381
- this.checkedList = this.checkedList.filter(item => !listKeys.includes(item[this.rowKey]))
382
- }
383
- this.$emit('select-change', cloneDeep(this.checkedList))
384
- }
385
- },
386
-
387
- selectionChange () {},
388
- /**
389
- * @description: 清空用户的选择
390
- */
391
- clearSelection () {
392
- this.checkedList = []
393
- this.$refs.table.clearSelection()
394
- },
395
- /**
396
- * @description: 改变给定值的选中状态
397
- * @param {*} value 改变选中状态的值
398
- * @param {*} selected 是否选中
399
- */
400
- toggleAsyncSelection (value, selected = false) {
401
- this.checkedList = this.checkedList.filter(item => item[this.rowKey] !== value)
402
- const data = this.list.find(item => item[this.rowKey] === value)
403
- if (data) {
404
- this.$refs.table.toggleRowSelection(data, selected)
405
- } else {
406
- this.$emit('select-change', this.checkedList)
407
- }
408
- },
409
- /**
410
- * @description: 表格拖拽
411
- */
412
- initSortable () {
413
- const el = this.$refs.table.$el.querySelector('.el-table__body-wrapper > table > tbody')
414
- /* eslint-disable no-new */
415
- new Sortable(el, {
416
- handle: '.table-drag-icon',
417
- onEnd: (evt) => {
418
- // 拖拽后数据位置置换
419
- const oldList = JSON.parse(JSON.stringify(this.list))
420
- const data = oldList.splice(evt.oldIndex, 1)
421
- oldList.splice(evt.newIndex, 0, data[0])
422
- this.list = oldList
423
- // this.key++
424
- this.$nextTick(() => {
425
- // 改变key值table重新渲染导致拖拽功能消失,需要重新初始化sorable实例
426
- this.initSortable()
427
- this.handleSelectionData()
428
- this.$emit('drag-change', this.list, evt)
429
- })
430
- }
431
- })
432
- }
433
- }
434
- }
435
- </script>
436
-
437
- <style lang="scss" scoped>
438
- .d-table {
439
- display: flex;
440
- flex-direction: column;
441
- }
442
- .drag-query-table {
443
- :deep(.hover-row) {
444
- td {
445
- background-color: #fff;
446
- }
447
- }
448
- }
449
- .table-drag-icon {
450
- cursor: pointer;
451
- }
452
- .d-table-footer {
453
- display: flex;
454
- align-items: center;
455
- justify-content: space-between;
456
- margin-top: 16px;
457
- }
458
- </style>
1
+ <!-- 表格 -->
2
+ <template>
3
+ <div
4
+ :key="key"
5
+ v-loading="loading"
6
+ class="d-table"
7
+ :class="draggable && 'drag-query-table'"
8
+ >
9
+ <el-table
10
+ ref="table"
11
+ :row-key="rowKey"
12
+ style="width: 100%"
13
+ :data="list"
14
+ :border="border"
15
+ v-bind="$attrs"
16
+ v-on="$listeners"
17
+ @select="selectChange"
18
+ @select-all="selectAllChange"
19
+ @selection-change="selectionChange"
20
+ >
21
+ <el-table-column
22
+ v-if="selection || asyncSelection"
23
+ key="selection"
24
+ type="selection"
25
+ align="center"
26
+ width="50"
27
+ />
28
+ <el-table-column
29
+ v-if="index"
30
+ key="index"
31
+ label="序号"
32
+ align="center"
33
+ width="55"
34
+ >
35
+ <template slot-scope="scope">
36
+ {{ (pageNum - 1) * pageSize + scope.$index + 1 }}
37
+ </template>
38
+ </el-table-column>
39
+ <el-table-column
40
+ v-for="column in visibleColumns"
41
+ :key="column.prop"
42
+ v-bind="column"
43
+ v-on="$listeners"
44
+ >
45
+ <template slot-scope="scope">
46
+ <component
47
+ :is="column.component"
48
+ v-if="column.component"
49
+ v-bind="{ ...scope.row, ...column }"
50
+ :row="scope.row"
51
+ :index="scope.$index"
52
+ />
53
+ <column-render
54
+ v-else-if="column.render"
55
+ :scope="scope"
56
+ :render="column.render"
57
+ />
58
+ <template v-else>
59
+ {{ scope.row[column.prop] }}
60
+ </template>
61
+ </template>
62
+ </el-table-column>
63
+ <el-table-column
64
+ v-if="draggable"
65
+ label=""
66
+ width="55"
67
+ align="center"
68
+ >
69
+ <img
70
+ class="table-drag-icon"
71
+ src="./drag.png"
72
+ >
73
+ </el-table-column>
74
+ </el-table>
75
+ <!-- 分页器 -->
76
+ <div
77
+ v-if="pagination"
78
+ class="d-table-footer"
79
+ >
80
+ <div>
81
+ <slot name="footer" />
82
+ </div>
83
+ <el-pagination
84
+ class="pagination"
85
+ :background="background"
86
+ :layout="layout"
87
+ :total="total"
88
+ :page-sizes="pageSizes"
89
+ :page-size="pageSize"
90
+ :current-page="pageNum"
91
+ @size-change="handleSizeChange"
92
+ @current-change="handleCurrentChange"
93
+ />
94
+ </div>
95
+ </div>
96
+ </template>
97
+
98
+ <script>
99
+ import Sortable from 'sortablejs'
100
+ import { cloneDeep } from 'lodash'
101
+ import modules from '../../lib/slots'
102
+ import ColumnRender from '../render'
103
+
104
+ export default {
105
+ name: 'DTable',
106
+ components: { ...modules, ColumnRender },
107
+ props: {
108
+ data: {
109
+ type: Array,
110
+ default: () => []
111
+ },
112
+ rowKey: {
113
+ type: String,
114
+ default: ''
115
+ },
116
+ border: {
117
+ type: Boolean,
118
+ default: true
119
+ },
120
+ // request config
121
+ requestConfig: {
122
+ type: Object,
123
+ require: false,
124
+ default: () => ({
125
+ api: () => {}, // 接口
126
+ params: {}, // 默认参数
127
+ autoRequest: true, // 是否自动请求
128
+ method: null // 自定义请求方法
129
+ })
130
+ },
131
+ layout: {
132
+ type: String,
133
+ default: 'total, sizes, prev, pager, next, jumper'
134
+ },
135
+ pageSizes: {
136
+ type: Array,
137
+ default: () => ([10, 20, 30, 50])
138
+ },
139
+ size: {
140
+ type: Number,
141
+ default: 10
142
+ },
143
+ page: {
144
+ type: Number,
145
+ default: 1
146
+ },
147
+ background: {
148
+ type: Boolean,
149
+ default: true
150
+ },
151
+ // 是否需要分页
152
+ pagination: {
153
+ default: true,
154
+ type: Boolean
155
+ },
156
+ // 是否拖拽
157
+ draggable: {
158
+ default: false,
159
+ type: Boolean
160
+ },
161
+ // 是否展示多选框
162
+ selection: {
163
+ default: false,
164
+ type: Boolean
165
+ },
166
+ // 是否跨页勾选
167
+ asyncSelection: {
168
+ default: false,
169
+ type: Boolean
170
+ },
171
+ // 默认勾选项,必须设置rowKey
172
+ defaultCheckList: {
173
+ default: () => [],
174
+ type: Array
175
+ },
176
+ // 是否展示排序列
177
+ index: {
178
+ default: false,
179
+ type: Boolean
180
+ },
181
+ // 列表项配置
182
+ columns: {
183
+ default: () => [],
184
+ type: Array
185
+ },
186
+ // 开启静态分页
187
+ staticPaging: {
188
+ default: false,
189
+ type: Boolean
190
+ }
191
+ },
192
+ data () {
193
+ return {
194
+ pageSize: 10,
195
+ pageNum: 1,
196
+ total: 0,
197
+ loading: false,
198
+ checkedList: [],
199
+ ids: [],
200
+ key: 0,
201
+ list: [],
202
+ totalList: []
203
+ }
204
+ },
205
+ computed: {
206
+ visibleColumns () {
207
+ return this.columns.filter(item => !item.hide)
208
+ }
209
+ },
210
+ watch: {
211
+ data: {
212
+ handler (val) {
213
+ if (JSON.stringify(val) === JSON.stringify(this.list)) {
214
+ return
215
+ }
216
+ this.handleRequestData(val)
217
+ if (this.draggable) {
218
+ this.$nextTick(() => {
219
+ this.initSortable()
220
+ })
221
+ }
222
+ },
223
+ deep: true,
224
+ immediate: true
225
+ },
226
+ defaultCheckList: {
227
+ handler () {
228
+ this.handleSelectionData()
229
+ },
230
+ deep: true
231
+ }
232
+ },
233
+ mounted () {
234
+ this.checkedList = this.defaultCheckList
235
+ // 如果不需要分页器,每页显示条目数设置最大
236
+ if (!this.pagination) {
237
+ this.pageSize = 9999999
238
+ }
239
+ // 注册表格拖拽
240
+ if (this.draggable) {
241
+ this.initSortable()
242
+ }
243
+ // 是否开启自动请求
244
+ if (!this.staticPaging && (this.requestConfig.autoRequest || this.requestConfig.autoRequest === undefined)) {
245
+ this.search()
246
+ }
247
+ },
248
+ methods: {
249
+ /**
250
+ * @description: 表格查询方法包装
251
+ * @param {*} param 自定义查询条件
252
+ */
253
+ async search (param = {}) {
254
+ this.loading = true
255
+ try {
256
+ if (param.pageNum) {
257
+ this.pageNum = param.pageNum
258
+ }
259
+ if (param.pageSize) {
260
+ this.pageSize = param.pageSize
261
+ }
262
+ const { total, data } = await (this.requestConfig.method || this.getData)({
263
+ pageSize: this.pageSize,
264
+ pageNum: this.pageNum,
265
+ ...this.requestConfig.params,
266
+ ...param
267
+ })
268
+ this.total = total
269
+ this.handleRequestData(data)
270
+ // eslint-disable-next-line no-empty
271
+ } catch (e) {}
272
+ this.loading = false
273
+ },
274
+ async getData (params) {
275
+ const defaultParams = typeof this.requestConfig.params === 'function' ? this.requestConfig.params() : this.requestConfig.params
276
+ const { data } = await this.requestConfig.api({ ...defaultParams, ...params })
277
+ this.total = data.total
278
+ let listData = data.list || []
279
+ if (this.requestConfig.props) {
280
+ if (this.requestConfig.props.list === null) {
281
+ listData = data
282
+ } else if (this.requestConfig.props.list) {
283
+ listData = data[this.requestConfig.props.list]
284
+ }
285
+ }
286
+ return {
287
+ total: data.total,
288
+ data: listData
289
+ }
290
+ },
291
+ /**
292
+ * @description: 处理表格接口获取的数据
293
+ */
294
+ handleRequestData (data) {
295
+ if (this.staticPaging) {
296
+ this.totalList = cloneDeep(data)
297
+ this.total = this.totalList.length
298
+ } else {
299
+ this.list = cloneDeep(data)
300
+ }
301
+
302
+ // 静态分页
303
+ if (this.staticPaging) {
304
+ this.pageNum = 1
305
+ this.calcStaticPageData()
306
+ return
307
+ }
308
+
309
+ this.$emit('loaded', this.list)
310
+
311
+ // 如果当前页码大于1且当前数据列表为空,自动返回上一页并查询
312
+ if (this.list.length === 0 && this.pageNum > 1) {
313
+ this.pageNum = this.pageNum - 1
314
+ this.search()
315
+ return
316
+ }
317
+ // 跨页勾选逻辑
318
+ if (this.asyncSelection || this.selection) {
319
+ this.key++
320
+ this.handleSelectionData()
321
+ }
322
+ },
323
+ /**
324
+ * @description: 处理表格默认勾选
325
+ */
326
+ handleSelectionData () {
327
+ this.checkedList = this.defaultCheckList
328
+ this.$refs.table && this.$refs.table.clearSelection()
329
+ if (this.checkedList.length) {
330
+ const ids = this.checkedList.map(item => item[this.rowKey])
331
+ const rows = this.list.filter(item => ids.includes(item[this.rowKey]))
332
+ this.$nextTick(() => {
333
+ rows.forEach(row => {
334
+ this.$refs.table.toggleRowSelection(row, true)
335
+ })
336
+ })
337
+ }
338
+ },
339
+ calcStaticPageData () {
340
+ const _list = JSON.parse(JSON.stringify(this.totalList))
341
+ this.list = _list.splice((this.pageNum - 1) * this.pageSize, this.pageSize)
342
+ },
343
+ handleSizeChange (val) {
344
+ this.pageSize = val
345
+ this.pageNum = 1
346
+ this.staticPaging ? this.calcStaticPageData() : this.search()
347
+ this.$emit('page-change', { size: this.pageSize, page: this.pageNum })
348
+ },
349
+ handleCurrentChange (val) {
350
+ this.pageNum = val
351
+ this.staticPaging ? this.calcStaticPageData() : this.search()
352
+ this.$emit('page-change', { size: this.pageSize, page: this.pageNum })
353
+ },
354
+ /**
355
+ * @description: 勾选改变事件
356
+ * @param {Array} list
357
+ * @return {Array}
358
+ */
359
+ selectChange (list, row) {
360
+ if (!this.asyncSelection) {
361
+ this.$emit('select-change', list)
362
+ } else {
363
+ if (list.some(i => i[this.rowKey] === row[this.rowKey])) {
364
+ this.checkedList.push(row)
365
+ } else {
366
+ this.checkedList = this.checkedList.filter(item => item[this.rowKey] !== row[this.rowKey])
367
+ }
368
+ this.$emit('select-change', cloneDeep(this.checkedList))
369
+ }
370
+ },
371
+ selectAllChange (list) {
372
+ if (!this.asyncSelection) {
373
+ this.$emit('select-change', list)
374
+ } else {
375
+ if (list.length) {
376
+ const checkedKeys = this.checkedList.map(item => item[this.rowKey])
377
+ const newData = list.filter(item => !checkedKeys.includes(item[this.rowKey]))
378
+ this.checkedList = this.checkedList.concat(newData)
379
+ } else {
380
+ const listKeys = this.list.map(item => item[this.rowKey])
381
+ this.checkedList = this.checkedList.filter(item => !listKeys.includes(item[this.rowKey]))
382
+ }
383
+ this.$emit('select-change', cloneDeep(this.checkedList))
384
+ }
385
+ },
386
+
387
+ selectionChange () {},
388
+ /**
389
+ * @description: 清空用户的选择
390
+ */
391
+ clearSelection () {
392
+ this.checkedList = []
393
+ this.$refs.table.clearSelection()
394
+ },
395
+ /**
396
+ * @description: 改变给定值的选中状态
397
+ * @param {*} value 改变选中状态的值
398
+ * @param {*} selected 是否选中
399
+ */
400
+ toggleAsyncSelection (value, selected = false) {
401
+ this.checkedList = this.checkedList.filter(item => item[this.rowKey] !== value)
402
+ const data = this.list.find(item => item[this.rowKey] === value)
403
+ if (data) {
404
+ this.$refs.table.toggleRowSelection(data, selected)
405
+ } else {
406
+ this.$emit('select-change', this.checkedList)
407
+ }
408
+ },
409
+ /**
410
+ * @description: 表格拖拽
411
+ */
412
+ initSortable () {
413
+ const el = this.$refs.table.$el.querySelector('.el-table__body-wrapper > table > tbody')
414
+ /* eslint-disable no-new */
415
+ new Sortable(el, {
416
+ handle: '.table-drag-icon',
417
+ onEnd: (evt) => {
418
+ // 拖拽后数据位置置换
419
+ const oldList = JSON.parse(JSON.stringify(this.list))
420
+ const data = oldList.splice(evt.oldIndex, 1)
421
+ oldList.splice(evt.newIndex, 0, data[0])
422
+ this.list = oldList
423
+ // this.key++
424
+ this.$nextTick(() => {
425
+ // 改变key值table重新渲染导致拖拽功能消失,需要重新初始化sorable实例
426
+ this.initSortable()
427
+ this.handleSelectionData()
428
+ this.$emit('drag-change', this.list, evt)
429
+ })
430
+ }
431
+ })
432
+ }
433
+ }
434
+ }
435
+ </script>
436
+
437
+ <style lang="scss" scoped>
438
+ .d-table {
439
+ display: flex;
440
+ flex-direction: column;
441
+ }
442
+ .drag-query-table {
443
+ :deep(.hover-row) {
444
+ td {
445
+ background-color: #fff;
446
+ }
447
+ }
448
+ }
449
+ .table-drag-icon {
450
+ cursor: pointer;
451
+ }
452
+ .d-table-footer {
453
+ display: flex;
454
+ align-items: center;
455
+ justify-content: space-between;
456
+ margin-top: 16px;
457
+ }
458
+ </style>