@netang/quasar 0.2.54 → 0.2.57

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.
package/utils/$table.js CHANGED
@@ -1,1364 +1,1394 @@
1
- import { ref, computed, provide, inject, watch } from 'vue'
2
- import { useQuasar } from 'quasar'
3
-
4
- import $n_has from 'lodash/has'
5
- import $n_get from 'lodash/get'
6
- import $n_cloneDeep from 'lodash/cloneDeep'
7
- import $n_merge from 'lodash/merge'
8
- import $n_isFunction from 'lodash/isFunction'
9
- import $n_findIndex from 'lodash/findIndex'
10
- import $n_uniq from 'lodash/uniq'
11
- import $n_concat from 'lodash/concat'
12
- import $n_isNil from 'lodash/isNil'
13
- import $n_isArray from 'lodash/isArray'
14
-
15
- import $n_router from '@netang/utils/vue/router'
16
-
17
- import $n_forEach from '@netang/utils/forEach'
18
- import $n_isValidArray from '@netang/utils/isValidArray'
19
- import $n_isValidString from '@netang/utils/isValidString'
20
- import $n_indexOf from '@netang/utils/indexOf'
21
- import $n_storage from '@netang/utils/storage'
22
-
23
- import $n_isRequired from '@netang/utils/isRequired'
24
- import $n_forIn from '@netang/utils/forIn'
25
- import $n_run from '@netang/utils/run'
26
- import $n_runAsync from '@netang/utils/runAsync'
27
- import $n_isValidObject from '@netang/utils/isValidObject'
28
- import $n_isValidValue from '@netang/utils/isValidValue'
29
- import $n_slash from '@netang/utils/slash'
30
- import $n_http from '@netang/utils/http'
31
- import $n_cb from '@netang/utils/cb'
32
-
33
- import $n_$power from './$power'
34
- import $n_dict from './dict'
35
- import $n_price from './price'
36
- import $n_getTime from './getTime'
37
-
38
- import { configs } from './config'
39
-
40
- import {
41
- // 设置单个搜索值
42
- setItemValue,
43
- // 从表格列获取原始值
44
- getRawData,
45
- // 获取参数
46
- getOptions,
47
- // 格式化值
48
- formatValue,
49
- } from './useSearch'
50
-
51
- import { NRenderKey, NPowerKey, NTableKey } from './symbols'
52
-
53
- const {
54
- // 表格配置
55
- tablesConfig,
56
- // 字典常量
57
- dicts,
58
- } = configs
59
-
60
- /**
61
- * 创建表格
62
- */
63
- function create(options) {
64
-
65
- // ==========【数据】=================================================================================================
66
-
67
- // quasar 对象
68
- const $q = useQuasar()
69
-
70
- // 每页显示行数选项
71
- const rowsPerPageOptions = [30, 40, 50, 100, 200, 500, 1000]
72
-
73
- // 原始参数
74
- const rawOptions = {
75
- // 路由路径
76
- path: '',
77
- // 请求地址(默认为 path)
78
- url: '',
79
- // 路由参数
80
- query: {},
81
- // 附加请求数据
82
- data: {},
83
- // 表格行唯一键值
84
- rowKey: 'id',
85
- // 选择类型, 可选值 none single(默认) multiple
86
- // selection: '',
87
- // 分隔栏, 可选值 horizontal vertical cell none
88
- separator: 'cell',
89
- // 初始已选数据
90
- selected: [],
91
- // 初始表格加载状态
92
- loading: false,
93
- // 表格列数据(对象数组)
94
- columns: [],
95
- // 初始可见列
96
- visibleColumns: [],
97
- // 表格行数据
98
- rows: [],
99
- // 表格翻页参数
100
- pagination: {
101
- // 页码
102
- page: 1,
103
- // 每页的数据条数
104
- rowsPerPage: $n_has(options, 'rowsPerPageOptions') ? options.rowsPerPageOptions[0] : rowsPerPageOptions[0],
105
- // 数据总数(服务器返回)
106
- rowsNumber: 1,
107
- // 排序字段
108
- sortBy: null,
109
- // sortBy: 'id',
110
- // 是否降序排列
111
- descending: true,
112
- },
113
- // 每页显示行数选项
114
- rowsPerPageOptions,
115
- // 请求前执行
116
- requestBefore: null,
117
- // 请求成功执行
118
- requestSuccess: null,
119
- // 请求失败执行
120
- requestFail: null,
121
- // 请求后执行
122
- requestAfter: null,
123
- // 自定义请求方法
124
- request: null,
125
- // 格式化单条数据
126
- formatRow: null,
127
- // http 设置
128
- httpSettings: {},
129
- // 是否开启初始搜素
130
- search: true,
131
- // 是否开启合计
132
- summary: false,
133
- // 从参数中获取搜索值
134
- searchFromQuery: true,
135
- // 是否显示宫格
136
- showGrid: false,
137
- // 是否显示可见列
138
- showVisibleColumns: true,
139
- // 是否开启缓存
140
- cache: true,
141
- // 是否刷新后清空已选数据
142
- refreshResetSelected: true,
143
-
144
- // 单击表格行事件
145
- rowClick: null,
146
- // 双击表格行事件
147
- rowDblClick: null,
148
- }
149
-
150
- let o
151
- let $power
152
- let hasPowr
153
- let $render
154
- let $route
155
- let hasPowerBtns
156
- let tableSelected
157
- let isCache
158
- let cacheName
159
- let tableColumns
160
- let tableImgs
161
-
162
- // 是否显示可见列
163
- let tableShowVisibleColumns
164
- // 表格可见列
165
- let tableVisibleColumns
166
- // 表格加载状态
167
- let tableLoading
168
- // 表格行数据
169
- let tableRows
170
- // 表格翻页参数
171
- let tablePagination
172
- // 表格宫格
173
- let tableGrid
174
- // 表格请求参数(将表格传参中的搜索参数剥离掉, 剩下的直接当做参数传递给服务器)
175
- let tableRequestQuery
176
- // 是否请求表格合计
177
- let isRequestSummary
178
- // 表格合计
179
- let tableSummary
180
- // 表格选择类型
181
- let tableSelection
182
- // 表格分隔栏
183
- let tableSeparator
184
-
185
- // 原始参数
186
- let rawQuery
187
- // 原始表格搜索参数
188
- let rawSearchOptions
189
- // 原始表格搜索值(空表格搜索值, 用于搜索重置)
190
- let rawTableSearchValue
191
- // 首次表格搜索值(如果表格搜索参数中带了初始值, 则设置初始值)
192
- let firstTableSearchValue
193
-
194
- // 表格搜索数据值
195
- let tableSearchValue
196
- // 表格搜索参数
197
- let tableSearchOptions
198
- // 是否已加载
199
- let _isTableLoaded
200
-
201
- // 是否已生成数据
202
- let _isCreated = false
203
-
204
- // 创建表格
205
- reCreate(options)
206
-
207
- // 已生成数据
208
- _isCreated = true
209
-
210
- // ==========【方法】================================================================================================
211
-
212
- /**
213
- * 重新创建表格
214
- */
215
- function reCreate(options) {
216
-
217
- // 获取参数
218
- o = $n_merge({}, rawOptions, options)
219
-
220
- // 获取权限注入
221
- $power = $n_has(options, '$power') ? options.$power : (_isCreated ? $power : inject(NPowerKey))
222
- hasPowr = !! $power
223
-
224
- // 获取渲染注入
225
- $render = $n_has(options, '$render') ? options.$render : (_isCreated ? $render : inject(NRenderKey))
226
- if (!! $render) {
227
- // 如果有表格传参, 则合并参数
228
- const tableProps = $n_get($render, 'props.tableProps')
229
- if ($n_isValidObject(tableProps)) {
230
- $n_merge(o, tableProps)
231
- }
232
- }
233
-
234
- // 获取选择类型(默认 single)
235
- if (! $n_has(o, 'selection') || ! $n_isValidString(o.selection)) {
236
- if (hasPowr) {
237
- o.selection = $n_get($power, 'powerPage.data.selection')
238
- if (! $n_isValidString(o.selection)) {
239
- o.selection = 'single'
240
- }
241
- } else {
242
- o.selection = 'single'
243
- }
244
- }
245
-
246
- // 自定义参数
247
- const optionsQuery = $n_isValidObject(o.query) ? o.query : {}
248
-
249
- // 如果为自定义路由
250
- if ($n_isValidString(o.path)) {
251
- // 获取权限路由
252
- $route = $n_router.resolve({
253
- path: o.path,
254
- query: optionsQuery,
255
- })
256
- // 否则获取当前路由
257
- } else {
258
- // 获取权限路由
259
- $route = hasPowr ? $power.getRoute() : $n_router.getRoute()
260
- Object.assign($route.query, optionsQuery)
261
- }
262
-
263
- // 是否有权限按钮
264
- const _hasPowerBtns = hasPowr ? $power.powerBtns.value.length : false
265
- if (_isCreated) {
266
- hasPowerBtns.value = _hasPowerBtns
267
- } else {
268
- hasPowerBtns = ref(_hasPowerBtns)
269
- }
270
-
271
- // 表格已选数据
272
- if (hasPowr) {
273
- tableSelected = $power.tableSelected
274
- } else if (_isCreated) {
275
- tableSelected.value = []
276
- } else {
277
- tableSelected = ref([])
278
- }
279
- if ($n_isValidArray(o.selected)) {
280
- tableSelected.value = o.selected
281
- }
282
-
283
- // 是否开启缓存
284
- isCache = !! o.cache
285
-
286
- // 缓存名
287
- cacheName = $route.path ? $route.path : ($n_isValidString(o.cache) ? o.cache : '')
288
-
289
- // 表格列
290
- const _tableColumns = []
291
-
292
- // 如果有权限按钮
293
- if (hasPowerBtns.value) {
294
- // 添加操作列
295
- o.columns.push({
296
- label: '操作',
297
- name: 'settings',
298
- })
299
- }
300
-
301
- // 表格图片
302
- if (_isCreated) {
303
- tableImgs.value = []
304
- } else {
305
- tableImgs = ref([])
306
- }
307
-
308
- // 设置表格列数据
309
- // 设置列参数
310
- $n_forEach(o.columns, function(item) {
311
-
312
- if (
313
- ! $n_has(item, 'field')
314
- && $n_has(item, 'name')
315
- ) {
316
- item.field = item.name
317
- }
318
-
319
- if (! $n_has(item, 'align')) {
320
- item.align = 'left'
321
- }
322
-
323
- // 是否隐藏
324
- item.hide = $n_get(item, 'hide') === true
325
-
326
- // 如果有显示项
327
- if ($n_get(item, 'visible') !== false) {
328
- o.visibleColumns.push(item.field)
329
- }
330
-
331
- // 如果有时间戳
332
- if ($n_has(item, 'time')) {
333
- if (! $n_has(item, 'format')) {
334
- item.format = val => $n_getTime(val, { format: item.time === true ? `YYYY-MM-DD HH:mm` : item.time }, '-')
335
- }
336
-
337
- // 如果有数据字典
338
- } else if ($n_has(item, 'dict')) {
339
- if (! $n_has(item, 'format')) {
340
- item.format = $n_isArray(item.dict)
341
- ? function (val) {
342
- for (const e of item.dict) {
343
- if (e.value === val) {
344
- return e.label
345
- }
346
- }
347
- return ''
348
- }
349
- : val => $n_dict(item.dict, val)
350
- }
351
-
352
- // 如果有图片
353
- } else if ($n_has(item, 'img')) {
354
- tableImgs.value.push({
355
- name: item.name,
356
- count: item.img === true ? 1 : item.img,
357
- })
358
-
359
- // 如果有价格
360
- } else if ($n_has(item, 'price')) {
361
- if (! $n_has(item, 'format')) {
362
- item.format = val => $n_price(val)
363
- }
364
- }
365
-
366
- // 如果有路由
367
- if ($n_get(item, 'route')) {
368
- // 如果该值在当前路由路径中, 则显示
369
- if ($n_indexOf($route.fullPath, item.route) > -1) {
370
- _tableColumns.push(item)
371
- }
372
-
373
- } else {
374
- _tableColumns.push(item)
375
- }
376
- })
377
-
378
- // 表格可见列
379
- const _tableVisibleColumns = o.showVisibleColumns && isCache ?
380
- $n_cb($n_storage.get('table:visible_columns:' + cacheName), e => Array.isArray(e) ? e : $n_uniq([...o.visibleColumns]))
381
- : $n_uniq([...o.visibleColumns])
382
-
383
- // 表格翻页参数
384
- const _tablePagination = $route.fullPath ? o.pagination : {}
385
-
386
- // 表格宫格
387
- const _tableGrid = isCache && $n_storage.get('table:grid:' + cacheName) === true ? true : o.showGrid
388
-
389
- // 获取原始数据
390
- const r = getRawData(_tableColumns, Object.assign({}, $route.query), o.searchFromQuery)
391
-
392
- // 原始参数
393
- rawQuery = r.rawQuery
394
- // 原始表格搜索参数
395
- rawSearchOptions = r.rawSearchOptions
396
- // 原始表格搜索值(空表格搜索值, 用于搜索重置)
397
- rawTableSearchValue = r.rawTableSearchValue
398
- // 首次表格搜索值(如果表格搜索参数中带了初始值, 则设置初始值)
399
- firstTableSearchValue = r.firstTableSearchValue
400
-
401
- // 表格搜索数据值
402
- const _tableSearchValue = $route.fullPath ? firstTableSearchValue : []
403
-
404
- if (_isCreated) {
405
-
406
- // 表格列
407
- tableColumns.value = _tableColumns
408
-
409
- // 表格是否显示可见列
410
- tableShowVisibleColumns.value = o.showVisibleColumns
411
-
412
- // 表格可见列
413
- tableVisibleColumns.value = _tableVisibleColumns
414
-
415
- // 表格加载状态
416
- tableLoading.value = o.loading
417
-
418
- // 表格行数据
419
- tableRows.value = o.rows
420
-
421
- // 表格翻页参数
422
- tablePagination.value = _tablePagination
423
-
424
- // 表格宫格
425
- tableGrid.value = _tableGrid
426
-
427
- // 表格合计
428
- tableSummary.value = null
429
-
430
- // 表格选择类型
431
- tableSelection.value = o.selection
432
-
433
- // 表格分隔栏
434
- tableSeparator.value = o.separator
435
-
436
- // 表格搜索数据值
437
- tableSearchValue.value = _tableSearchValue
438
-
439
- // 表格搜索参数
440
- tableSearchOptions.value = null
441
-
442
- } else {
443
-
444
- // 表格列
445
- tableColumns = ref(_tableColumns)
446
-
447
- // 表格是否显示可见列
448
- tableShowVisibleColumns = ref(o.showVisibleColumns)
449
-
450
- // 表格可见列
451
- tableVisibleColumns = ref(_tableVisibleColumns)
452
-
453
- // 表格加载状态
454
- tableLoading = ref(o.loading)
455
-
456
- // 表格行数据
457
- tableRows = ref(o.rows)
458
-
459
- // 表格翻页参数
460
- tablePagination = ref(_tablePagination)
461
-
462
- // 表格宫格
463
- tableGrid = ref(_tableGrid)
464
-
465
- // 表格合计
466
- tableSummary = ref(null)
467
-
468
- // 表格选择类型
469
- tableSelection = ref(o.selection)
470
-
471
- // 表格分隔栏
472
- tableSeparator = ref(o.separator)
473
-
474
- // 表格搜索数据值
475
- tableSearchValue = ref(_tableSearchValue)
476
-
477
- // 表格搜索参数
478
- tableSearchOptions = ref(null)
479
- }
480
-
481
- // 表格请求参数(将表格传参中的搜索参数剥离掉, 剩下的直接当做参数传递给服务器)
482
- tableRequestQuery = {}
483
-
484
- // 是否请求表格合计
485
- isRequestSummary = false
486
-
487
- // 是否已加载
488
- _isTableLoaded = false
489
-
490
- // 如果开启搜索
491
- if (o.search) {
492
- // 设置表格搜索参数
493
- setTableSearchOptions()
494
- .finally()
495
- }
496
-
497
- if (_isCreated) {
498
-
499
- // 重新赋值
500
- Object.assign(resTable, {
501
- // 当前路由全路径
502
- routeFullPath: $route.fullPath,
503
- // 当前路由路径
504
- routePath: $route.path,
505
- // 当前路由参数
506
- routeQuery: $route.query,
507
- // 表格行唯一键值
508
- tableRowKey: o.rowKey,
509
- // 表格每页显示行数选项
510
- tableRowsPerPageOptions: o.rowsPerPageOptions,
511
- })
512
-
513
- if (hasPowr) {
514
- $power.update(function(data, _data) {
515
- _data.$table = resTable
516
- })
517
- }
518
- }
519
- }
520
-
521
- // ==========【计算属性】=============================================================================================
522
-
523
- /**
524
- * 固定在表格右边的权限按钮列表
525
- */
526
- const tableFixedPowerBtns = computed(function () {
527
-
528
- const lists = []
529
-
530
- if (hasPowerBtns.value) {
531
-
532
- // 先格式化权限按钮列表
533
- $n_forEach($n_$power.formatBtns($power.powerBtns.value), function(item) {
534
- // 如果是固定按钮
535
- if (item.fixed) {
536
- lists.push(item)
537
- }
538
- })
539
- }
540
-
541
- return lists
542
- })
543
-
544
- /**
545
- * 获取权限按钮中可双击的按钮
546
- */
547
- const tableDbClickPowerBtn = computed(function () {
548
- if (
549
- // 如果有权限按钮
550
- hasPowerBtns.value
551
- // 非手机模式
552
- && ! $q.platform.is.mobile
553
- // 有权限列表
554
- && $n_isValidArray($power.powerBtns.value)
555
- ) {
556
- for (const item of $power.powerBtns.value) {
557
- if ($n_has(item, 'data.dbclick') === true) {
558
- return item
559
- }
560
- }
561
- }
562
- })
563
-
564
- /**
565
- * 是否显示固定在右边的权限按钮列表
566
- */
567
- const showTableFixed = computed(function () {
568
- return $n_indexOf(tableVisibleColumns.value, 'settings') > -1
569
- })
570
-
571
- // ==========【监听数据】=============================================================================================
572
-
573
- // #if ! IS_DEV
574
-
575
- /**
576
- * 监听表格宫格模式
577
- */
578
- watch(tableGrid, function(val) {
579
- if (isCache) {
580
- // 设置宫格模式缓存(永久缓存)
581
- $n_storage.set('table:grid:' + cacheName, val, 0)
582
- }
583
- })
584
-
585
- /**
586
- * 监听表格可见列
587
- */
588
- watch(tableVisibleColumns, function(val) {
589
- if (o.showVisibleColumns && isCache) {
590
- // 设置监听表格可见列缓存(永久缓存)
591
- $n_storage.set('table:visible_columns:' + cacheName, val, 0)
592
- }
593
- })
594
-
595
- // #endif
596
-
597
- /**
598
- * 监听固定在右边的权限按钮列表
599
- */
600
- watch(tableFixedPowerBtns, function (lists) {
601
-
602
- if (! hasPowerBtns.value) {
603
- return
604
- }
605
-
606
- const index = $n_indexOf(tableVisibleColumns.value, 'settings')
607
-
608
- // 如果有固定在右边的权限按钮列表
609
- if ($n_isValidArray(lists)) {
610
-
611
- // 如果设置不在可见列中
612
- if (index === -1) {
613
-
614
- // 如果非手机模式
615
- if (! $q.platform.is.mobile) {
616
-
617
- // 则将设置加入可见列中
618
- tableVisibleColumns.value.push('settings')
619
- }
620
-
621
- // 否则在可见列中 && 如果是手机模式
622
- } else if ($q.platform.is.mobile) {
623
-
624
- // 则将设置从可见列中删除
625
- tableVisibleColumns.value.splice(index, 1)
626
- }
627
-
628
- // 否则如果设置在可见列中
629
- } else if (index > -1) {
630
-
631
- // 则将设置从可见列中删除
632
- tableVisibleColumns.value.splice(index, 1)
633
- }
634
-
635
- }, {
636
- // 立即执行
637
- immediate: true,
638
- })
639
-
640
- // ==========【方法】================================================================================================
641
-
642
- /**
643
- * 设置表格传参
644
- */
645
- function setQuery(query) {
646
-
647
- if ($n_isValidObject(query)) {
648
-
649
- query = $n_cloneDeep(query)
650
-
651
- // 搜索参数键值数组
652
- const searchQueryKey = []
653
-
654
- // 搜索键值数组
655
- const NSearchKeys = []
656
- // 搜索数组
657
- const NSearchValues = []
658
-
659
- // 参数中是否有自定义搜索参数
660
- const hasNSearch = $n_has(query, 'n_search')
661
- if (hasNSearch) {
662
- // 删除在搜索中存在的参数键值
663
- $n_forIn(query.n_search, function (item, key) {
664
- if ($n_has(query, key)) {
665
- delete query[key]
666
- }
667
- })
668
- }
669
-
670
- // 如果允许从参数中获取搜索值
671
- if (o.searchFromQuery) {
672
-
673
- $n_forEach(rawSearchOptions, function (item, index) {
674
-
675
- const valueItem = tableSearchValue.value[index]
676
-
677
- // 如果传参在搜素 n_search 参数中
678
- if (hasNSearch && $n_has(query.n_search, item.name)) {
679
- const newSearchItem = query.n_search[item.name]
680
- if ($n_isValidArray(newSearchItem)) {
681
- valueItem[0].compare = newSearchItem[0].compare
682
- valueItem[0].value = newSearchItem[0].value
683
-
684
- if (newSearchItem.length > 1) {
685
- valueItem[1].compare = newSearchItem[1].compare
686
- valueItem[1].value = newSearchItem[1].value
687
- }
688
- }
689
- // 设置搜索的 key
690
- NSearchKeys.push(item.name)
691
-
692
- // 如果传参在搜索参数中
693
- } else if ($n_has(query, item.name)) {
694
- // 设置单个搜索值
695
- setItemValue(valueItem, $n_isRequired(query[item.name]) ? query[item.name] : '')
696
- // 设置参数中搜索的 key
697
- searchQueryKey.push(item.name)
698
- }
699
- })
700
-
701
- $n_forEach(searchQueryKey, function (key) {
702
- delete query[key]
703
- })
704
-
705
- if (hasNSearch) {
706
- $n_forIn(query.n_search, function(item, key) {
707
- if (
708
- NSearchKeys.indexOf(key) === -1
709
- && $n_isValidArray(item)
710
- ) {
711
- item[0].field = key
712
- NSearchValues.push(item[0])
713
-
714
- if (item.length > 1) {
715
- item[1].field = key
716
- NSearchValues.push(item[1])
717
- }
718
- }
719
- })
720
- }
721
-
722
- } else {
723
- $n_forIn(query.n_search, function(item, key) {
724
- if ($n_isValidArray(item)) {
725
- item[0].field = key
726
- NSearchValues.push(item[0])
727
- if (item.length > 1) {
728
- item[1].field = key
729
- NSearchValues.push(item[1])
730
- }
731
- }
732
- })
733
- }
734
-
735
- if (NSearchValues.length) {
736
- query.n_search = NSearchValues
737
- } else if (hasNSearch) {
738
- delete query.n_search
739
- }
740
-
741
- tableRequestQuery = query
742
- return
743
- }
744
-
745
- tableRequestQuery = {}
746
- }
747
-
748
-
749
- /**
750
- * 表格是否已加载
751
- */
752
- function isTableLoaded() {
753
- return _isTableLoaded
754
- }
755
-
756
- /**
757
- * 表格加载(只加载一次)
758
- */
759
- async function tableLoad() {
760
-
761
- // 如果表格已加载过了
762
- if (_isTableLoaded) {
763
- // 则无任何操作
764
- return
765
- }
766
-
767
- // 表格重新加载
768
- await tableReload()
769
- }
770
-
771
- /**
772
- * 表格重新加载
773
- */
774
- async function tableReload(params = null) {
775
-
776
- // 表格已加载
777
- _isTableLoaded = true
778
-
779
- if (! $route.fullPath) {
780
- return
781
- }
782
-
783
- // 请求表格合计
784
- if (o.summary) {
785
- isRequestSummary = true
786
- }
787
-
788
- // 请求表格数据
789
- // $tableRef?.requestServerInteraction({
790
- // pagination: o.pagination,
791
- // })
792
-
793
- await tableRequest(Object.assign({
794
- pagination: o.pagination,
795
- }, params))
796
-
797
- // 清空表格已选数据
798
- if (o.refreshResetSelected) {
799
- tableSelected.value = []
800
- }
801
- }
802
-
803
- /**
804
- * 表格刷新
805
- */
806
- async function tableRefresh() {
807
-
808
- if (! $route.fullPath) {
809
- return
810
- }
811
-
812
- // 请求表格合计
813
- if (o.summary) {
814
- isRequestSummary = true
815
- }
816
-
817
- // 请求表格数据
818
- // $tableRef.requestServerInteraction()
819
- await tableRequest({
820
- pagination: tablePagination.value,
821
- })
822
-
823
- // 清空表格已选数据
824
- if (o.refreshResetSelected) {
825
- tableSelected.value = []
826
- }
827
- }
828
-
829
- /**
830
- * 表格搜索重置
831
- */
832
- function tableSearchReset(reload = true, params = null) {
833
-
834
- const newValue = []
835
-
836
- $n_forEach(rawSearchOptions, function (item, index) {
837
- // 如果该搜索条件是隐藏的
838
- if (item.hide) {
839
- newValue.push(tableSearchValue.value[index])
840
- // 否则为初始值
841
- } else {
842
- newValue.push(rawTableSearchValue[index])
843
- }
844
- })
845
-
846
- // 还原表格搜索数据
847
- tableSearchValue.value = $n_cloneDeep(newValue)
848
-
849
- // 表格重新加载
850
- if (reload) {
851
- tableReload(params)
852
- .finally()
853
- }
854
- }
855
-
856
- /**
857
- * 获取表格请求数据
858
- */
859
- function getTableRequestData(props, isSummary = undefined) {
860
-
861
- // 解构数据
862
- const {
863
- // filter,
864
- pagination: {
865
- // 页码
866
- page,
867
- // 每页的数据条数
868
- rowsPerPage,
869
- // 排序字段
870
- sortBy,
871
- // 是否降序排列
872
- descending,
873
- }
874
- } = props
875
-
876
- // 请求数据
877
- const data = {
878
- // 页码
879
- page,
880
- // 每页的数据条数
881
- per_page: rowsPerPage,
882
- }
883
-
884
- // 如果排序字段是有效值
885
- if ($n_isValidValue(sortBy)) {
886
- Object.assign(data, {
887
- // 排序字段
888
- order_by: sortBy,
889
- // 是否降序排列
890
- is_desc: descending ? 1 : 0,
891
- })
892
- }
893
-
894
- // 合并参数
895
- $n_forIn(Object.assign({}, rawQuery, tableRequestQuery, o.data), function(value, key) {
896
- // 如果有值
897
- if ($n_isRequired(value)) {
898
- data[key] = value
899
- }
900
- })
901
-
902
- // 获取搜索值
903
- const search = formatValue(rawSearchOptions, tableSearchValue.value)
904
- if ($n_isValidArray(search)) {
905
- data.n_search = $n_has(data, 'n_search') ? $n_concat(data.n_search, search) : search
906
- }
907
-
908
- if ($n_isNil(isSummary)) {
909
- isSummary = isRequestSummary
910
- }
911
-
912
- // 如果请求表格合计
913
- if (isSummary) {
914
- data.summary = 1
915
- }
916
-
917
- return data
918
- }
919
-
920
- /**
921
- * 请求数据
922
- */
923
- async function tableRequest(props) {
924
-
925
- // 加载
926
- tableLoading.value = true
927
-
928
- // 解构数据
929
- const {
930
- // filter,
931
- pagination: {
932
- // 页码
933
- page,
934
- // 每页的数据条数
935
- rowsPerPage,
936
- // 排序字段
937
- sortBy,
938
- // 是否降序排列
939
- descending,
940
- }
941
- } = props
942
-
943
- // http 请求参数
944
- let httpOptions = Object.assign({
945
- // 请求数据
946
- url: $n_isValidString(o.url) ? o.url : $route.path,
947
- // 请求数据
948
- data: getTableRequestData(props, isRequestSummary),
949
- // ~~~~~~ 先开启防抖, 如果后期遇到表格加载不出来的情况, 再关闭防抖
950
- // 关闭防抖(允许重复请求)
951
- debounce: false,
952
- }, o.httpSettings)
953
-
954
- // 下一步
955
- async function next() {
956
-
957
- const e = $n_isFunction(o.request)
958
- // 如果有自定义请求方法
959
- ? await $n_runAsync(o.request)({
960
- // http 请求参数
961
- httpOptions,
962
- // 表格声明属性
963
- props,
964
- // 表格行数据
965
- rows: tableRows,
966
- // 表格已选数据
967
- selected: tableSelected,
968
- })
969
- // 否则请求服务器
970
- : await $n_http(httpOptions)
971
-
972
- const { status, data: res } = e
973
-
974
- // 返回结果数据
975
- const resultData = Object.assign({
976
- // 请求地址
977
- requestUrl: httpOptions.url,
978
- // 参数
979
- options: httpOptions,
980
- // 请求数据
981
- requestData: httpOptions.data,
982
- }, e)
983
-
984
- // 请求后执行
985
- if (await $n_runAsync(o.requestAfter)(resultData) === false) {
986
- return
987
- }
988
-
989
- // 请求成功
990
- if (status) {
991
-
992
- // 下一步
993
- function nextSuccess() {
994
-
995
- const {
996
- // 返回数据
997
- rows,
998
- // 数据总数
999
- total,
1000
- } = res
1001
-
1002
- // 如果请求表格合计
1003
- if (isRequestSummary) {
1004
- const summary = $n_get(res, 'summary')
1005
- tableSummary.value = $n_isValidObject(summary) ? summary : null
1006
- }
1007
-
1008
- // 更新页码
1009
- tablePagination.value.page = page
1010
- // 更新每页的数据条数
1011
- tablePagination.value.rowsPerPage = rowsPerPage
1012
- // 更新数据总数
1013
- tablePagination.value.rowsNumber = total
1014
- // 更新排序字段
1015
- tablePagination.value.sortBy = sortBy
1016
- // 更新是否降序排列
1017
- tablePagination.value.descending = descending
1018
-
1019
- // 格式化单条数据
1020
- if ($n_isFunction(o.formatRow)) {
1021
- $n_forEach(rows, function(row) {
1022
- o.formatRow({
1023
- row,
1024
- rows: tableRows,
1025
- selected: tableSelected,
1026
- })
1027
- })
1028
- }
1029
-
1030
- // 清除现有数据并添加新数据
1031
- tableRows.value.splice(0, tableRows.value.length, ...rows)
1032
-
1033
- // 取消请求表格合计
1034
- isRequestSummary = false
1035
-
1036
- // 取消加载
1037
- tableLoading.value = false
1038
- }
1039
-
1040
- // 请求成功执行
1041
- if (await $n_runAsync(o.requestSuccess)(Object.assign({ next: nextSuccess }, resultData)) === false) {
1042
- return
1043
- }
1044
-
1045
- nextSuccess()
1046
-
1047
- // 否则请求失败
1048
- } else {
1049
-
1050
- // 请求失败执行
1051
- $n_run(o.requestFail)(resultData)
1052
-
1053
- // 取消请求表格合计
1054
- isRequestSummary = false
1055
-
1056
- // 取消加载
1057
- tableLoading.value = false
1058
- }
1059
- }
1060
-
1061
- if (o.requestBefore) {
1062
-
1063
- // 请求前执行
1064
- const resBefore = await $n_runAsync(o.requestBefore)({
1065
- options: httpOptions,
1066
- next,
1067
- })
1068
- if (resBefore !== void 0) {
1069
- if (resBefore === false) {
1070
- return
1071
- }
1072
- httpOptions = resBefore
1073
- }
1074
- }
1075
-
1076
- // 下一步
1077
- next()
1078
- .finally()
1079
- }
1080
-
1081
- /**
1082
- * 单击表格行
1083
- */
1084
- function _tableRowClick(e, row) {
1085
-
1086
- // 如果选择类型为无
1087
- if (tableSelection.value === 'none') {
1088
- // 则无任何操作
1089
- return
1090
- }
1091
-
1092
- const opt = {}
1093
- opt[o.rowKey] = row[o.rowKey]
1094
-
1095
- // 获取当前数据索引
1096
- const itemIndex = $n_findIndex(tableSelected.value, opt)
1097
-
1098
- // 如果不存在, 则添加
1099
- if (itemIndex === -1) {
1100
-
1101
- // 如果选择类型为单选
1102
- if (tableSelection.value === 'single') {
1103
- tableSelected.value = [ row ]
1104
-
1105
- // 否则为多选
1106
- } else {
1107
- tableSelected.value.push(row)
1108
- }
1109
-
1110
- // 否则删除
1111
- } else {
1112
- tableSelected.value.splice(itemIndex, 1)
1113
- }
1114
- }
1115
- function tableRowClick(...e) {
1116
-
1117
- // 单击表格行
1118
- _tableRowClick(...e)
1119
-
1120
- // 如果有自定义单击事件
1121
- if ($n_isFunction(o.rowClick)) {
1122
- o.rowClick(...e)
1123
- }
1124
- }
1125
-
1126
- /**
1127
- * 双击表格行
1128
- */
1129
- function _tableRowDblclick(e, row) {
1130
-
1131
- // 如果选择类型为无
1132
- if (tableSelection.value === 'none') {
1133
- // 则无任何操作
1134
- return
1135
- }
1136
-
1137
- if (
1138
- // 有权限
1139
- hasPowr
1140
- // 有双击的权限按钮
1141
- && tableDbClickPowerBtn.value
1142
- ) {
1143
- $power.powerBtnClick(tableDbClickPowerBtn.value, [ row ])
1144
- }
1145
- }
1146
- function tableRowDblclick(...e) {
1147
-
1148
- // 双击表格行
1149
- _tableRowDblclick(...e)
1150
-
1151
- // 如果有自定义双击表格行事件
1152
- if ($n_isFunction(o.tableRowDblclick)) {
1153
- o.tableRowDblclick(...e)
1154
- }
1155
- }
1156
-
1157
- /**
1158
- * 设置表格搜索参数
1159
- */
1160
- async function setTableSearchOptions(format) {
1161
- tableSearchOptions.value = await getOptions(rawSearchOptions, format)
1162
- }
1163
-
1164
- /**
1165
- * 获取表格搜索值
1166
- */
1167
- function getTableSearchValue() {
1168
- return formatValue(rawSearchOptions, tableSearchValue.value)
1169
- }
1170
-
1171
- /**
1172
- * 是否有表格搜索值
1173
- */
1174
- // function hasTableSearchValue() {
1175
- // return !! formatValue(rawSearchOptions, tableSearchValue.value).length
1176
- // }
1177
-
1178
- // ==========【返回】=================================================================================================
1179
-
1180
- const resTable = {
1181
- // 当前路由全路径
1182
- routeFullPath: $route.fullPath,
1183
- // 当前路由路径
1184
- routePath: $route.path,
1185
- // 当前路由参数
1186
- routeQuery: $route.query,
1187
- // 表格行唯一键值
1188
- tableRowKey: o.rowKey,
1189
- // 表格每页显示行数选项
1190
- tableRowsPerPageOptions: o.rowsPerPageOptions,
1191
-
1192
- // 表格加载状态
1193
- tableLoading,
1194
- // 表格选择类型
1195
- tableSelection,
1196
- // 表格分隔栏
1197
- tableSeparator,
1198
- // 表格列数据(对象数组)
1199
- tableColumns,
1200
- // 表格是否显示可见列
1201
- tableShowVisibleColumns,
1202
- // 表格可见列
1203
- tableVisibleColumns,
1204
- // 表格行数据
1205
- tableRows,
1206
- // 表格翻页参数
1207
- tablePagination,
1208
- // 表格已选数据
1209
- tableSelected,
1210
- // 固定在右边的权限按钮列表
1211
- tableFixedPowerBtns,
1212
- // 是否显示固定在右边的权限按钮列表
1213
- showTableFixed,
1214
- // 表格图片
1215
- tableImgs,
1216
-
1217
- // 表格宫格
1218
- tableGrid,
1219
- // 表格合计
1220
- tableSummary,
1221
- // 表格搜索数据
1222
- tableSearchValue,
1223
- // 表格搜索参数
1224
- tableSearchOptions,
1225
-
1226
- // 设置表格传参
1227
- setQuery,
1228
- // 表格是否已加载
1229
- isTableLoaded,
1230
- // 表格加载(只加载一次)
1231
- tableLoad,
1232
- // 表格重新加载
1233
- tableReload,
1234
- // 表格刷新
1235
- tableRefresh,
1236
- // 表格搜索重置
1237
- tableSearchReset,
1238
- // 获取表格请求数据
1239
- getTableRequestData,
1240
- // 表格请求数据
1241
- tableRequest,
1242
- // 表格单击表格行
1243
- tableRowClick,
1244
- // 表格双击表格行
1245
- tableRowDblclick,
1246
- // 设置表格搜索参数
1247
- setTableSearchOptions,
1248
-
1249
- // 获取表格搜索值
1250
- getTableSearchValue,
1251
- // 是否有表格搜索值
1252
- // hasTableSearchValue,
1253
-
1254
- // 获取当前路由
1255
- getRoute() {
1256
- return $route
1257
- },
1258
- // 重新创建表格
1259
- reCreate,
1260
- }
1261
-
1262
- if (hasPowr) {
1263
- $power.update(function(data, _data) {
1264
- _data.$table = resTable
1265
- })
1266
- }
1267
-
1268
- // 提供可以被后代组件注入的值
1269
- provide(NTableKey, resTable)
1270
-
1271
- return resTable
1272
- }
1273
-
1274
- /**
1275
- * 获取表格配置
1276
- */
1277
- function config(routePath, path, defaultValue) {
1278
- const res = $n_get(tablesConfig, $n_slash(routePath, 'start', false) + (path ? '.' + path : ''), defaultValue)
1279
- return $n_cloneDeep($n_isFunction(res) ? res() : res)
1280
- }
1281
-
1282
- /**
1283
- * 获取表格浏览数据
1284
- */
1285
- async function getViewData(options) {
1286
-
1287
- const {
1288
- url,
1289
- field,
1290
- value: httpValue,
1291
- data: httpData,
1292
- } = Object.assign({
1293
- field: 'id',
1294
- value: [],
1295
- data: {},
1296
- }, options)
1297
-
1298
- const _isValidValue = $n_isValidValue(httpValue)
1299
- const hasSearch = $n_has(httpData, 'n_search')
1300
- if (_isValidValue || $n_isValidArray(httpValue) || hasSearch) {
1301
-
1302
- const n_search = []
1303
- let per_page = 1
1304
-
1305
- if (! hasSearch) {
1306
-
1307
- let value
1308
- let compare
1309
-
1310
- if (_isValidValue) {
1311
- compare = dicts.SEARCH_COMPARE_TYPE__EQUAL
1312
- value = httpValue
1313
- } else {
1314
- compare = dicts.SEARCH_COMPARE_TYPE__IN
1315
- value = $n_uniq(httpValue)
1316
- per_page = value.length
1317
- }
1318
-
1319
- n_search.push({
1320
- field,
1321
- compare,
1322
- value,
1323
- })
1324
- }
1325
-
1326
- const { status, data } = await $n_http({
1327
- url,
1328
- // 头部请求
1329
- headers: {
1330
- // 添加头部查看请求
1331
- Pview: 1,
1332
- },
1333
- data: Object.assign({
1334
- n_search,
1335
- page: 1,
1336
- per_page,
1337
- }, httpData),
1338
- })
1339
-
1340
- if (
1341
- status
1342
- && $n_isValidArray($n_get(data, 'rows'))
1343
- ) {
1344
- return data.rows
1345
- }
1346
- }
1347
-
1348
- return []
1349
- }
1350
-
1351
- /**
1352
- * 业务表格
1353
- */
1354
- const $table = {
1355
- // 创建表格
1356
- create,
1357
- // 获取表格配置
1358
- config,
1359
-
1360
- // 获取表格浏览数据
1361
- getViewData,
1362
- }
1363
-
1364
- export default $table
1
+ import { ref, computed, provide, inject, watch, nextTick } from 'vue'
2
+ import { useQuasar } from 'quasar'
3
+
4
+ import $n_has from 'lodash/has'
5
+ import $n_get from 'lodash/get'
6
+ import $n_cloneDeep from 'lodash/cloneDeep'
7
+ import $n_merge from 'lodash/merge'
8
+ import $n_isFunction from 'lodash/isFunction'
9
+ import $n_findIndex from 'lodash/findIndex'
10
+ import $n_uniq from 'lodash/uniq'
11
+ import $n_concat from 'lodash/concat'
12
+ import $n_isNil from 'lodash/isNil'
13
+ import $n_isArray from 'lodash/isArray'
14
+
15
+ import $n_router from '@netang/utils/vue/router'
16
+
17
+ import $n_forEach from '@netang/utils/forEach'
18
+ import $n_isValidArray from '@netang/utils/isValidArray'
19
+ import $n_isValidString from '@netang/utils/isValidString'
20
+ import $n_indexOf from '@netang/utils/indexOf'
21
+ import $n_storage from '@netang/utils/storage'
22
+
23
+ import $n_isRequired from '@netang/utils/isRequired'
24
+ import $n_forIn from '@netang/utils/forIn'
25
+ import $n_run from '@netang/utils/run'
26
+ import $n_runAsync from '@netang/utils/runAsync'
27
+ import $n_isValidObject from '@netang/utils/isValidObject'
28
+ import $n_isValidValue from '@netang/utils/isValidValue'
29
+ import $n_slash from '@netang/utils/slash'
30
+ import $n_http from '@netang/utils/http'
31
+ import $n_cb from '@netang/utils/cb'
32
+
33
+ import $n_$power from './$power'
34
+ import $n_dict from './dict'
35
+ import $n_price from './price'
36
+ import $n_getTime from './getTime'
37
+
38
+ import { configs } from './config'
39
+
40
+ import {
41
+ // 设置单个搜索值
42
+ setItemValue,
43
+ // 从表格列获取原始值
44
+ getRawData,
45
+ // 获取参数
46
+ getOptions,
47
+ // 格式化值
48
+ formatValue,
49
+ } from './useSearch'
50
+
51
+ import { NRenderKey, NPowerKey, NTableKey } from './symbols'
52
+
53
+ const {
54
+ // 表格配置
55
+ tablesConfig,
56
+ // 字典常量
57
+ dicts,
58
+ } = configs
59
+
60
+ /**
61
+ * 创建表格
62
+ */
63
+ function create(options) {
64
+
65
+ // ==========【数据】=================================================================================================
66
+
67
+ // quasar 对象
68
+ const $q = useQuasar()
69
+
70
+ // 每页显示行数选项
71
+ const rowsPerPageOptions = [30, 40, 50, 100, 200, 500, 1000]
72
+
73
+ // 原始参数
74
+ const rawOptions = {
75
+ // 路由路径
76
+ path: '',
77
+ // 请求地址(默认为 path)
78
+ url: '',
79
+ // 路由参数
80
+ query: {},
81
+ // 附加请求数据
82
+ data: {},
83
+ // 表格行唯一键值
84
+ rowKey: 'id',
85
+ // 选择类型, 可选值 none single(默认) multiple
86
+ // selection: '',
87
+ // 分隔栏, 可选值 horizontal vertical cell none
88
+ separator: 'cell',
89
+ // 初始已选数据
90
+ selected: [],
91
+ // 初始表格加载状态
92
+ loading: false,
93
+ // 表格列数据(对象数组)
94
+ columns: [],
95
+ // 初始可见列
96
+ visibleColumns: [],
97
+ // 表格行数据
98
+ rows: [],
99
+ // 表格翻页参数
100
+ pagination: {
101
+ // 页码
102
+ page: 1,
103
+ // 每页的数据条数
104
+ rowsPerPage: $n_has(options, 'rowsPerPageOptions') ? options.rowsPerPageOptions[0] : rowsPerPageOptions[0],
105
+ // 数据总数(服务器返回)
106
+ rowsNumber: 1,
107
+ // 排序字段
108
+ sortBy: null,
109
+ // sortBy: 'id',
110
+ // 是否降序排列
111
+ descending: true,
112
+ },
113
+ // 每页显示行数选项
114
+ rowsPerPageOptions,
115
+ // 请求前执行
116
+ requestBefore: null,
117
+ // 请求成功执行
118
+ requestSuccess: null,
119
+ // 请求失败执行
120
+ requestFail: null,
121
+ // 请求后执行
122
+ requestAfter: null,
123
+ // 自定义请求方法
124
+ request: null,
125
+ // 格式化单条数据
126
+ formatRow: null,
127
+ // http 设置
128
+ httpSettings: {},
129
+ // 是否开启初始搜素
130
+ search: true,
131
+ // 是否开启合计
132
+ summary: false,
133
+ // 从参数中获取搜索值
134
+ searchFromQuery: true,
135
+ // 是否显示宫格
136
+ showGrid: false,
137
+ // 是否显示可见列
138
+ showVisibleColumns: true,
139
+ // 是否开启缓存
140
+ cache: true,
141
+ // 是否刷新后清空已选数据
142
+ refreshResetSelected: true,
143
+
144
+ // 单击表格行事件
145
+ rowClick: null,
146
+ // 双击表格行事件
147
+ rowDblClick: null,
148
+ }
149
+
150
+ let o
151
+ let $power
152
+ let hasPowr
153
+ let $render
154
+ let $route
155
+ let hasPowerBtns
156
+ let tableSelected
157
+ let isCache
158
+ let cacheName
159
+ let tableColumns
160
+ let tableImgs
161
+
162
+ // 是否显示可见列
163
+ let tableShowVisibleColumns
164
+ // 表格可见列
165
+ let tableVisibleColumns
166
+ // 表格加载状态
167
+ let tableLoading
168
+ // 表格行数据
169
+ let tableRows
170
+ // 表格翻页参数
171
+ let tablePagination
172
+ // 表格宫格
173
+ let tableGrid
174
+ // 表格请求参数(将表格传参中的搜索参数剥离掉, 剩下的直接当做参数传递给服务器)
175
+ let tableRequestQuery
176
+ // 是否请求表格合计
177
+ let isRequestSummary
178
+ // 表格合计
179
+ let tableSummary
180
+ // 表格选择类型
181
+ let tableSelection
182
+ // 表格分隔栏
183
+ let tableSeparator
184
+
185
+ // 原始参数
186
+ let rawQuery
187
+ // 原始表格搜索参数
188
+ let rawSearchOptions
189
+ // 原始表格搜索值(空表格搜索值, 用于搜索重置)
190
+ let rawTableSearchValue
191
+ // 首次表格搜索值(如果表格搜索参数中带了初始值, 则设置初始值)
192
+ let firstTableSearchValue
193
+
194
+ // 表格搜索数据值
195
+ let tableSearchValue
196
+ // 表格搜索参数
197
+ let tableSearchOptions
198
+ // 是否已加载
199
+ let _isTableLoaded
200
+
201
+ // 是否已生成数据
202
+ let _isCreated = false
203
+
204
+ // 表格节点
205
+ const tableRef = ref(null)
206
+
207
+ // 创建表格
208
+ reCreate(options)
209
+
210
+ // 已生成数据
211
+ _isCreated = true
212
+
213
+ // ==========【方法】================================================================================================
214
+
215
+ /**
216
+ * 重新创建表格
217
+ */
218
+ function reCreate(options) {
219
+
220
+ // 获取参数
221
+ o = $n_merge({}, rawOptions, options)
222
+
223
+ // 获取权限注入
224
+ $power = $n_has(options, '$power') ? options.$power : (_isCreated ? $power : inject(NPowerKey))
225
+ hasPowr = !! $power
226
+
227
+ // 获取渲染注入
228
+ $render = $n_has(options, '$render') ? options.$render : (_isCreated ? $render : inject(NRenderKey))
229
+ if (!! $render) {
230
+ // 如果有表格传参, 则合并参数
231
+ const tableProps = $n_get($render, 'props.tableProps')
232
+ if ($n_isValidObject(tableProps)) {
233
+ $n_merge(o, tableProps)
234
+ }
235
+ }
236
+
237
+ // 获取选择类型(默认 single)
238
+ if (! $n_has(o, 'selection') || ! $n_isValidString(o.selection)) {
239
+ if (hasPowr) {
240
+ o.selection = $n_get($power, 'powerPage.data.selection')
241
+ if (! $n_isValidString(o.selection)) {
242
+ o.selection = 'single'
243
+ }
244
+ } else {
245
+ o.selection = 'single'
246
+ }
247
+ }
248
+
249
+ // 自定义参数
250
+ const optionsQuery = $n_isValidObject(o.query) ? o.query : {}
251
+
252
+ // 如果为自定义路由
253
+ if ($n_isValidString(o.path)) {
254
+ // 获取权限路由
255
+ $route = $n_router.resolve({
256
+ path: o.path,
257
+ query: optionsQuery,
258
+ })
259
+ // 否则获取当前路由
260
+ } else {
261
+ // 获取权限路由
262
+ $route = hasPowr ? $power.getRoute() : $n_router.getRoute()
263
+ Object.assign($route.query, optionsQuery)
264
+ }
265
+
266
+ // 是否有权限按钮
267
+ const _hasPowerBtns = hasPowr ? $power.powerBtns.value.length : false
268
+ if (_isCreated) {
269
+ hasPowerBtns.value = _hasPowerBtns
270
+ } else {
271
+ hasPowerBtns = ref(_hasPowerBtns)
272
+ }
273
+
274
+ // 表格已选数据
275
+ if (hasPowr) {
276
+ tableSelected = $power.tableSelected
277
+ } else if (_isCreated) {
278
+ tableSelected.value = []
279
+ } else {
280
+ tableSelected = ref([])
281
+ }
282
+ if ($n_isValidArray(o.selected)) {
283
+ tableSelected.value = o.selected
284
+ }
285
+
286
+ // 是否开启缓存
287
+ isCache = !! o.cache
288
+
289
+ // 缓存名
290
+ cacheName = $route.path ? $route.path : ($n_isValidString(o.cache) ? o.cache : '')
291
+
292
+ // 表格列
293
+ const _tableColumns = []
294
+
295
+ // 如果有权限按钮
296
+ if (hasPowerBtns.value) {
297
+ // 添加操作列
298
+ o.columns.push({
299
+ label: '操作',
300
+ name: 'settings',
301
+ })
302
+ }
303
+
304
+ // 表格图片
305
+ if (_isCreated) {
306
+ tableImgs.value = []
307
+ } else {
308
+ tableImgs = ref([])
309
+ }
310
+
311
+ // 设置表格列数据
312
+ // 设置列参数
313
+ $n_forEach(o.columns, function(item) {
314
+
315
+ if (
316
+ ! $n_has(item, 'field')
317
+ && $n_has(item, 'name')
318
+ ) {
319
+ item.field = item.name
320
+ }
321
+
322
+ if (! $n_has(item, 'align')) {
323
+ item.align = 'left'
324
+ }
325
+
326
+ // 是否隐藏
327
+ item.hide = $n_get(item, 'hide') === true
328
+
329
+ // 如果有显示项
330
+ if ($n_get(item, 'visible') !== false) {
331
+ o.visibleColumns.push(item.field)
332
+ }
333
+
334
+ // 如果有时间戳
335
+ if ($n_has(item, 'time')) {
336
+ if (! $n_has(item, 'format')) {
337
+ item.format = val => $n_getTime(val, { format: item.time === true ? `YYYY-MM-DD HH:mm` : item.time }, '-')
338
+ }
339
+
340
+ // 如果有数据字典
341
+ } else if ($n_has(item, 'dict')) {
342
+ if (! $n_has(item, 'format')) {
343
+ item.format = $n_isArray(item.dict)
344
+ ? function (val) {
345
+ for (const e of item.dict) {
346
+ if (e.value === val) {
347
+ return e.label
348
+ }
349
+ }
350
+ return ''
351
+ }
352
+ : val => $n_dict(item.dict, val)
353
+ }
354
+
355
+ // 如果有图片
356
+ } else if ($n_has(item, 'img')) {
357
+ tableImgs.value.push({
358
+ name: item.name,
359
+ count: item.img === true ? 1 : item.img,
360
+ })
361
+
362
+ // 如果有价格
363
+ } else if ($n_has(item, 'price')) {
364
+ if (! $n_has(item, 'format')) {
365
+ item.format = val => $n_price(val)
366
+ }
367
+ }
368
+
369
+ // 如果有路由
370
+ if ($n_get(item, 'route')) {
371
+ // 如果该值在当前路由路径中, 则显示
372
+ if ($n_indexOf($route.fullPath, item.route) > -1) {
373
+ _tableColumns.push(item)
374
+ }
375
+
376
+ } else {
377
+ _tableColumns.push(item)
378
+ }
379
+ })
380
+
381
+ // 表格可见列
382
+ const _tableVisibleColumns = o.showVisibleColumns && isCache ?
383
+ $n_cb($n_storage.get('table:visible_columns:' + cacheName), e => Array.isArray(e) ? e : $n_uniq([...o.visibleColumns]))
384
+ : $n_uniq([...o.visibleColumns])
385
+
386
+ // 表格翻页参数
387
+ const _tablePagination = $route.fullPath ? o.pagination : {}
388
+
389
+ // 表格宫格
390
+ const _tableGrid = isCache && $n_storage.get('table:grid:' + cacheName) === true ? true : o.showGrid
391
+
392
+ // 获取原始数据
393
+ const r = getRawData(_tableColumns, Object.assign({}, $route.query), o.searchFromQuery)
394
+
395
+ // 原始参数
396
+ rawQuery = r.rawQuery
397
+ // 原始表格搜索参数
398
+ rawSearchOptions = r.rawSearchOptions
399
+ // 原始表格搜索值(空表格搜索值, 用于搜索重置)
400
+ rawTableSearchValue = r.rawTableSearchValue
401
+ // 首次表格搜索值(如果表格搜索参数中带了初始值, 则设置初始值)
402
+ firstTableSearchValue = r.firstTableSearchValue
403
+
404
+ // 表格搜索数据值
405
+ const _tableSearchValue = $route.fullPath ? firstTableSearchValue : []
406
+
407
+ if (_isCreated) {
408
+
409
+ // 表格列
410
+ tableColumns.value = _tableColumns
411
+
412
+ // 表格是否显示可见列
413
+ tableShowVisibleColumns.value = o.showVisibleColumns
414
+
415
+ // 表格可见列
416
+ tableVisibleColumns.value = _tableVisibleColumns
417
+
418
+ // 表格加载状态
419
+ tableLoading.value = o.loading
420
+
421
+ // 表格行数据
422
+ tableRows.value = o.rows
423
+
424
+ // 表格翻页参数
425
+ tablePagination.value = _tablePagination
426
+
427
+ // 表格宫格
428
+ tableGrid.value = _tableGrid
429
+
430
+ // 表格合计
431
+ tableSummary.value = null
432
+
433
+ // 表格选择类型
434
+ tableSelection.value = o.selection
435
+
436
+ // 表格分隔栏
437
+ tableSeparator.value = o.separator
438
+
439
+ // 表格搜索数据值
440
+ tableSearchValue.value = _tableSearchValue
441
+
442
+ // 表格搜索参数
443
+ tableSearchOptions.value = null
444
+
445
+ } else {
446
+
447
+ // 表格列
448
+ tableColumns = ref(_tableColumns)
449
+
450
+ // 表格是否显示可见列
451
+ tableShowVisibleColumns = ref(o.showVisibleColumns)
452
+
453
+ // 表格可见列
454
+ tableVisibleColumns = ref(_tableVisibleColumns)
455
+
456
+ // 表格加载状态
457
+ tableLoading = ref(o.loading)
458
+
459
+ // 表格行数据
460
+ tableRows = ref(o.rows)
461
+
462
+ // 表格翻页参数
463
+ tablePagination = ref(_tablePagination)
464
+
465
+ // 表格宫格
466
+ tableGrid = ref(_tableGrid)
467
+
468
+ // 表格合计
469
+ tableSummary = ref(null)
470
+
471
+ // 表格选择类型
472
+ tableSelection = ref(o.selection)
473
+
474
+ // 表格分隔栏
475
+ tableSeparator = ref(o.separator)
476
+
477
+ // 表格搜索数据值
478
+ tableSearchValue = ref(_tableSearchValue)
479
+
480
+ // 表格搜索参数
481
+ tableSearchOptions = ref(null)
482
+ }
483
+
484
+ // 表格请求参数(将表格传参中的搜索参数剥离掉, 剩下的直接当做参数传递给服务器)
485
+ tableRequestQuery = {}
486
+
487
+ // 是否请求表格合计
488
+ isRequestSummary = false
489
+
490
+ // 是否已加载
491
+ _isTableLoaded = false
492
+
493
+ // 如果开启搜索
494
+ if (o.search) {
495
+ // 设置表格搜索参数
496
+ setTableSearchOptions()
497
+ .finally()
498
+ }
499
+
500
+ if (_isCreated) {
501
+
502
+ // 重新赋值
503
+ Object.assign(resTable, {
504
+ // 当前路由全路径
505
+ routeFullPath: $route.fullPath,
506
+ // 当前路由路径
507
+ routePath: $route.path,
508
+ // 当前路由参数
509
+ routeQuery: $route.query,
510
+ // 表格行唯一键值
511
+ tableRowKey: o.rowKey,
512
+ // 表格每页显示行数选项
513
+ tableRowsPerPageOptions: o.rowsPerPageOptions,
514
+ })
515
+
516
+ if (hasPowr) {
517
+ $power.update(function(data, _data) {
518
+ _data.$table = resTable
519
+ })
520
+ }
521
+ }
522
+ }
523
+
524
+ // ==========【计算属性】=============================================================================================
525
+
526
+ /**
527
+ * 固定在表格右边的权限按钮列表
528
+ */
529
+ const tableFixedPowerBtns = computed(function () {
530
+
531
+ const lists = []
532
+
533
+ if (hasPowerBtns.value) {
534
+
535
+ // 先格式化权限按钮列表
536
+ $n_forEach($n_$power.formatBtns($power.powerBtns.value), function(item) {
537
+ // 如果是固定按钮
538
+ if (item.fixed) {
539
+ lists.push(item)
540
+ }
541
+ })
542
+ }
543
+
544
+ return lists
545
+ })
546
+
547
+ /**
548
+ * 获取权限按钮中可双击的按钮
549
+ */
550
+ const tableDbClickPowerBtn = computed(function () {
551
+ if (
552
+ // 如果有权限按钮
553
+ hasPowerBtns.value
554
+ // 非手机模式
555
+ && ! $q.platform.is.mobile
556
+ // 有权限列表
557
+ && $n_isValidArray($power.powerBtns.value)
558
+ ) {
559
+ for (const item of $power.powerBtns.value) {
560
+ if ($n_has(item, 'data.dbclick') === true) {
561
+ return item
562
+ }
563
+ }
564
+ }
565
+ })
566
+
567
+ /**
568
+ * 是否显示固定在右边的权限按钮列表
569
+ */
570
+ const showTableFixed = computed(function () {
571
+ return $n_indexOf(tableVisibleColumns.value, 'settings') > -1
572
+ })
573
+
574
+ // ==========【监听数据】=============================================================================================
575
+
576
+ // #if ! IS_DEV
577
+
578
+ /**
579
+ * 监听表格宫格模式
580
+ */
581
+ watch(tableGrid, function(val) {
582
+ if (isCache) {
583
+ // 设置宫格模式缓存(永久缓存)
584
+ $n_storage.set('table:grid:' + cacheName, val, 0)
585
+ }
586
+ })
587
+
588
+ /**
589
+ * 监听表格可见列
590
+ */
591
+ watch(tableVisibleColumns, function(val) {
592
+ if (o.showVisibleColumns && isCache) {
593
+ // 设置监听表格可见列缓存(永久缓存)
594
+ $n_storage.set('table:visible_columns:' + cacheName, val, 0)
595
+ }
596
+ })
597
+
598
+ // #endif
599
+
600
+ /**
601
+ * 监听固定在右边的权限按钮列表
602
+ */
603
+ watch(tableFixedPowerBtns, function (lists) {
604
+
605
+ if (! hasPowerBtns.value) {
606
+ return
607
+ }
608
+
609
+ const index = $n_indexOf(tableVisibleColumns.value, 'settings')
610
+
611
+ // 如果有固定在右边的权限按钮列表
612
+ if ($n_isValidArray(lists)) {
613
+
614
+ // 如果设置不在可见列中
615
+ if (index === -1) {
616
+
617
+ // 如果非手机模式
618
+ if (! $q.platform.is.mobile) {
619
+
620
+ // 则将设置加入可见列中
621
+ tableVisibleColumns.value.push('settings')
622
+ }
623
+
624
+ // 否则在可见列中 && 如果是手机模式
625
+ } else if ($q.platform.is.mobile) {
626
+
627
+ // 则将设置从可见列中删除
628
+ tableVisibleColumns.value.splice(index, 1)
629
+ }
630
+
631
+ // 否则如果设置在可见列中
632
+ } else if (index > -1) {
633
+
634
+ // 则将设置从可见列中删除
635
+ tableVisibleColumns.value.splice(index, 1)
636
+ }
637
+
638
+ }, {
639
+ // 立即执行
640
+ immediate: true,
641
+ })
642
+
643
+ // ==========【方法】================================================================================================
644
+
645
+ /**
646
+ * 设置表格节点节点
647
+ */
648
+ function setTableRef(target) {
649
+ tableRef.value = target
650
+ }
651
+
652
+ /**
653
+ * 设置表格传参
654
+ */
655
+ function setQuery(query) {
656
+
657
+ if ($n_isValidObject(query)) {
658
+
659
+ query = $n_cloneDeep(query)
660
+
661
+ // 搜索参数键值数组
662
+ const searchQueryKey = []
663
+
664
+ // 搜索键值数组
665
+ const NSearchKeys = []
666
+ // 搜索数组
667
+ const NSearchValues = []
668
+
669
+ // 参数中是否有自定义搜索参数
670
+ const hasNSearch = $n_has(query, 'n_search')
671
+ if (hasNSearch) {
672
+ // 删除在搜索中存在的参数键值
673
+ $n_forIn(query.n_search, function (item, key) {
674
+ if ($n_has(query, key)) {
675
+ delete query[key]
676
+ }
677
+ })
678
+ }
679
+
680
+ // 如果允许从参数中获取搜索值
681
+ if (o.searchFromQuery) {
682
+
683
+ $n_forEach(rawSearchOptions, function (item, index) {
684
+
685
+ const valueItem = tableSearchValue.value[index]
686
+
687
+ // 如果传参在搜素 n_search 参数中
688
+ if (hasNSearch && $n_has(query.n_search, item.name)) {
689
+ const newSearchItem = query.n_search[item.name]
690
+ if ($n_isValidArray(newSearchItem)) {
691
+ valueItem[0].compare = newSearchItem[0].compare
692
+ valueItem[0].value = newSearchItem[0].value
693
+
694
+ if (newSearchItem.length > 1) {
695
+ valueItem[1].compare = newSearchItem[1].compare
696
+ valueItem[1].value = newSearchItem[1].value
697
+ }
698
+ }
699
+ // 设置搜索的 key
700
+ NSearchKeys.push(item.name)
701
+
702
+ // 如果传参在搜索参数中
703
+ } else if ($n_has(query, item.name)) {
704
+ // 设置单个搜索值
705
+ setItemValue(valueItem, $n_isRequired(query[item.name]) ? query[item.name] : '')
706
+ // 设置参数中搜索的 key
707
+ searchQueryKey.push(item.name)
708
+ }
709
+ })
710
+
711
+ $n_forEach(searchQueryKey, function (key) {
712
+ delete query[key]
713
+ })
714
+
715
+ if (hasNSearch) {
716
+ $n_forIn(query.n_search, function(item, key) {
717
+ if (
718
+ NSearchKeys.indexOf(key) === -1
719
+ && $n_isValidArray(item)
720
+ ) {
721
+ item[0].field = key
722
+ NSearchValues.push(item[0])
723
+
724
+ if (item.length > 1) {
725
+ item[1].field = key
726
+ NSearchValues.push(item[1])
727
+ }
728
+ }
729
+ })
730
+ }
731
+
732
+ } else {
733
+ $n_forIn(query.n_search, function(item, key) {
734
+ if ($n_isValidArray(item)) {
735
+ item[0].field = key
736
+ NSearchValues.push(item[0])
737
+ if (item.length > 1) {
738
+ item[1].field = key
739
+ NSearchValues.push(item[1])
740
+ }
741
+ }
742
+ })
743
+ }
744
+
745
+ if (NSearchValues.length) {
746
+ query.n_search = NSearchValues
747
+ } else if (hasNSearch) {
748
+ delete query.n_search
749
+ }
750
+
751
+ tableRequestQuery = query
752
+ return
753
+ }
754
+
755
+ tableRequestQuery = {}
756
+ }
757
+
758
+
759
+ /**
760
+ * 表格是否已加载
761
+ */
762
+ function isTableLoaded() {
763
+ return _isTableLoaded
764
+ }
765
+
766
+ /**
767
+ * 表格加载(只加载一次)
768
+ */
769
+ async function tableLoad() {
770
+
771
+ // 如果表格已加载过了
772
+ if (_isTableLoaded) {
773
+ // 则无任何操作
774
+ return
775
+ }
776
+
777
+ // 表格重新加载
778
+ await tableReload()
779
+ }
780
+
781
+ /**
782
+ * 表格重新加载
783
+ */
784
+ async function tableReload(params = null) {
785
+
786
+ // 表格已加载
787
+ _isTableLoaded = true
788
+
789
+ if (! $route.fullPath) {
790
+ return
791
+ }
792
+
793
+ // 请求表格合计
794
+ if (o.summary) {
795
+ isRequestSummary = true
796
+ }
797
+
798
+ // 请求表格数据
799
+ // $tableRef?.requestServerInteraction({
800
+ // pagination: o.pagination,
801
+ // })
802
+
803
+ await tableRequest(Object.assign({
804
+ pagination: o.pagination,
805
+ }, params))
806
+
807
+ // 清空表格已选数据
808
+ if (o.refreshResetSelected) {
809
+ tableSelected.value = []
810
+ }
811
+ }
812
+
813
+ /**
814
+ * 表格刷新
815
+ */
816
+ async function tableRefresh() {
817
+
818
+ if (! $route.fullPath) {
819
+ return
820
+ }
821
+
822
+ // 请求表格合计
823
+ if (o.summary) {
824
+ isRequestSummary = true
825
+ }
826
+
827
+ // 请求表格数据
828
+ // $tableRef.requestServerInteraction()
829
+ await tableRequest({
830
+ pagination: tablePagination.value,
831
+ })
832
+
833
+ // 清空表格已选数据
834
+ if (o.refreshResetSelected) {
835
+ tableSelected.value = []
836
+ }
837
+ }
838
+
839
+ /**
840
+ * 表格搜索重置
841
+ */
842
+ function tableSearchReset(reload = true, params = null) {
843
+
844
+ const newValue = []
845
+
846
+ $n_forEach(rawSearchOptions, function (item, index) {
847
+ // 如果该搜索条件是隐藏的
848
+ if (item.hide) {
849
+ newValue.push(tableSearchValue.value[index])
850
+ // 否则为初始值
851
+ } else {
852
+ newValue.push(rawTableSearchValue[index])
853
+ }
854
+ })
855
+
856
+ // 还原表格搜索数据
857
+ tableSearchValue.value = $n_cloneDeep(newValue)
858
+
859
+ // 表格重新加载
860
+ if (reload) {
861
+ tableReload(params)
862
+ .finally()
863
+ }
864
+ }
865
+
866
+ /**
867
+ * 获取表格请求数据
868
+ */
869
+ function getTableRequestData(props, isSummary = undefined) {
870
+
871
+ // 解构数据
872
+ const {
873
+ // filter,
874
+ pagination: {
875
+ // 页码
876
+ page,
877
+ // 每页的数据条数
878
+ rowsPerPage,
879
+ // 排序字段
880
+ sortBy,
881
+ // 是否降序排列
882
+ descending,
883
+ }
884
+ } = props
885
+
886
+ // 请求数据
887
+ const data = {
888
+ // 页码
889
+ page,
890
+ // 每页的数据条数
891
+ per_page: rowsPerPage,
892
+ }
893
+
894
+ // 如果排序字段是有效值
895
+ if ($n_isValidValue(sortBy)) {
896
+ Object.assign(data, {
897
+ // 排序字段
898
+ order_by: sortBy,
899
+ // 是否降序排列
900
+ is_desc: descending ? 1 : 0,
901
+ })
902
+ }
903
+
904
+ // 合并参数
905
+ $n_forIn(Object.assign({}, rawQuery, tableRequestQuery, o.data), function(value, key) {
906
+ // 如果有值
907
+ if ($n_isRequired(value)) {
908
+ data[key] = value
909
+ }
910
+ })
911
+
912
+ // 获取搜索值
913
+ const search = formatValue(rawSearchOptions, tableSearchValue.value)
914
+ if ($n_isValidArray(search)) {
915
+ data.n_search = $n_has(data, 'n_search') ? $n_concat(data.n_search, search) : search
916
+ }
917
+
918
+ if ($n_isNil(isSummary)) {
919
+ isSummary = isRequestSummary
920
+ }
921
+
922
+ // 如果请求表格合计
923
+ if (isSummary) {
924
+ data.summary = 1
925
+ }
926
+
927
+ return data
928
+ }
929
+
930
+ /**
931
+ * 请求数据
932
+ */
933
+ async function tableRequest(props) {
934
+
935
+ // 加载
936
+ tableLoading.value = true
937
+
938
+ // 解构数据
939
+ const {
940
+ // filter,
941
+ pagination: {
942
+ // 页码
943
+ page,
944
+ // 每页的数据条数
945
+ rowsPerPage,
946
+ // 排序字段
947
+ sortBy,
948
+ // 是否降序排列
949
+ descending,
950
+ }
951
+ } = props
952
+
953
+ // http 请求参数
954
+ let httpOptions = Object.assign({
955
+ // 请求数据
956
+ url: $n_isValidString(o.url) ? o.url : $route.path,
957
+ // 请求数据
958
+ data: getTableRequestData(props, isRequestSummary),
959
+ // ~~~~~~ 先开启防抖, 如果后期遇到表格加载不出来的情况, 再关闭防抖
960
+ // 关闭防抖(允许重复请求)
961
+ debounce: false,
962
+ }, o.httpSettings)
963
+
964
+ // 下一步
965
+ async function next() {
966
+
967
+ const e = $n_isFunction(o.request)
968
+ // 如果有自定义请求方法
969
+ ? await $n_runAsync(o.request)({
970
+ // http 请求参数
971
+ httpOptions,
972
+ // 表格声明属性
973
+ props,
974
+ // 表格行数据
975
+ rows: tableRows,
976
+ // 表格已选数据
977
+ selected: tableSelected,
978
+ })
979
+ // 否则请求服务器
980
+ : await $n_http(httpOptions)
981
+
982
+ const { status, data: res } = e
983
+
984
+ // 返回结果数据
985
+ const resultData = Object.assign({
986
+ // 请求地址
987
+ requestUrl: httpOptions.url,
988
+ // 参数
989
+ options: httpOptions,
990
+ // 请求数据
991
+ requestData: httpOptions.data,
992
+ }, e)
993
+
994
+ // 请求后执行
995
+ if (await $n_runAsync(o.requestAfter)(resultData) === false) {
996
+ return
997
+ }
998
+
999
+ // 请求成功
1000
+ if (status) {
1001
+
1002
+ // 下一步
1003
+ function nextSuccess() {
1004
+
1005
+ const {
1006
+ // 返回数据
1007
+ rows,
1008
+ // 数据总数
1009
+ total,
1010
+ } = res
1011
+
1012
+ // 如果请求表格合计
1013
+ if (isRequestSummary) {
1014
+ const summary = $n_get(res, 'summary')
1015
+ tableSummary.value = $n_isValidObject(summary) ? summary : null
1016
+ }
1017
+
1018
+ // 更新页码
1019
+ tablePagination.value.page = page
1020
+ // 更新每页的数据条数
1021
+ tablePagination.value.rowsPerPage = rowsPerPage
1022
+ // 更新数据总数
1023
+ tablePagination.value.rowsNumber = total
1024
+ // 更新排序字段
1025
+ tablePagination.value.sortBy = sortBy
1026
+ // 更新是否降序排列
1027
+ tablePagination.value.descending = descending
1028
+
1029
+ // 格式化单条数据
1030
+ if ($n_isFunction(o.formatRow)) {
1031
+ $n_forEach(rows, function(row) {
1032
+ o.formatRow({
1033
+ row,
1034
+ rows: tableRows,
1035
+ selected: tableSelected,
1036
+ })
1037
+ })
1038
+ }
1039
+
1040
+ // 清除现有数据并添加新数据
1041
+ tableRows.value.splice(0, tableRows.value.length, ...rows)
1042
+
1043
+ // 取消请求表格合计
1044
+ isRequestSummary = false
1045
+
1046
+ // 取消加载
1047
+ tableLoading.value = false
1048
+ }
1049
+
1050
+ // 请求成功执行
1051
+ if (await $n_runAsync(o.requestSuccess)(Object.assign({ next: nextSuccess }, resultData)) === false) {
1052
+ return
1053
+ }
1054
+
1055
+ nextSuccess()
1056
+
1057
+ // 否则请求失败
1058
+ } else {
1059
+
1060
+ // 请求失败执行
1061
+ $n_run(o.requestFail)(resultData)
1062
+
1063
+ // 取消请求表格合计
1064
+ isRequestSummary = false
1065
+
1066
+ // 取消加载
1067
+ tableLoading.value = false
1068
+ }
1069
+
1070
+ // 请求后, 将表格列表置顶
1071
+
1072
+ await nextTick()
1073
+
1074
+ const $el = tableRef.value?.$el
1075
+ if ($el) {
1076
+ let dom = $el.querySelector('.q-virtual-scroll--vertical')
1077
+ if (dom) {
1078
+ dom.scrollTop = 0
1079
+ }
1080
+ dom = $el.querySelector('.q-table__grid-content')
1081
+ if (dom) {
1082
+ dom.scrollTop = 0
1083
+ }
1084
+ }
1085
+ }
1086
+
1087
+ if (o.requestBefore) {
1088
+
1089
+ // 请求前执行
1090
+ const resBefore = await $n_runAsync(o.requestBefore)({
1091
+ options: httpOptions,
1092
+ next,
1093
+ })
1094
+ if (resBefore !== void 0) {
1095
+ if (resBefore === false) {
1096
+ return
1097
+ }
1098
+ httpOptions = resBefore
1099
+ }
1100
+ }
1101
+
1102
+ // 下一步
1103
+ next()
1104
+ .finally()
1105
+ }
1106
+
1107
+ /**
1108
+ * 单击表格行
1109
+ */
1110
+ function _tableRowClick(e, row) {
1111
+
1112
+ // 如果选择类型为无
1113
+ if (tableSelection.value === 'none') {
1114
+ // 则无任何操作
1115
+ return
1116
+ }
1117
+
1118
+ const opt = {}
1119
+ opt[o.rowKey] = row[o.rowKey]
1120
+
1121
+ // 获取当前数据索引
1122
+ const itemIndex = $n_findIndex(tableSelected.value, opt)
1123
+
1124
+ // 如果不存在, 则添加
1125
+ if (itemIndex === -1) {
1126
+
1127
+ // 如果选择类型为单选
1128
+ if (tableSelection.value === 'single') {
1129
+ tableSelected.value = [ row ]
1130
+
1131
+ // 否则为多选
1132
+ } else {
1133
+ tableSelected.value.push(row)
1134
+ }
1135
+
1136
+ // 否则删除
1137
+ } else {
1138
+ tableSelected.value.splice(itemIndex, 1)
1139
+ }
1140
+ }
1141
+ function tableRowClick(...e) {
1142
+
1143
+ // 单击表格行
1144
+ _tableRowClick(...e)
1145
+
1146
+ // 如果有自定义单击事件
1147
+ if ($n_isFunction(o.rowClick)) {
1148
+ o.rowClick(...e)
1149
+ }
1150
+ }
1151
+
1152
+ /**
1153
+ * 双击表格行
1154
+ */
1155
+ function _tableRowDblclick(e, row) {
1156
+
1157
+ // 如果选择类型为无
1158
+ if (tableSelection.value === 'none') {
1159
+ // 则无任何操作
1160
+ return
1161
+ }
1162
+
1163
+ if (
1164
+ // 有权限
1165
+ hasPowr
1166
+ // 有双击的权限按钮
1167
+ && tableDbClickPowerBtn.value
1168
+ ) {
1169
+ $power.powerBtnClick(tableDbClickPowerBtn.value, [ row ])
1170
+ }
1171
+ }
1172
+ function tableRowDblclick(...e) {
1173
+
1174
+ // 双击表格行
1175
+ _tableRowDblclick(...e)
1176
+
1177
+ // 如果有自定义双击表格行事件
1178
+ if ($n_isFunction(o.tableRowDblclick)) {
1179
+ o.tableRowDblclick(...e)
1180
+ }
1181
+ }
1182
+
1183
+ /**
1184
+ * 设置表格搜索参数
1185
+ */
1186
+ async function setTableSearchOptions(format) {
1187
+ tableSearchOptions.value = await getOptions(rawSearchOptions, format)
1188
+ }
1189
+
1190
+ /**
1191
+ * 获取表格搜索值
1192
+ */
1193
+ function getTableSearchValue() {
1194
+ return formatValue(rawSearchOptions, tableSearchValue.value)
1195
+ }
1196
+
1197
+ /**
1198
+ * 是否有表格搜索值
1199
+ */
1200
+ // function hasTableSearchValue() {
1201
+ // return !! formatValue(rawSearchOptions, tableSearchValue.value).length
1202
+ // }
1203
+
1204
+ // ==========【返回】=================================================================================================
1205
+
1206
+ const resTable = {
1207
+ // 当前路由全路径
1208
+ routeFullPath: $route.fullPath,
1209
+ // 当前路由路径
1210
+ routePath: $route.path,
1211
+ // 当前路由参数
1212
+ routeQuery: $route.query,
1213
+ // 表格行唯一键值
1214
+ tableRowKey: o.rowKey,
1215
+ // 表格每页显示行数选项
1216
+ tableRowsPerPageOptions: o.rowsPerPageOptions,
1217
+
1218
+ // 表格节点
1219
+ tableRef,
1220
+ // 表格加载状态
1221
+ tableLoading,
1222
+ // 表格选择类型
1223
+ tableSelection,
1224
+ // 表格分隔栏
1225
+ tableSeparator,
1226
+ // 表格列数据(对象数组)
1227
+ tableColumns,
1228
+ // 表格是否显示可见列
1229
+ tableShowVisibleColumns,
1230
+ // 表格可见列
1231
+ tableVisibleColumns,
1232
+ // 表格行数据
1233
+ tableRows,
1234
+ // 表格翻页参数
1235
+ tablePagination,
1236
+ // 表格已选数据
1237
+ tableSelected,
1238
+ // 固定在右边的权限按钮列表
1239
+ tableFixedPowerBtns,
1240
+ // 是否显示固定在右边的权限按钮列表
1241
+ showTableFixed,
1242
+ // 表格图片
1243
+ tableImgs,
1244
+
1245
+ // 表格宫格
1246
+ tableGrid,
1247
+ // 表格合计
1248
+ tableSummary,
1249
+ // 表格搜索数据
1250
+ tableSearchValue,
1251
+ // 表格搜索参数
1252
+ tableSearchOptions,
1253
+
1254
+ // 设置表格节点
1255
+ setTableRef,
1256
+ // 设置表格传参
1257
+ setQuery,
1258
+ // 表格是否已加载
1259
+ isTableLoaded,
1260
+ // 表格加载(只加载一次)
1261
+ tableLoad,
1262
+ // 表格重新加载
1263
+ tableReload,
1264
+ // 表格刷新
1265
+ tableRefresh,
1266
+ // 表格搜索重置
1267
+ tableSearchReset,
1268
+ // 获取表格请求数据
1269
+ getTableRequestData,
1270
+ // 表格请求数据
1271
+ tableRequest,
1272
+ // 表格单击表格行
1273
+ tableRowClick,
1274
+ // 表格双击表格行
1275
+ tableRowDblclick,
1276
+ // 设置表格搜索参数
1277
+ setTableSearchOptions,
1278
+
1279
+ // 获取表格搜索值
1280
+ getTableSearchValue,
1281
+ // 是否有表格搜索值
1282
+ // hasTableSearchValue,
1283
+
1284
+ // 获取当前路由
1285
+ getRoute() {
1286
+ return $route
1287
+ },
1288
+ // 重新创建表格
1289
+ reCreate,
1290
+ }
1291
+
1292
+ if (hasPowr) {
1293
+ $power.update(function(data, _data) {
1294
+ _data.$table = resTable
1295
+ })
1296
+ }
1297
+
1298
+ // 提供可以被后代组件注入的值
1299
+ provide(NTableKey, resTable)
1300
+
1301
+ return resTable
1302
+ }
1303
+
1304
+ /**
1305
+ * 获取表格配置
1306
+ */
1307
+ function config(routePath, path, defaultValue) {
1308
+ const res = $n_get(tablesConfig, $n_slash(routePath, 'start', false) + (path ? '.' + path : ''), defaultValue)
1309
+ return $n_cloneDeep($n_isFunction(res) ? res() : res)
1310
+ }
1311
+
1312
+ /**
1313
+ * 获取表格浏览数据
1314
+ */
1315
+ async function getViewData(options) {
1316
+
1317
+ const {
1318
+ url,
1319
+ field,
1320
+ value: httpValue,
1321
+ data: httpData,
1322
+ } = Object.assign({
1323
+ field: 'id',
1324
+ value: [],
1325
+ data: {},
1326
+ }, options)
1327
+
1328
+ const _isValidValue = $n_isValidValue(httpValue)
1329
+ const hasSearch = $n_has(httpData, 'n_search')
1330
+ if (_isValidValue || $n_isValidArray(httpValue) || hasSearch) {
1331
+
1332
+ const n_search = []
1333
+ let per_page = 1
1334
+
1335
+ if (! hasSearch) {
1336
+
1337
+ let value
1338
+ let compare
1339
+
1340
+ if (_isValidValue) {
1341
+ compare = dicts.SEARCH_COMPARE_TYPE__EQUAL
1342
+ value = httpValue
1343
+ } else {
1344
+ compare = dicts.SEARCH_COMPARE_TYPE__IN
1345
+ value = $n_uniq(httpValue)
1346
+ per_page = value.length
1347
+ }
1348
+
1349
+ n_search.push({
1350
+ field,
1351
+ compare,
1352
+ value,
1353
+ })
1354
+ }
1355
+
1356
+ const { status, data } = await $n_http({
1357
+ url,
1358
+ // 头部请求
1359
+ headers: {
1360
+ // 添加头部查看请求
1361
+ Pview: 1,
1362
+ },
1363
+ data: Object.assign({
1364
+ n_search,
1365
+ page: 1,
1366
+ per_page,
1367
+ }, httpData),
1368
+ })
1369
+
1370
+ if (
1371
+ status
1372
+ && $n_isValidArray($n_get(data, 'rows'))
1373
+ ) {
1374
+ return data.rows
1375
+ }
1376
+ }
1377
+
1378
+ return []
1379
+ }
1380
+
1381
+ /**
1382
+ * 业务表格
1383
+ */
1384
+ const $table = {
1385
+ // 创建表格
1386
+ create,
1387
+ // 获取表格配置
1388
+ config,
1389
+
1390
+ // 获取表格浏览数据
1391
+ getViewData,
1392
+ }
1393
+
1394
+ export default $table