shikeying
2023-04-07 c192f834c4e092bc7c0f2722c343c25c1be619ab
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<template>
  <el-card shadow="never">
    <!--工具条-->
    <div class="table-tool-bar">
      <!--自定义工具-->
      <my-button
        v-for="(custom,idx) in myTable.tools.custom"
        :key="idx"
        :check-permission="custom.checkPermission"
        :name="custom.name"
        site="tools"
        @click="custom.click"
      />
      <!--列控制-->
      <el-popover
        placement="bottom"
        style="float: right;"
        trigger="click"
        width="150"
      >
        <el-form label-width="10px" style="max-height: 500px;overflow-y: auto;">
          <el-form-item label="">
            <el-checkbox-group v-model="checkColumns" @change="changeColumns">
              <el-checkbox v-for="(column,index) in myTable.columns" :key="index" :label="index">{{ column.title }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
        <el-button slot="reference" icon="el-icon-setting" size="mini">设置列</el-button>
      </el-popover>
    </div>
    <!--table列表-->
    <el-table
      ref="myTable"
      v-loading="myTable.loading"
      :data="myTable.data"
      row-key="id"
      :default-expand-all="myTable.expandAll"
      :tree-props="{children: 'children'}"
      :fit="false"
      :highlight-current-row="true"
      border
      size="medium"
    >
      <!--列内容-->
      <el-table-column
        v-for="(column, index) in myTable.columns"
        v-if="!column.hidden"
        :key="index"
        :align="column.align"
        :header-align="column.align"
        :label="column.title"
        :min-width="column.width"
        :sortable="column.sortable"
        :type="column.type"
        :width="column.width"
      >
        <template slot-scope="scope">
          <!--图片展示-->
          <template v-if="column.img">
            <preview-picture img-style="width:40px;height:40px;" :imgs="column.img(scope.row).imgs" />
          </template>
          <!--开关(生成开关按钮)-->
          <template v-else-if="column.switch">
            <my-switch
              :check-permission="column.checkPermission"
              :disabled="column.switch(scope.row).disabled"
              :value="column.switch(scope.row).value"
              @click="switchClick(column.switch(scope.row).click)"
            />
          </template>
          <!--格式化(显示颜色,内容,点击事件控制)-->
          <template v-else-if="column.formatter">
            <span
              slot="reference"
              :style="setStyle(column.formatter(scope.row))"
              class="content-text"
              @click="handleClick(column.formatter(scope.row))"
            >{{ column.formatter(scope.row).value }}</span>
          </template>
          <template v-else>
            <!--点击弹出单元格内容-->
            <el-popover
              :content="getPopoverContent(scope.row,column)"
              placement="top-start"
              trigger="click"
            >
              <span slot="reference" class="content-text">{{ scope.row[column.field] }}</span>
            </el-popover>
          </template>
        </template>
      </el-table-column>
 
      <!--操作列-->
      <el-table-column
        v-if="myTable.operation.show"
        :width="myTable.operation.width"
        align="center"
        label="操作"
      >
        <template slot-scope="scope">
          <template v-for="(operation, index) in myTable.operation.attr">
            <my-button-v2
              v-if="!(operation.hidden && operation.hidden(scope.row))"
              :key="index"
              :check-permission="operation.checkPermission"
              :name="operation.title"
              :type="operation.type"
              site="operation"
              @click="operation.events(scope.row)"
            />
          </template>
          <template v-if="myTable.operation.more && myTable.operation.more.length>0">
            <a-dropdown :trigger="['click']" style="margin-left: 5px;">
              <a-button size="small">
                更多
                <a-icon type="down" />
              </a-button>
              <a-menu slot="overlay">
                <template v-for="(m,i) in myTable.operation.more">
                  <a-menu-item :key="i" @click="m.events(scope.row)">
                    <my-button-v2
                      v-if="!(m.hidden && m.hidden(scope.row))"
                      :check-permission="m.checkPermission"
                      :name="m.title"
                    />
                  </a-menu-item>
                </template>
              </a-menu>
            </a-dropdown>
          </template>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
 
<script>
import myButton from '@/views/components/myButton'
import myButtonV2 from '@/views/components/myButtonV2'
import mySwitch from '@/views/components/mySwitch'
import request from '@/utils/request'
import previewPicture from '@/views/components/previewPicture'
 
export default {
  components: { myButton, mySwitch, previewPicture, myButtonV2 },
  props: {
    table: {
      type: Object,
      default() {
        return null
      }
    },
    filter: {
      type: Object,
      default() {
        return null
      }
    }
  },
  data() {
    return {
      /** table列表数据*/
      myTable: {
        url: '',
        params: {},
        expandAll: true, // 展开
        loading: false, // 加载效果
        size: 'small', // Table 的尺寸 medium / small / mini
        // 工具条
        tools: {
          custom: [] // 自定义工具条按钮
        },
        columns: [], // 列属性配置
        operation: { // 操作列
          show: false, // 显示操作列
          width: '250', // 宽度
          attr: [], // 操作
          more: []// 更多
        },
        data: [], // 列表数据
        paging: {
          show: true, // 显示分页
          // 分页信息
          page: {
            small: false,
            pageNumber: 1,
            pageSize: 10,
            total: 0
          }
        }
      },
      checkColumns: [] // 记录列控制中选中的列
    }
  },
  watch: {
    filter: {
      handler() {
        this.search()
      },
      deep: true
    }
  },
  created() {
    if (this.table != null) {
      Object.assign(this.myTable, this.table)
      this.myTable.columns.forEach((column, idx) => {
        if (!column.hidden) {
          this.checkColumns.push(idx)
        }
      })
    }
  },
  methods: {
    // 搜索
    search() {
      this.myTable.loading = true
      const params = Object.assign({}, this.filter)
      request({
        url: this.myTable.url,
        method: 'get',
        params: params
      }).then(res => {
        this.$set(this.myTable, 'data', res.data)
        this.myTable.loading = false
      })
    },
    // 获取设置列表弹层内容
    getPopoverContent(row, column) {
      if (column.formatterHigh) {
        return column.formatterHigh(row).value + ''
      } else if (column.formatter) {
        return column.formatter(row) + ''
      } else {
        return row[column.field] + ''
      }
    },
    // 单元格内容格式化样式
    setStyle(format) {
      let styles = ''
      if (format.click != undefined) {
        styles += 'cursor: pointer;'
      }
      if (format.type != undefined) {
        if (format.type == 'primary') {
          styles += 'color:#409EFF;'
        } else if (format.type == 'danger') {
          styles += 'color:#F56C6C;'
        } else if (format.type == 'success') {
          styles += 'color:#67C23A;'
        } else if (format.type == 'warning') {
          styles += 'color:#E6A23C;'
        } else if (format.type == 'info') {
          styles += 'color:#909399;'
        }
      }
      return styles
    },
    // formatter 点击事件
    handleClick(format) {
      if (format.click != undefined) {
        format.click()
      }
    },
    // 开关点击事件
    switchClick(click) {
      if (typeof click === 'function') {
        click()
      }
    },
    // 列控制
    changeColumns() {
      this.myTable.columns.forEach((column, idx) => {
        if (this.checkColumns.includes(idx)) {
          column.hidden = false
        } else {
          column.hidden = true
        }
        this.$set(this.myTable.columns, idx, column)
      })
    }
  }
}
</script>
 
<style scoped>
  /*详情表单*/
  .demo-table-expand .el-form-item{
    width: 100%;
  }
 
  /*列表内文字过长加省略*/
  .content-text{
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap
  }
 
  /*分页上边距*/
  .el-pagination {
    white-space: nowrap;
    padding: 2px 5px;
    color: #303133;
    font-weight: 700;
    margin-top: 10px;
  }
  .el-radio {
    margin-right: 10px;
  }
  .el-radio__label {
    font-size: 14px;
    padding-left: 2px !important;
  }
 
  .a-button-cus{
    font-size: 12px;
  }
  .ant-btn-sm {
    padding: 0px 7px;
    font-size: 12px;
    border-radius: 3px;
    height: 20px;
  }
  .ant-btn > .anticon + span, .ant-btn > span + .anticon {
    margin-left: unset;
  }
 
</style>