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