@94ai/nf-table 3.2.3 → 3.2.20

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.
@@ -49,11 +49,14 @@ export default defineComponent({
49
49
  <script lang="ts" setup>
50
50
  import {
51
51
  computed,
52
+ getCurrentInstance,
52
53
  onMounted,
53
54
  ref
54
55
  } from 'vue-demi'
56
+ import { deepClone } from 'packages/nf-table/lib/utils'
55
57
 
56
58
  const props = withDefaults(defineProps<{
59
+ storageKey?: string
57
60
  show?: boolean,
58
61
  title?: string,
59
62
  width?: string,
@@ -75,6 +78,7 @@ const props = withDefaults(defineProps<{
75
78
  center?: boolean,
76
79
  destroyOnClose?: boolean,
77
80
  }>(), {
81
+ storageKey: 'nf-table-columns-cached-key',
78
82
  show: false,
79
83
  width: '600px',
80
84
  title: '设置显示字段',
@@ -104,6 +108,7 @@ const emit = defineEmits<{
104
108
  }>()
105
109
 
106
110
 
111
+ const instance = getCurrentInstance()?.proxy
107
112
 
108
113
  const tableData = ref<Record<string, any>>({})
109
114
 
@@ -119,7 +124,7 @@ const visible = computed({
119
124
 
120
125
  const fields = computed(() => {
121
126
  const obj: Record<string, any> = {}
122
- props.tableColumns.map(item => {
127
+ deepClone(props.tableColumns).sort((a,b) => a.__index! - b.__index! < 0 ? -1 : a.__index! - b.__index! > 0 ? 1 : 0).map(item => {
123
128
  if (!item.type) {
124
129
  const key = item.category || '基础字段'
125
130
  if (!obj[key]) {
@@ -138,9 +143,19 @@ const fields = computed(() => {
138
143
  return fields
139
144
  })
140
145
 
141
-
142
146
  const submit = () =>{
143
147
  visible.value = false
148
+ const columnState = {}
149
+ props.tableColumns.map((i, index) => {
150
+ columnState[i.prop] = {
151
+ initHidden: !tableData.value[i.prop] || false,
152
+ fixed: i.attr?.fixed || false,
153
+ prop: i.prop,
154
+ index
155
+ }
156
+ })
157
+ // @ts-ignore
158
+ instance?.$cookies?.set(props.storageKey, JSON.stringify(columnState), -1)
144
159
  emit('save', tableData.value)
145
160
  }
146
161
 
@@ -0,0 +1,420 @@
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.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.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.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
+ PropType,
141
+ onMounted
142
+ } from 'vue-demi'
143
+ import { deepClone } from './utils'
144
+
145
+ const instance = getCurrentInstance()?.proxy
146
+
147
+
148
+ const props = withDefaults(defineProps<{
149
+ selectEmptyWarningTips?: string,
150
+ // 源序列
151
+ tableColumns?: Record<string, any>[],
152
+ storageKey?: string
153
+ operationPropKey?: string
154
+ searchPlaceholder?: string
155
+ fixedLeftDesc?: string
156
+ clearDesc?: string
157
+ noFixedDesc?: string
158
+ getCheckedLengthDesc?: Function
159
+ getAllCheckDesc?: Function
160
+ }>(), {
161
+ selectEmptyWarningTips: '请选择至少一个选项',
162
+ tableColumns: () => [],
163
+ storageKey: 'nf-table-columns-cached-key',
164
+ operationPropKey: 'operation',
165
+ searchPlaceholder: '搜索',
166
+ fixedLeftDesc: '固定在左侧',
167
+ clearDesc: '清除',
168
+ noFixedDesc: '不固定',
169
+ getCheckedLengthDesc: (length) => '已选(' + length + ')',
170
+ getAllCheckDesc: (length) => '全选(' + length + ')'
171
+ })
172
+
173
+ // 获取当前缓存中的动态数据列
174
+ const currentTable = ref<Record<string, any>[]>([])
175
+ // 可选列表的绑定的已选值
176
+ const showColumn = ref<string[]>([])
177
+ // 不可操作列
178
+ const unChangedColumn = ref<string[]>([])
179
+ // 固定列
180
+ const fixedColumn = ref<Record<string, any>[]>([])
181
+ // 不固定列
182
+ const unFixedColumn = ref<Record<string, any>[]>([])
183
+
184
+
185
+ onBeforeMount(() => {
186
+ currentTable.value = deepClone(props.tableColumns).sort((a,b) => a._index! - b._index! < 0 ? -1 : a._index! - b._index! > 0 ? 1 : 0) as Record<string, any>[]
187
+ // 兼容处理
188
+ currentTable.value.map(i => {
189
+ if (!i.attr) {
190
+ i.attr = {}
191
+ }
192
+ })
193
+
194
+ // 可选列表 → 字段区分
195
+ setFields(props.tableColumns)
196
+ // 已选列表
197
+ updateColumn()
198
+ if (currentTable.value.findIndex((item) => !!item.type) > -1) {
199
+ unChangedColumn.value.push('selection')
200
+ }
201
+ if (currentTable.value.findIndex((item) => (item.prop || item.attr?.prop) === props.operationPropKey) > -1) {
202
+ unChangedColumn.value.push(props.operationPropKey)
203
+ }
204
+ if (currentTable.value.every(i => !i.initHidden)) {
205
+ showColumn.value = currentTable.value.map(i => i.prop)
206
+ checkedCount.value = props.tableColumns.length
207
+ checkAll.value = true
208
+ isIndeterminate.value = false
209
+ }
210
+ })
211
+
212
+ /**
213
+ * 可选列表 → 字段区分
214
+ * @description fields = [{ name: '基础字段', list: [{
215
+ * ...
216
+ * }] }, ...]
217
+ */
218
+ const fields = ref<{
219
+ name: string,
220
+ list: Record<string, any>[]
221
+ }[]>([])
222
+ const setFields = (tableColumns) => {
223
+ const obj: Record<string, any> = {}
224
+ tableColumns.map(item => {
225
+ const key = item.category || '基础字段'
226
+ if (!obj[key]) {
227
+ obj[key] = []
228
+ }
229
+ obj[key].push(item)
230
+ })
231
+ const items: {
232
+ name: string
233
+ list: Record<string, any>[]
234
+ }[] = []
235
+ for (const key in obj) {
236
+ items.push({
237
+ name: key,
238
+ list: obj[key]
239
+ })
240
+ }
241
+ fields.value = items
242
+ }
243
+
244
+ /**
245
+ * 更新已选列表
246
+ */
247
+ const checkedCount = ref(0) // 勾选的数量
248
+ const updateColumn = () => {
249
+ fixedColumn.value = []
250
+ unFixedColumn.value = []
251
+ currentTable.value.map(item => {
252
+ if (item.attr.fixed) {
253
+ fixedColumn.value.push(item)
254
+ } else {
255
+ unFixedColumn.value.push(item)
256
+ }
257
+ })
258
+ // 勾选状态
259
+ showColumn.value = currentTable.value.filter(i => !i.initHidden).map(item => item.prop)
260
+ // [全选 checkbox] 展示状态
261
+ if (columnName.value && fields.value.length) {
262
+ // 搜索时
263
+ checkedCount.value = searchValue.filter(i => !i.initHidden).length
264
+ checkAll.value = checkedCount.value === searchValue.length
265
+ isIndeterminate.value = checkedCount.value > 0 && checkedCount.value < searchValue.length
266
+ return
267
+ }
268
+ checkedCount.value = showColumn.value.length
269
+ checkAll.value = checkedCount.value === currentTable.value.length
270
+ isIndeterminate.value = checkedCount.value > 0 && checkedCount.value < currentTable.value.length
271
+ }
272
+
273
+ /**
274
+ * 搜索
275
+ */
276
+ const columnName = ref('')
277
+ let searchValue: Record<string, any>[] = [] // 搜索结果
278
+ const handleSearch = (str) => {
279
+ searchValue = []
280
+ currentTable.value.map(i => {
281
+ if (i.label.includes(str)) {
282
+ searchValue.push(i)
283
+ }
284
+ })
285
+ setFields(searchValue)
286
+ updateColumn()
287
+ }
288
+
289
+ /**
290
+ * 全选
291
+ * @param {Boolean} v 回调参数 - 是否全选
292
+ */
293
+ const checkAll = ref(false)
294
+ const isIndeterminate = ref(false) // 来自官方解释:indeterminate 属性用以表示 checkbox 的不确定状态,一般用于实现全选的效果
295
+ const handleCheckAllChange = (v) => {
296
+ // 搜索时,全选
297
+ if (columnName.value && fields.value.length) {
298
+ searchValue.map(item => {
299
+ if (!unChangedColumn.value.includes(item.prop)) {
300
+ item.initHidden = !v
301
+ // 清除列目时,清除固定属性
302
+ v ? '' : item.attr.fixed = false
303
+ }
304
+ })
305
+ updateColumn()
306
+ } else {
307
+ currentTable.value.map(item => {
308
+ if (!unChangedColumn.value.includes(item.prop)) {
309
+ item.initHidden = !v
310
+ // 清除列目时,清除固定属性
311
+ v ? '' : item.attr.fixed = false
312
+ }
313
+ })
314
+ updateColumn()
315
+ }
316
+ isIndeterminate.value = false
317
+ }
318
+
319
+ /**
320
+ * 单选/取消
321
+ * @param {Boolean} v 是否取消 true:选中; false: 取消
322
+ * @param {String} prop 当前选中列目的 prop
323
+ */
324
+ const handleCheckedChange = (v, prop) => {
325
+ currentTable.value.map(item => {
326
+ if (item.prop === prop) {
327
+ item.initHidden = !v
328
+ // 清除列目时,清除固定属性
329
+ v ? '' : item.attr.fixed = false
330
+ }
331
+ })
332
+ updateColumn()
333
+ }
334
+
335
+ /**
336
+ * 固定/取消固定
337
+ * @param {Boolean} v 是否取消 true:固定; false: 取消固定
338
+ * @param {String} prop 当前选中列目的 prop
339
+ */
340
+ const handleFixed = (v, prop) => {
341
+ const columnInfo = currentTable.value.find(item => item.prop === prop)
342
+ if (columnInfo) {
343
+ columnInfo.attr.fixed = v ? 'left' : false
344
+ }
345
+ updateColumn()
346
+ }
347
+
348
+ /**
349
+ * 确定
350
+ */
351
+ const syncTableColumnsToCookies = () => {
352
+ const newTableColumns = [...fixedColumn.value, ...unFixedColumn.value]
353
+ if (newTableColumns.filter(item => !item.type && item.prop !== props.operationPropKey && !item.initHidden).length === 0) {
354
+ // @ts-ignore
355
+ instance?.$openMessage.warning({
356
+ message: props.selectEmptyWarningTips,
357
+ duration: 2000,
358
+ })
359
+ throw new Error(props.selectEmptyWarningTips)
360
+ }
361
+ const selectionPropItemIndex = newTableColumns.findIndex((item) => !!item.type)
362
+ if (selectionPropItemIndex > -1) {
363
+ newTableColumns.unshift(newTableColumns.splice(selectionPropItemIndex, 1)[0])
364
+ }
365
+ const operationPropItemIndex = newTableColumns.findIndex((item) => (item.prop || item.attr?.prop) === props.operationPropKey)
366
+
367
+ if (operationPropItemIndex > -1) {
368
+ newTableColumns.push(newTableColumns.splice(operationPropItemIndex, 1)[0])
369
+ }
370
+ const columnState = {}
371
+ const obj: Record<string, any> = {}
372
+ newTableColumns.map((i, index) => {
373
+ columnState[i.prop] = {
374
+ initHidden: i.initHidden || false,
375
+ fixed: i.attr?.fixed || false,
376
+ prop: i.prop,
377
+ index
378
+ }
379
+ obj[i.prop] = !columnState[i.prop].initHidden
380
+ })
381
+ // @ts-ignore
382
+ instance?.$cookies?.set(props.storageKey, JSON.stringify(columnState), -1)
383
+ return obj
384
+ }
385
+
386
+ /**
387
+ * 拖拽
388
+ * @description vueDraggable
389
+ */
390
+ const drag = ref(false)
391
+ const dragOptions = computed(() => {
392
+ return {
393
+ animation: 200,
394
+ group: 'description',
395
+ disabled: false,
396
+ ghostClass: 'ghost'
397
+ }
398
+ })
399
+
400
+ defineExpose({
401
+ syncTableColumnsToCookies,
402
+ setFields,
403
+ updateColumn,
404
+ handleSearch,
405
+ currentTable,
406
+ showColumn,
407
+ unChangedColumn,
408
+ fixedColumn,
409
+ unFixedColumn,
410
+ checkAll,
411
+ isIndeterminate,
412
+ columnName,
413
+ searchValue,
414
+ checkedCount,
415
+ fields,
416
+ dragOptions,
417
+ drag,
418
+ })
419
+ </script>
420
+