index.vue 5.33 KB
<template>
  <el-popover popper-class="gl-col" width="260" trigger="click">
    <div>
      <div class="gl-title">
        表头字段配置
        <el-tooltip content="可拖动位置" popper-class="gl-tip" placement="bottom" effect="light">
          <i class="gl-icon-circle-question"></i>
        </el-tooltip>
      </div>
      <div class="gl-content">
        <el-checkbox-group v-model="innerValue" @change="changeFn">
          <vuedraggable v-model="innerOptionsList" draggable=".item" group="tableClomns" animation="10" @start="startHook" @end="endHook">
            <template v-for="(item, index) in innerOptionsList">
              <template v-if="item.children">
                <subOption :key="'tableOpt' + index" :class="{ item: !disabledList.includes(item[attr]) }" :innerOptionsObj="item" :disabledList="disabledList" :attr="attr" :attrLabel="attrLabel" @start="onStartFn" @end="onEndFn" @change="subChange"></subOption>
              </template>
              <template v-else>
                <el-checkbox :key="'tableOpt' + index" :class="{ item: !disabledList.includes(item[attr]) }" :label="item[attr]" :disabled="disabledList.includes(item[attr])">{{ item[attrLabel] }}</el-checkbox>
              </template>
            </template>
          </vuedraggable>
        </el-checkbox-group>
      </div>
      <div class="gl-btns">
        <el-button plain size="small" class="btn-three">重置</el-button>
        <el-button type="primary" size="small">确定</el-button>
      </div>
    </div>
    <el-button plain size="small" class="btn-three" icon="gl-icon-cog" round slot="reference"></el-button>
  </el-popover>
</template>

<script>
import vuedraggable from 'vuedraggable'
import subOption from './sub.vue'
export default {
  name: 'GLTableOption',
  props: {
    // 可选列表
    optionsList: {
      type: Array,
      default: () => []
    },
    // 默认可选列表关联的属性
    attr: {
      type: String,
      default: 'prop'
    },
    // 默认可选列表关联的文本属性
    attrLabel: {
      type: String,
      default: 'label'
    }
  },
  data() {
    return {
      innerValue: [], //父项选中的值
      innerOptionsList: [],
      disabledList: [],
      subSelectArr: [], //子项选中的值
      allCheckbox: [] //总的勾选项(包含子项)
    }
  },
  mounted() {
    this.innerOptionsList = this.getOrderList()
    this.innerValue = this.getCheckAttrList('checkItem')
    this.disabledList = this.getCheckAttrList('disabledCheck')
    // setTimeout(() => {
    //   this.getCheckList()
    // })
  },
  methods: {
    // 获取选中项的对应的attr属性数组
    getCheckAttrList(propertyName) {
      return this.innerOptionsList.filter(item => item[propertyName]).map(item => item[this.attr])
    },
    //排序order(将禁用项提前)
    getOrderList() {
      return this.optionsList.sort((a, b) => {
        if (!a.order) a.order = 0
        if (!b.order) b.order = 0
        return b.order - a.order
      })
    },
    startHook() {
      //拖拽开始
      this.$emit('start')
    },
    endHook() {
      //拖拽结束
      this.$emit('end', this.innerOptionsList)
    },
    onStartFn() {
      this.startHook()
    },
    onEndFn(list) {
      // console.log('list: ', list)
      this.endHook()
    },
    // 当绑定值变化时触发的事件
    changeFn(selectList) {
      console.log('selectList: ', selectList)
      //选中的checkbox项
      let res = selectList.concat(this.subSelectArr)
      // 绑定值变化
      this.$emit('change', this.getCheckList(), res)
    },
    subChange(selectList) {
      this.subSelectArr = selectList
      let res = this.innerValue.concat(selectList)
      //总的勾选项
      this.allCheckbox = res
      // 绑定值变化
      this.$emit('change', this.getCheckList(), res)
    },
    // @vuese
    // 获取选中的列表配置项
    // @arg 参数1选择的列表(树),参数2选中的项(attr)
    getCheckList() {
      if (this.innerOptionsList.length === 0) return
      const resultArr = this.copySelectData(this.innerOptionsList, [])
      return resultArr
    },
    // 克隆选中的树状结构
    copySelectData(originData = [], resultData = []) {
      originData.forEach(item => {
        if (item.children) {
          let temp = {
            ...item,
            children: this.copySelectData([...item.children], [])
          }
          if (temp.children.length !== 0) {
            resultData.push(temp)
          }
        } else {
          if (this.allCheckbox.includes(item[this.attr])) {
            resultData.push({ ...item })
          }
        }
      })
      return resultData
    }
  },
  components: {
    vuedraggable,
    subOption
  }
}
</script>
<style lang="scss">
// 配置表单项
.gl-col {
  .gl-title {
  }
  .gl-content {
    margin-top: 10px;
    padding-top: 10px;
    padding-bottom: 10px;
    border-top: 1px solid #e8e8ea;
    border-bottom: 1px solid #e8e8ea;
    .el-checkbox {
      display: block;
    }
  }
  .gl-btns {
    padding-top: 16px;
    padding-bottom: 6px;
    text-align: right;
  }
}
.gl-tip.el-tooltip__popper.is-light {
  color: #666666;
  border: 0;
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.08);
  .popper__arrow {
    border-bottom-color: rgba(0, 0, 0, 0.05) !important;
    // box-shadow: 0 -4px 12px 0 rgba(0,0,0,0.1);
    &:after {
      // display: none;
      // border: 0;
    }
  }
}
</style>