@94ai/nf-table 3.1.13

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.
@@ -0,0 +1,432 @@
1
+ <template>
2
+ <div class="nf-table-nf-column-check-drawer">
3
+ <!-- 左侧-可操作类目 START -->
4
+ <div class="nf-not-chosen">
5
+ <div class="nf-search-column">
6
+ <el-input
7
+ v-model="columnName"
8
+ size="small"
9
+ :placeholder="searchPlaceholder"
10
+ clearable
11
+ style="width: 308px;margin-bottom: 16px;"
12
+ prefix-icon="iconfont icon-sousuo"
13
+ @input="handleSearch"
14
+ />
15
+ <el-checkbox
16
+ v-if="fields.length"
17
+ v-model="checkAll"
18
+ :indeterminate="isIndeterminate"
19
+ @change="handleCheckAllChange"
20
+ >{{ getAllCheckDesc(checkedCount) }}</el-checkbox>
21
+ </div>
22
+ <div class="nf-select-column">
23
+ <div v-for="(item,index) in fields" :key="index + item.name" class="nf-column-category">
24
+ <el-checkbox-group v-model="showColumn">
25
+ <div class="nf-check-title">{{ item.name }}</div>
26
+ <el-checkbox
27
+ v-for="obj in item.list"
28
+ :key="obj.prop"
29
+ :label="obj.prop"
30
+ :disabled="unChangedColumn.includes(obj.prop)"
31
+ @change="(v) => handleCheckedChange(v, obj.prop)"
32
+ >{{ obj.addLabel || obj.label }}</el-checkbox>
33
+ </el-checkbox-group>
34
+ </div>
35
+ </div>
36
+ </div>
37
+ <!-- 左侧-可操作类目 END -->
38
+ <!-- 右侧-已选择类目 START -->
39
+ <div class="nf-already-chosen">
40
+ <div class="nf-selected-column">
41
+ <p>{{ getCheckedLengthDesc((showColumn.length - unChangedColumn.length)) }}</p>
42
+ <span v-if="showColumn.length - unChangedColumn.length" @click="handleCheckAllChange(false)">{{ clearDesc }}</span>
43
+ </div>
44
+ <div class="nf-drag-container">
45
+ <div
46
+ class="nf-fixed"
47
+ :style="{'margin-bottom': fixedColumn.length - unChangedColumn.length ? '8px' : '16px'}"
48
+ ><p> {{ fixedLeftDesc }} </p>
49
+ <draggable
50
+ v-model="fixedColumn"
51
+ v-bind="dragOptions"
52
+ class="nf-drag-column"
53
+ handle=".nf-handle"
54
+ :group="{ put: false }"
55
+ tag="ul"
56
+ @start="drag = true"
57
+ @end="drag = false"
58
+ >
59
+ <transition-group name="nf-slide-fade" tag="ul">
60
+ <template v-for="(dragElement, index) in fixedColumn" >
61
+ <li
62
+ v-if="!dragElement.initHidden && !unChangedColumn.includes(dragElement.prop)"
63
+ :key="dragElement.prop || index"
64
+ class="nf-drag-element"
65
+ >
66
+ <span class="nf-handle">
67
+ <i class="iconfont icon-ketuozhuai" />
68
+ <span>{{ dragElement.addLabel || dragElement.label }}</span>
69
+ </span>
70
+ <span>
71
+ <el-tooltip placement="top" :content="noFixedDesc">
72
+ <i
73
+ class="iconfont icon-buguding"
74
+ @click="handleFixed(false, dragElement.prop)"
75
+ />
76
+ </el-tooltip>
77
+ <i
78
+ class="iconfont icon-guanbi1"
79
+ @click="handleCheckedChange(false, dragElement.prop)"
80
+ />
81
+ </span>
82
+ </li>
83
+ </template>
84
+ </transition-group>
85
+ </draggable>
86
+ </div>
87
+ <div class="nf-not-fixed">
88
+ <p>{{ noFixedDesc }}</p>
89
+ <draggable
90
+ v-model="unFixedColumn"
91
+ v-bind="dragOptions"
92
+ class="nf-drag-column"
93
+ handle=".nf-handle"
94
+ tag="ul"
95
+ :group="{ put: false }"
96
+ :pull="false"
97
+ @start="drag = true"
98
+ @end="drag = false"
99
+ >
100
+ <transition-group name="nf-slide-fade" tag="ul">
101
+ <template v-for="(dragElement, index) in unFixedColumn">
102
+ <li v-if="!dragElement.initHidden" :key="dragElement.prop || index" class="nf-drag-element">
103
+ <span class="nf-handle">
104
+ <i class="iconfont icon-ketuozhuai nf-handle" />
105
+ <span>{{ dragElement.addLabel || dragElement.label }}</span>
106
+ </span>
107
+ <span>
108
+ <el-tooltip style="margin-left:20px" placement="top" :content="fixedLeftDesc">
109
+ <i class="iconfont icon-gudingzailieshou" @click="handleFixed(true, dragElement.prop)" />
110
+ </el-tooltip>
111
+ <i v-if="!unChangedColumn.includes(dragElement.prop)" class="iconfont icon-guanbi1" @click="handleCheckedChange(false, dragElement.prop)" />
112
+ </span>
113
+ </li>
114
+ </template>
115
+ </transition-group>
116
+ </draggable>
117
+ </div>
118
+ </div>
119
+ </div>
120
+ <!-- 右侧-已选择类目 END -->
121
+ </div>
122
+ </template>
123
+
124
+
125
+ <script lang="ts">
126
+ import { defineComponent } from 'vue-demi'
127
+
128
+ export default defineComponent({
129
+ name: "NfColumnCheckDrawer",
130
+ })
131
+ </script>
132
+
133
+ <script setup lang="ts">
134
+ import draggable from 'vuedraggable'
135
+ import {
136
+ ref,
137
+ computed,
138
+ onBeforeMount,
139
+ getCurrentInstance,
140
+ } from 'vue-demi'
141
+ import { deepClone } from '@94ai/common-utils'
142
+ import { CachedManager } from '@94ai/common-utils'
143
+
144
+ const instance = getCurrentInstance()?.proxy
145
+
146
+
147
+ const props = withDefaults(defineProps<{
148
+ searchPlaceholder?: string
149
+ fixedLeftDesc?: string
150
+ clearDesc?: string
151
+ noFixedDesc?: string
152
+ /**
153
+ * 默认category分类标题
154
+ */
155
+ basicFieldDesc?: string
156
+ selectEmptyWarningTips?: string
157
+ getCheckedLengthDesc?: Function
158
+ getAllCheckDesc?: Function
159
+ /**
160
+ * 列编辑显示开关
161
+ */
162
+ columnEdit?: boolean,
163
+ // 源序列
164
+ tableColumns?: Record<string, any>[],
165
+ storageKey?: string
166
+ operationPropKey?: string
167
+ }>(), {
168
+ selectEmptyWarningTips: '请选择至少一个选项',
169
+ searchPlaceholder: '搜索',
170
+ fixedLeftDesc: '固定在左侧',
171
+ clearDesc: '清除',
172
+ noFixedDesc: '不固定',
173
+ basicFieldDesc: '基础字段',
174
+ getCheckedLengthDesc: (length) => '已选(' + length + ')',
175
+ getAllCheckDesc: (length) => '全选(' + length + ')',
176
+ tableColumns: () => [],
177
+ columnEdit: false,
178
+ storageKey: 'nf-table-columns-cached-key',
179
+ operationPropKey: 'operation',
180
+ })
181
+
182
+ // 获取当前缓存中的动态数据列
183
+ const currentTable = ref<Record<string, any>[]>([])
184
+ // 可选列表的绑定的已选值
185
+ const showColumn = ref<string[]>([])
186
+ // 不可操作列
187
+ const unChangedColumn = ref<string[]>([])
188
+ // 固定列
189
+ const fixedColumn = ref<Record<string, any>[]>([])
190
+ // 不固定列
191
+ const unFixedColumn = ref<Record<string, any>[]>([])
192
+
193
+
194
+ onBeforeMount(() => {
195
+ currentTable.value = deepClone(props.tableColumns.filter(item => !item.destory)).sort((a,b) => a._index! - b._index! < 0 ? -1 : a._index! - b._index! > 0 ? 1 : 0) as Record<string, any>[]
196
+ // 兼容处理
197
+ currentTable.value.map(i => {
198
+ if (!i.attr) {
199
+ i.attr = {}
200
+ }
201
+ })
202
+
203
+ const list = props.tableColumns.filter(item => !item.destory)
204
+
205
+ // 可选列表 → 字段区分
206
+ setFields(list)
207
+ // 已选列表
208
+ updateColumn()
209
+ if (currentTable.value.findIndex((item) => !!item.type) > -1) {
210
+ unChangedColumn.value.push('selection')
211
+ }
212
+ if (currentTable.value.findIndex((item) => (item.prop || item.attr?.prop) === props.operationPropKey) > -1) {
213
+ unChangedColumn.value.push(props.operationPropKey)
214
+ }
215
+ if (currentTable.value.every(i => !i.initHidden)) {
216
+ showColumn.value = currentTable.value.map(i => i.prop)
217
+ checkedCount.value = list.length
218
+ checkAll.value = true
219
+ isIndeterminate.value = false
220
+ }
221
+ })
222
+
223
+ /**
224
+ * 可选列表 → 字段区分
225
+ * @description fields = [{ name: '基础字段', list: [{
226
+ * ...
227
+ * }] }, ...]
228
+ */
229
+ const fields = ref<{
230
+ name: string,
231
+ list: Record<string, any>[]
232
+ }[]>([])
233
+ const setFields = (tableColumns) => {
234
+ const obj: Record<string, any> = {}
235
+ tableColumns.map(item => {
236
+ const key = item.category || props.basicFieldDesc
237
+ if (!obj[key]) {
238
+ obj[key] = []
239
+ }
240
+ obj[key].push(item)
241
+ })
242
+ const items: {
243
+ name: string
244
+ list: Record<string, any>[]
245
+ }[] = []
246
+ for (const key in obj) {
247
+ items.push({
248
+ name: key,
249
+ list: obj[key]
250
+ })
251
+ }
252
+ fields.value = items
253
+ }
254
+
255
+ /**
256
+ * 更新已选列表
257
+ */
258
+ const checkedCount = ref(0) // 勾选的数量
259
+ const updateColumn = () => {
260
+ fixedColumn.value = []
261
+ unFixedColumn.value = []
262
+ currentTable.value.map(item => {
263
+ if (item.attr.fixed) {
264
+ fixedColumn.value.push(item)
265
+ } else {
266
+ unFixedColumn.value.push(item)
267
+ }
268
+ })
269
+ // 勾选状态
270
+ showColumn.value = currentTable.value.filter(i => !i.initHidden).map(item => item.prop)
271
+ // [全选 checkbox] 展示状态
272
+ if (columnName.value && fields.value.length) {
273
+ // 搜索时
274
+ checkedCount.value = searchValue.filter(i => !i.initHidden).length
275
+ checkAll.value = checkedCount.value === searchValue.length
276
+ isIndeterminate.value = checkedCount.value > 0 && checkedCount.value < searchValue.length
277
+ return
278
+ }
279
+ checkedCount.value = showColumn.value.length
280
+ checkAll.value = checkedCount.value === currentTable.value.length
281
+ isIndeterminate.value = checkedCount.value > 0 && checkedCount.value < currentTable.value.length
282
+ }
283
+
284
+ /**
285
+ * 搜索
286
+ */
287
+ const columnName = ref('')
288
+ let searchValue: Record<string, any>[] = [] // 搜索结果
289
+ const handleSearch = (str) => {
290
+ searchValue = []
291
+ currentTable.value.map(i => {
292
+ if ((i.addLabel || i.label).includes(str)) {
293
+ searchValue.push(i)
294
+ }
295
+ })
296
+ setFields(searchValue)
297
+ updateColumn()
298
+ }
299
+
300
+ /**
301
+ * 全选
302
+ * @param {Boolean} v 回调参数 - 是否全选
303
+ */
304
+ const checkAll = ref(false)
305
+ const isIndeterminate = ref(false) // 来自官方解释:indeterminate 属性用以表示 checkbox 的不确定状态,一般用于实现全选的效果
306
+ const handleCheckAllChange = (v) => {
307
+ // 搜索时,全选
308
+ if (columnName.value && fields.value.length) {
309
+ searchValue.map(item => {
310
+ if (!unChangedColumn.value.includes(item.prop)) {
311
+ item.initHidden = !v
312
+ // 清除列目时,清除固定属性
313
+ v ? '' : item.attr.fixed = false
314
+ }
315
+ })
316
+ updateColumn()
317
+ } else {
318
+ currentTable.value.map(item => {
319
+ if (!unChangedColumn.value.includes(item.prop)) {
320
+ item.initHidden = !v
321
+ // 清除列目时,清除固定属性
322
+ v ? '' : item.attr.fixed = false
323
+ }
324
+ })
325
+ updateColumn()
326
+ }
327
+ isIndeterminate.value = false
328
+ }
329
+
330
+ /**
331
+ * 单选/取消
332
+ * @param {Boolean} v 是否取消 true:选中; false: 取消
333
+ * @param {String} prop 当前选中列目的 prop
334
+ */
335
+ const handleCheckedChange = (v, prop) => {
336
+ currentTable.value.map(item => {
337
+ if (item.prop === prop) {
338
+ item.initHidden = !v
339
+ // 清除列目时,清除固定属性
340
+ v ? '' : item.attr.fixed = false
341
+ }
342
+ })
343
+ updateColumn()
344
+ }
345
+
346
+ /**
347
+ * 固定/取消固定
348
+ * @param {Boolean} v 是否取消 true:固定; false: 取消固定
349
+ * @param {String} prop 当前选中列目的 prop
350
+ */
351
+ const handleFixed = (v, prop) => {
352
+ const columnInfo = currentTable.value.find(item => item.prop === prop)
353
+ if (columnInfo) {
354
+ columnInfo.attr.fixed = v ? 'left' : false
355
+ }
356
+ updateColumn()
357
+ }
358
+
359
+ /**
360
+ * 确定
361
+ */
362
+ const syncTableColumnsToCookies = () => {
363
+ const newTableColumns = [...fixedColumn.value, ...unFixedColumn.value]
364
+ if (newTableColumns.filter(item => !item.type && item.prop !== props.operationPropKey && !item.initHidden).length === 0) {
365
+ // @ts-ignore
366
+ instance?.$openMessage?.warning({
367
+ message: props.selectEmptyWarningTips,
368
+ duration: 2000,
369
+ })
370
+ throw new Error(props.selectEmptyWarningTips)
371
+ }
372
+ const selectionPropItemIndex = newTableColumns.findIndex((item) => !!item.type)
373
+ if (selectionPropItemIndex > -1) {
374
+ newTableColumns.unshift(newTableColumns.splice(selectionPropItemIndex, 1)[0])
375
+ }
376
+ const operationPropItemIndex = newTableColumns.findIndex((item) => (item.prop || item.attr?.prop) === props.operationPropKey)
377
+
378
+ if (operationPropItemIndex > -1) {
379
+ newTableColumns.push(newTableColumns.splice(operationPropItemIndex, 1)[0])
380
+ }
381
+ const columnState = {}
382
+ const obj: Record<string, any> = {}
383
+ newTableColumns.map((i, index) => {
384
+ columnState[i.prop] = {
385
+ initHidden: i.initHidden || false,
386
+ fixed: i.attr?.fixed || false,
387
+ prop: i.prop,
388
+ index
389
+ }
390
+ obj[i.prop] = !columnState[i.prop].initHidden
391
+ })
392
+ if (props.columnEdit) {
393
+ CachedManager.syncCachedSet(props.storageKey, columnState)
394
+ }
395
+ return obj
396
+ }
397
+
398
+ /**
399
+ * 拖拽
400
+ * @description vueDraggable
401
+ */
402
+ const drag = ref(false)
403
+ const dragOptions = computed(() => {
404
+ return {
405
+ animation: 200,
406
+ group: 'description',
407
+ disabled: false,
408
+ ghostClass: 'ghost'
409
+ }
410
+ })
411
+
412
+ defineExpose({
413
+ syncTableColumnsToCookies,
414
+ setFields,
415
+ updateColumn,
416
+ handleSearch,
417
+ currentTable,
418
+ showColumn,
419
+ unChangedColumn,
420
+ fixedColumn,
421
+ unFixedColumn,
422
+ checkAll,
423
+ isIndeterminate,
424
+ columnName,
425
+ searchValue,
426
+ checkedCount,
427
+ fields,
428
+ dragOptions,
429
+ drag,
430
+ })
431
+ </script>
432
+
@@ -0,0 +1,20 @@
1
+ <script lang="ts">
2
+ import { defineComponent } from 'vue-demi'
3
+
4
+ export default defineComponent({
5
+ props: {
6
+ render: {
7
+ type: Function
8
+ },
9
+ row: {
10
+ type: Object,
11
+ default: () => ({})
12
+ }
13
+ },
14
+ render(h) {
15
+ // @ts-ignore
16
+ return this.render(h, this.row)
17
+ }
18
+ })
19
+
20
+ </script>