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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<template>
  <!-- 主列表 -->
  <el-table :data='tableData' :height='tableHeight' :show-header='showHeader' stripe @expand-change="expandSelect"
    :expand-row-keys="expands" @cell-click="handleCellClick" :empty-text="$t('common.noData')" class='el_tab_alage'
    :header-cell-style="cellHeaderStyle" :row-key='getRowKeys' @selection-change="handleSelectionChange">
    <!-- 单选框 -->
    <el-table-column align="center" width="50" label=""
      v-if="tableSelection.key === true && tableSelection.type === 'radio'">
      <template slot-scope="scope">
        <el-radio :label="scope.$index" v-model="radio"
          @change="handleTemplateRow(scope.$index, scope.row)">&nbsp;</el-radio>
      </template>
    </el-table-column>
    <!-- index索引 -->
    <el-table-column label="序号" type="index" width="50" align="center"
      v-if="tableSelection.key === true && tableSelection.type === 'index'"></el-table-column>
    <!-- 多选框 -->
    <el-table-column type="selection" width="50" align="center"
      v-if="tableSelection.key === true && tableSelection.type === 'selection'"></el-table-column>
    <!-- 列表表头-->
    <el-table-column type="expand" v-if="tableSelection.key === true && tableSelection.type === 'expand'">
      <template slot-scope="scope">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item :label="index.label" v-for="(index, item) in tableLabel" :key='item'
            v-show="index.type === 'expand'">
            <span>{{ scope.row[index.list] }}</span>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
    <template v-for="(index, item) in tableLabel">
      <el-table-column fit :align='index.tableAlign ? index.tableAlign : "center"' :key='item' :sortable='index.sort'
        v-if="index.type !== 'expand'" :label="index.label" :width="index.width"
        :show-overflow-tooltip="index.overflowShow === 'hidden' ? true : false" :prop="index.list">
        <template slot-scope="scope">
          <!-- 图片 -->
          <template v-if="index.type === 'image'">
            <el-image v-if="scope.row[index.list] !== ''" style="width: 100px; height: 50px;"
              :src="scope.row[index.list]">
            </el-image>
            <div v-else></div>
          </template>
          <!-- 产品图片 -->
          <template v-if="index.type === 'productImage'">
            <el-image v-if="scope.row[index.list] !== ''" style="width: 100px; height: 50px;"
              :src="getProductImageUrl(scope.row[index.list])">
            </el-image>
            <div v-else></div>
          </template>
          <!-- 头像 -->
          <template v-else-if="index.type === 'head'">
            <el-image v-if="!(scope.row[index.list] === '' || scope.row[index.list] === null)"
              style="width: 50px; height: 50px;" :src="scope.row[index.list]">
            </el-image>
            <div v-else></div>
          </template>
          <!-- 按钮 -->
          <template v-else-if="index.type === 'btn'">
            <el-button type="text" @click.native.prevent="index.method(scope.row, scope)">
              <u>{{ scope.row[index.list] }}</u>
            </el-button>
          </template>
          <!-- 下拉 -->
          <template v-else-if="index.type === 'select'">
            <el-select v-model="scope.row[index.list]" @change="changeType($event, scope.row, item)" size="medium">
              <el-option v-for="item in index.options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </template>
          <!-- 开关按钮 -->
          <template v-else-if="index.type === 'switch'">
            <div v-if="index.noSwitch(scope.row)">
              <el-switch @change="index.method(scope.row, scope)" v-model="scope.row[index.list]"
                :inactive-value="index.offValue !== null ? index.offValue : 'off'"
                :active-value="index.onValue ? index.onValue : 'on'" :inactive-text="index.offText ? index.offText : ''"
                :active-text="index.onText ? index.onText : ''"
                :inactive-color="index.offColor ? index.offColor : '#ff4949'"
                :active-color="index.onColor ? index.onColor : '#13ce66'"></el-switch>
            </div>
          </template>
          <!-- 内容自定义 -->
          <template v-else-if="index.type === 'html'">
            <div v-html="index.code(scope.row)" class="theHtml"></div>
          </template>
          <!-- 正常显示 -->
          <template v-else>
            <!-- {{scope.row[index.list]}} -->
            {{ scope.row[index.list] ? scope.row[index.list] : '-' }}
          </template>
        </template>
      </el-table-column>
    </template>
    <!-- 正常按钮操作 -->
    <el-table-column v-if="tableOption.value !== undefined" fit
      :align='tableOption.align ? tableOption.align : "center"' :label="tableOption.label"
      :fixed="tableOption.fixed ? tableOption.fixed : false" :width="tableOption.width">
      <template style="margin-left: 30px;" slot-scope="scope">
        <el-button v-for="(value, item) in filteredOptions(scope.row)" :key='item'
          :disabled="value.disabled ? value.disabled(scope.row) : false" :type="value.type ? value.type : 'text'"
          :style="value.style ? value.style : {}" :plain='value.plain ? value.plain : false'
          :round='value.round ? value.round : false' :size='value.size ? value.size : "medium"' :icon="value.icon"
          @click.native.prevent="value.method(scope.row, scope)">{{ value.label }}
        </el-button>
        <el-popover placement="top" trigger="hover"
          v-if="tableOption.isShowMore ? tableOption.isShowMore(scope.row) : false">
          <div class="popover-content">
            <el-button class="popover-button" v-for="(value, item) in filteredPopoverOptions(scope.row)" :key='item'
              :disabled="value.disabled ? value.disabled(scope.row) : false" :type="value.type ? value.type : 'info'"
              :style="value.style ? value.style : {}" :size='value.size ? value.size : "mini"'
              @click.native.prevent="value.method(scope.row, scope)">{{ value.label }}
            </el-button>
          </div>
          <el-button slot="reference" type="text"
            :style="tableOption.buttonStyle ? tableOption.buttonStyle : {}">{{ tableOption.buttonText }}</el-button>
        </el-popover>
      </template>
    </el-table-column>
  </el-table>
</template>
 
<script>
export default {
  data () {
    return {
      radio: '',
      cellHeaderStyle: {
        fontSize: "16px",
        color: "#606266"
      },
      expands: [],
      getRowKeys (row) {
        return row.id
      }
    }
  },
  props: {
    tableData: {
      type: Array,
      default: () => { }
    },
    tableHeight: {
      type: Number,
      default: () => {
        return null
      }
    },
    showHeader: {
      type: Boolean,
      default: () => {
        return true
      }
    },
    tableSelection: {
      type: Object,
      default: () => {
        return {
          key: false,
          type: '',
          detaile: false
        }
      }
    },
    tableLabel: {
      type: Array,
      default: () => { }
    },
    tableOption: {
      type: Object,
      default: () => {
        return {
          value: undefined
        }
      }
    }
  },
 
  methods: {
    handleSelectionChange (val) {
      this.$emit('onHandleSelectionChange', val)
    },
    handleTemplateRow (index, row) {
      this.$emit('onHandleTemplateRow', row)
    },
    changeType (event, row) {
      this.$emit('onChangeType', event, row)
    },
    expandSelect (row, expandedRows) {
      const that = this
      if (expandedRows.length) {
        that.expands = []
        if (row) {
          that.expands.push(row.id)
        }
      } else {
        that.expands = []
      }
    },
    handleCellClick (row, column) {
      this.$emit('onHandleCellClick', row, column)
    },
    getProductImageUrl (id) {
      return process.env.VUE_APP_BASE_API + 'saas/sysadmin/product/download/id?id=' + id
    },
    
    // ✅ 新增:过滤选项的方法
    filteredOptions(row) {
      if (!this.tableOption.options) return [];
      return this.tableOption.options.filter(value => {
        return value.show ? value.show(row) : true;
      });
    },
    
    // ✅ 新增:过滤弹出框选项的方法
    filteredPopoverOptions(row) {
      if (!this.tableOption.popoverOptions) return [];
      return this.tableOption.popoverOptions.filter(value => {
        return value.show ? value.show(row) : true;
      });
    }
  }
}
</script>