李旭东
2023-10-27 2d8dafdddd85653371e463ee43f960f2366d9313
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
<template>
  <div class="box">
    <!-- elTable -->
    <el-table v-loading="tableLoading" :data="tableData" :ref="tableRef" :size="tableSize" @row-click="handleRowClick"
      @row-dblclick="handleRowDblClick" @selection-change="handleSelectionChange" border stripe>
      <template v-for="(col, index) in tableColumns">
        <!-- 选择框 -->
        <el-table-column v-if="col.selection" :key="`selection${index}`" width="50" type="selection" align="center">
        </el-table-column>
 
        <!-- 序号 -->
        <el-table-column v-else-if="col.index" :key="`index${index}`" width="50" :label="col.name || '序号'" type="index"
          align="center">
        </el-table-column>
 
        <!-- 操作 -->
        <el-table-column v-else-if="col.operation" :key="`operation${col.name}`" :width="col.width" :label="col.name"
          align="center">
          <template #default="{ row }">
            <el-button v-for="subCol in col.value" :key="subCol.name" :class="subCol.class || ''"
              :icon="subCol.icon || ''" v-permission="subCol.permission || ''"
              @click="subCol.handleRow ? subCol.handleRow(row) : emptyFn">
              {{ subCol.name }}
            </el-button>
          </template>
        </el-table-column>
 
        <!-- 常规col -->
        <el-table-column v-else :key="col.name" :width="col.width" :label="col.name" :prop="col.key"
          :show-overflow-tooltip="col.showOverflowTip || false" :align="col.align || 'center'">
          <template #default="{ row }">
            <!-- slotのcol -->
            <template v-if="col.slot">
              <slot :name="col.slot" :row="row"></slot>
            </template>
            <!-- 需处理数据のcol -->
            <template v-else-if="col.formatter">
              {{ col.formatter(row) }}
            </template>
            <!-- 普通のcol -->
            <template v-else>
              {{ row[col.key] }}
            </template>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <!-- 分页组件 -->
    <cPagination v-if="pageTotal" :total="pageTotal" :page-num.sync="pageNum" :page-size.sync="pageSize"
      @change-page-num="changePageNum" @change-page-size="changePageSize" />
  </div>
</template>
<script>
import cPagination from "./Pagination"
export default {
  name: "cTable",
  data() {
    return {
      time: null,
      emptyFn: () => { }
    };
  },
  props: {
    tableData: {
      type: Array,
      default() {
        return [];
      }
    },
    tableHeight: {
      type: Number,
      default: null
    },
    tableLoading: {
      type: Boolean,
      default: false
    },
    tableRef: {
      type: String,
      default: "multipleTable"
    },
    tableSize: {
      type: String,
      default: "mini"
    },
    tableColumns: {
      type: Array,
      default() {
        return [];
      }
    },
    pageTotal: {
      type: Number
    },
    pageNum: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 10
    },
    pageChange: {
      type: Function,
      default: () => { }
    },
    handleSelection: {
      type: Function,
      default: () => { }
    },
    rowClick: {
      type: Function,
      default: () => { }
    },
  },
  components: {
    cPagination
  },
  methods: {
    changePageNum(val) {
      this.$emit("update:pageNum", val)
      this.pageChange()
    },
    changePageSize(val) {
      this.$emit("update:pageSize", val)
      if (val * (this.pageNum - 1) <= this.pageTotal) {
        this.pageChange()
      }
    },
    // 单击
    handleRowClick(row) {
      this.rowClick(row)
    },
    // 双击
    handleRowDblClick() { },
    handleSelectionChange(val) {
      this.handleSelection(val)
    }
  }
};
</script>
<style lang="scss" scoped>
.box {
  height: 100%;
  display: flex;
  flex-direction: column;
}
 
.el-table {
  overflow: auto;
  border: none;
  height: 100%;
}
</style>