Blame view

src/components/selectTable/index.vue 7.61 KB
1 2 3 4 5 6
<!--
 * @Descripttion: 表格选择器组件
 * @version: 1.3
 * @Author: sakuya
 * @Date: 2021年6月10日10:04:07
 * @LastEditors: Please set LastEditors
renchao@pashanhoo.com committed
7
 * @LastEditTime: 2023-07-27 17:21:52
8 9 10 11
-->

<template>
  <el-select ref="select" v-model="defaultValue" :size="size" :clearable="clearable" :multiple="multiple" :collapse-tags="collapseTags"
12
    :collapse-tags-tooltip="collapseTagsTooltip" :filterable="filterable" :placeholder="placeholder" :disabled="disabled"
13 14 15 16 17 18 19 20 21
    @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">
22
            <template #default="scope"><span>{{scope.$index +1 }}</span></template>
23 24 25 26 27 28 29 30 31 32 33 34
          </el-table-column>
          <slot></slot>
        </el-table>
      </div>
    </template>
  </el-select>
</template>

<script>
  import config from "./tableSelect";
  export default {
    props: {
renchao@pashanhoo.com committed
35
      value: null,
36 37 38 39 40 41 42 43 44 45 46
      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" },
47 48 49
      props: { type: Object, default: () => { } },
      // 表格数据
      tableData: { type: Array, default: () => { [] } },
50 51 52 53 54 55 56 57
    },
    data () {
      return {
        loading: false,
        keyword: null,
        defaultValue: [],
        defaultProps: {
          label: config.props.label,
58
          value: config.props.value
59 60 61 62 63 64 65
        },
        formData: {}
      }
    },
    computed: {
    },
    watch: {
renchao@pashanhoo.com committed
66
      value: {
67
        handler () {
renchao@pashanhoo.com committed
68 69
          this.defaultValue = this.value
          console.log(this.value, 'this.value');
70 71 72 73 74 75 76
          this.autoCurrentLabel()
        },
        deep: true
      }
    },
    mounted () {
      this.defaultProps = Object.assign(this.defaultProps, this.props);
renchao@pashanhoo.com committed
77
      this.defaultValue = this.value
78 79 80
      this.autoCurrentLabel()
    },
    methods: {
yuanbo committed
81 82 83 84 85
      /**
       * @description: 表格显示隐藏回调
       * @param {*} visible
       * @author: renchao
       */
86 87 88 89 90 91 92 93
      visibleChange (visible) {
        if (visible) {
          this.formData = {}
          this.getData()
        } else {
          this.autoCurrentLabel()
        }
      },
yuanbo committed
94 95 96 97
      /**
       * @description: 获取表格数据
       * @author: renchao
       */
98 99
      async getData () {
        //表格默认赋值
100 101 102 103 104 105 106 107 108
        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) {
109
            var setrow = this.tableData.filter(item => item[this.defaultProps.value] === this.defaultValue[this.defaultProps.value])
110
            // this.$refs.table.setCurrentRow(setrow[0]);
111
          }
112 113
        }
        // this.$refs.table.setScrollTop(0)
114
      },
yuanbo committed
115 116 117 118
      /**
       * @description: 插糟表单提交
       * @author: renchao
       */
119 120 121
      formSubmit () {
        this.getData()
      },
yuanbo committed
122 123 124 125
      /**
       * @description: 自动模拟options赋值
       * @author: renchao
       */
126 127 128 129 130 131 132
      autoCurrentLabel () {
        this.$nextTick(() => {
          if (this.multiple) {
            this.$refs.select.selected.forEach(item => {
              item.currentLabel = item.value[this.defaultProps.label]
            })
          } else {
133 134 135
            if (this.defaultValue) {
              this.$refs.select.selectedLabel = this.defaultValue[this.defaultProps.label]
            }
136 137 138
          }
        })
      },
yuanbo committed
139 140 141 142 143 144
      /**
       * @description: 表格勾选事件
       * @param {*} rows
       * @param {*} row
       * @author: renchao
       */
145 146 147 148 149 150 151 152
      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()
renchao@pashanhoo.com committed
153
        this.$emit('update:value', this.defaultValue);
154 155
        this.$emit('change', this.defaultValue);
      },
yuanbo committed
156 157 158 159 160
      /**
       * @description: 表格全选事件
       * @param {*} rows
       * @author: renchao
       */
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
      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()
renchao@pashanhoo.com committed
179
        this.$emit('update:value', this.defaultValue);
180 181
        this.$emit('change', this.defaultValue);
      },
yuanbo committed
182 183 184 185 186
      /**
       * @description: click
       * @param {*} row
       * @author: renchao
       */
187 188 189 190 191 192 193
      click (row) {
        if (this.multiple) {
          //处理多选点击行
        } else {
          this.defaultValue = row
          this.$refs.select.blur()
          this.autoCurrentLabel()
194
          this.$refs.table.setCurrentRow(row);
renchao@pashanhoo.com committed
195
          this.$emit('update:value', this.defaultValue);
196 197 198
          this.$emit('change', this.defaultValue);
        }
      },
yuanbo committed
199 200 201 202 203
      /**
       * @description: tags删除后回调
       * @param {*} tag
       * @author: renchao
       */
204 205 206
      removeTag (tag) {
        var row = this.findRowByKey(tag[this.defaultProps.value])
        this.$refs.table.toggleRowSelection(row, false);
renchao@pashanhoo.com committed
207
        this.$emit('update:value', this.defaultValue);
208
      },
yuanbo committed
209 210 211 212
      /**
       * @description: 清空后的回调
       * @author: renchao
       */
213
      clear () {
renchao@pashanhoo.com committed
214
        this.$emit('update:value', this.defaultValue);
215
      },
yuanbo committed
216 217 218 219 220
      /**
       * @description: 关键值查询表格数据行
       * @param {*} value
       * @author: renchao
       */
221 222 223
      findRowByKey (value) {
        return this.tableData.find(item => item[this.defaultProps.value] === value)
      },
yuanbo committed
224 225 226 227
      /**
       * @description: 触发select隐藏
       * @author: renchao
       */
228 229 230
      blur () {
        this.$refs.select.blur();
      },
yuanbo committed
231 232 233 234
      /**
       * @description: 触发select显示
       * @author: renchao
       */
235 236 237 238 239 240 241 242 243 244 245 246 247 248
      focus () {
        this.$refs.select.focus();
      }
    }
  }
</script>
<style scoped>
  .sc-table-select__table {
    padding: 12px;
  }
  .sc-table-select__page {
    padding-top: 12px;
  }
</style>