index.vue 7.61 KB
<!--
 * @Descripttion: 表格选择器组件
 * @version: 1.3
 * @Author: sakuya
 * @Date: 2021年6月10日10:04:07
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-07-28 09:49:26
-->

<template>
  <el-select ref="select" v-model="defaultValue" :size="size" :clearable="clearable" :multiple="multiple" :collapse-tags="collapseTags"
    :collapse-tags-tooltip="collapseTagsTooltip" :filterable="filterable" :placeholder="placeholder" :disabled="disabled"
    @remove-tag="removeTag" @visible-change="visibleChange" @clear="clear">
    <template #empty>
      <div class="sc-table-select__table" :style="{width: tableWidth+'px'}" v-loading="loading">
        <div class="sc-table-select__header">
          <slot name="header" :form="formData" :submit="formSubmit"></slot>
        </div>
        <el-table ref="table" :data="tableData" :height="245" :highlight-current-row="!multiple" @row-click="click" @select="select" @select-all="selectAll">
          <el-table-column v-if="multiple" type="selection" width="45"></el-table-column>
          <el-table-column v-else type="index" width="45">
            <template #default="scope"><span>{{scope.$index +1 }}</span></template>
          </el-table-column>
          <slot></slot>
        </el-table>
      </div>
    </template>
  </el-select>
</template>

<script>
  import config from "./tableSelect";
  export default {
    props: {
      value: null,
      apiObj: { type: Object, default: () => { } },
      placeholder: { type: String, default: "请选择" },
      size: { type: String, default: "small" },
      clearable: { type: Boolean, default: false },
      multiple: { type: Boolean, default: false },
      filterable: { type: Boolean, default: false },
      collapseTags: { type: Boolean, default: false },
      collapseTagsTooltip: { type: Boolean, default: false },
      disabled: { type: Boolean, default: false },
      tableWidth: { type: Number, default: 400 },
      mode: { type: String, default: "popover" },
      props: { type: Object, default: () => { } },
      // 表格数据
      tableData: { type: Array, default: () => { [] } },
    },
    data () {
      return {
        loading: false,
        keyword: null,
        defaultValue: [],
        defaultProps: {
          label: config.props.label,
          value: config.props.value
        },
        formData: {}
      }
    },
    computed: {
    },
    watch: {
      value: {
        handler () {
          this.defaultValue = this.value
          console.log(this.value, 'this.value');
          this.autoCurrentLabel()
        },
        deep: true
      }
    },
    mounted () {
      this.defaultProps = Object.assign(this.defaultProps, this.props);
      this.defaultValue = this.value
      this.autoCurrentLabel()
    },
    methods: {
      /**
       * @description: 表格显示隐藏回调
       * @param {*} visible
       * @author: renchao
       */
      visibleChange (visible) {
        if (visible) {
          this.formData = {}
          this.getData()
        } else {
          this.autoCurrentLabel()
        }
      },
      /**
       * @description: 获取表格数据
       * @author: renchao
       */
      async getData () {
        //表格默认赋值
        if (this.multiple) {
          this.defaultValue.forEach(row => {
            var setrow = this.tableData.filter(item => item[this.defaultProps.value] === row[this.defaultProps.value])
            if (setrow.length > 0) {
              this.$refs.table.toggleRowSelection(setrow[0], true);
            }
          })
        } else {
          if (this.defaultValue) {
            var setrow = this.tableData.filter(item => item[this.defaultProps.value] === this.defaultValue[this.defaultProps.value])
            // this.$refs.table.setCurrentRow(setrow[0]);
          }
        }
        // this.$refs.table.setScrollTop(0)
      },
      /**
       * @description: 插糟表单提交
       * @author: renchao
       */
      formSubmit () {
        this.getData()
      },
      /**
       * @description: 自动模拟options赋值
       * @author: renchao
       */
      autoCurrentLabel () {
        this.$nextTick(() => {
          if (this.multiple) {
            this.$refs.select.selected.forEach(item => {
              item.currentLabel = item.value[this.defaultProps.label]
            })
          } else {
            if (this.defaultValue) {
              this.$refs.select.selectedLabel = this.defaultValue[this.defaultProps.label]
            }
          }
        })
      },
      /**
       * @description: 表格勾选事件
       * @param {*} rows
       * @param {*} row
       * @author: renchao
       */
      select (rows, row) {
        var isSelect = rows.length && rows.indexOf(row) !== -1
        if (isSelect) {
          this.defaultValue.push(row)
        } else {
          this.defaultValue.splice(this.defaultValue.findIndex(item => item[this.defaultProps.value] == row[this.defaultProps.value]), 1)
        }
        this.autoCurrentLabel()
        this.$emit('update:value', this.defaultValue);
        this.$emit('change', this.defaultValue);
      },
      /**
       * @description: 表格全选事件
       * @param {*} rows
       * @author: renchao
       */
      selectAll (rows) {
        var isAllSelect = rows.length > 0
        if (isAllSelect) {
          rows.forEach(row => {
            var isHas = this.defaultValue.find(item => item[this.defaultProps.value] == row[this.defaultProps.value])
            if (!isHas) {
              this.defaultValue.push(row)
            }
          })
        } else {
          this.tableData.forEach(row => {
            var isHas = this.defaultValue.find(item => item[this.defaultProps.value] == row[this.defaultProps.value])
            if (isHas) {
              this.defaultValue.splice(this.defaultValue.findIndex(item => item[this.defaultProps.value] == row[this.defaultProps.value]), 1)
            }
          })
        }
        this.autoCurrentLabel()
        this.$emit('update:value', this.defaultValue);
        this.$emit('change', this.defaultValue);
      },
      /**
       * @description: click
       * @param {*} row
       * @author: renchao
       */
      click (row) {
        if (this.multiple) {
          //处理多选点击行
        } else {
          this.defaultValue = row
          this.$refs.select.blur()
          this.autoCurrentLabel()
          this.$refs.table.setCurrentRow(row);
          this.$emit('update:value', this.defaultValue);
          this.$emit('change', this.defaultValue);
        }
      },
      /**
       * @description: tags删除后回调
       * @param {*} tag
       * @author: renchao
       */
      removeTag (tag) {
        var row = this.findRowByKey(tag[this.defaultProps.value])
        this.$refs.table.toggleRowSelection(row, false);
        this.$emit('update:value', this.defaultValue);
      },
      /**
       * @description: 清空后的回调
       * @author: renchao
       */
      clear () {
        this.$emit('update:value', this.defaultValue);
      },
      /**
       * @description: 关键值查询表格数据行
       * @param {*} value
       * @author: renchao
       */
      findRowByKey (value) {
        return this.tableData.find(item => item[this.defaultProps.value] === value)
      },
      /**
       * @description: 触发select隐藏
       * @author: renchao
       */
      blur () {
        this.$refs.select.blur();
      },
      /**
       * @description: 触发select显示
       * @author: renchao
       */
      focus () {
        this.$refs.select.focus();
      }
    }
  }
</script>
<style scoped>
  .sc-table-select__table {
    padding: 12px;
  }
  .sc-table-select__page {
    padding-top: 12px;
  }
</style>