@ddwl/ddwl-ui 1.1.5-beta.4 → 1.1.6

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