zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<template>
  <el-popover
    v-bind="popoverProps"
  >
    <template #reference>
      <el-button link>
        <el-icon style="font-size: 15px">
          <setting/>
        </el-icon>
      </el-button>
    </template>
    <template #default>
      <div class="wrapper" ref="refNode">
        <div v-for="(item,index) in fields" :key="item.prop + index" class="item">
          <div>{{ item.label }}</div>
          <div class="ignore-elements">
            <el-switch size="small" v-model="item.isShow" @change="onSwitch"></el-switch>
          </div>
        </div>
      </div>
    </template>
  </el-popover>
</template>
 
<script setup>
 
import * as tools from '@/utils/tools.js'
import Sortable from "sortablejs";
 
import {context} from '@/hooks';
 
const {useForceUpdate} = context;
 
import {useI18n} from 'vue-i18n';
 
const {t} = useI18n();
 
const refNode = ref();
 
const popoverProps = {
  trigger: 'focus',
  placement: 'bottom',
  title: t('components.table.TableSearch.ColSetting.title'),
  width: 280
}
 
 
const props = defineProps({
  fields: {
    type: Array,
    default: () => {
    }
  },
  subFields: {
    type: Function,
    default: () => {
    }
  }
});
 
const [$props, forceUpdate] = useForceUpdate(props);
 
const $forceUpdate = async (force) => {
  forceUpdate(force);
  fields.value = [...$props.fields];
}
 
const fields = ref([]);
 
const dragSortHandler = () => {
  Sortable.create(refNode.value, {
    animation: 500,
    forceFallback: true,
    filter: ".ignore-elements",
    onEnd({newIndex, oldIndex}) {
      const list = fields.value.map(e => ({...e}));
      list.forEach(e => {
        e.id = tools.uuid();
      })
      const currRow = list.splice(oldIndex, 1)[0];
      list.splice(newIndex, 0, currRow);
      fields.value = list;
      props.subFields([...list]);
    }
  });
};
 
const onSwitch = () => {
  props.subFields([...fields.value]);
}
 
onMounted(() => {
  dragSortHandler();
})
 
defineExpose({
  $forceUpdate
});
 
</script>
 
<style lang='scss' scoped>
.wrapper {
  .item {
    pointer-events: bounding-box;
    position: relative;
    top: 5px;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 10px;
    margin-bottom: 4px;
    box-sizing: border-box;
    //font-size: 13px;
    cursor: pointer;
    border-radius: 5px;
    font-size: var(--el-font-size-small);
    background: var(--el-color-primary-light-9);
  }
}
</style>