inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
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
<template>
  <div slot="overlay" class="ant-dropdown-menu s-tool-column ant-dropdown-content">
    <div class="s-tool-column-header s-tool-column-item">
      <a-checkbox :indeterminate="indeterminate" :checked="checkAll" @change="onCheckAllChange">
        列展示
      </a-checkbox>
      <a @click="reset">重置</a>
    </div>
    <a-divider />
    <div class="ant-checkbox-group">
      <div>
        <draggable v-model="columnsSetting" animation="300" @end="emitColumnChange">
          <div class="s-tool-column-item" v-for="item in columnsSetting" :key="item.title">
            <div class="s-tool-column-handle" >
              <a-icon type="more"/>
              <a-icon type="more"/>
            </div>
            <a-checkbox v-model="item.checked" @change="onChange">{{ item.title }}</a-checkbox>
          </div>
        </draggable>
      </div>
    </div>
  </div>
</template>
 
<script>
  import draggable from 'vuedraggable'
 
  export default {
    props: {
      columns: {
        type: Array,
        default: () => ([])
      }
    },
    components: {
      draggable
    },
    data() {
      return {
        indeterminate: false,
        checkAll: true,
        columnsSetting: [],
        originColumns: []
      }
    },
    methods: {
      reset() {
        this.columnsSetting = JSON.parse(JSON.stringify(this.originColumns))
        this.indeterminate = false
        this.checkAll = true
        this.emitColumnChange()
      },
      onChange() {
        const checkedList = this.columnsSetting.filter(value => value.checked)
        this.indeterminate = !!checkedList.length && checkedList.length < this.columnsSetting.length
        this.checkAll = checkedList.length === this.columnsSetting.length
        this.emitColumnChange()
      },
      onCheckAllChange(e) {
        const val = e.target.checked
        Object.assign(this, {
          indeterminate: false,
          checkAll: val,
          columnsSetting: this.columns.map(value => ({ ...value, checked: val }))
        })
        this.emitColumnChange()
      },
      emitColumnChange() {
        this.$emit('columnChange', this.columnsSetting)
      }
    },
    mounted() {
      this.columnsSetting = this.columns.map(value => ({ ...value, checked: true }))
      this.originColumns = JSON.parse(JSON.stringify(this.columnsSetting))
    }
  }
</script>
 
<style lang="less" scoped>
 
</style>