riku
2025-09-18 c1d2051abc8ca88cd07f0d7c56c0dbf8165d5c33
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
<template>
  <el-row ref="searchRef">
    <FYSearchBar @search="onSearch">
      <template #options v-if="$slots.options">
        <slot name="options"></slot>
      </template>
      <template #buttons v-if="$slots.buttons">
        <slot name="buttons"></slot>
      </template>
    </FYSearchBar>
  </el-row>
  <el-row ref="expandRef" justify="space-between">
    <el-col span="22">
      <slot name="options-expand"></slot>
    </el-col>
    <el-col span="2">
      <el-space :wrap="false">
        <el-text size="small">字体</el-text>
        <el-radio-group v-model="fontSize" size="small">
          <el-radio-button value="small">小</el-radio-button>
          <el-radio-button value="default">中</el-radio-button>
          <el-radio-button value="large">大</el-radio-button>
        </el-radio-group>
      </el-space>
    </el-col>
  </el-row>
  <div ref="expand2Ref">
    <slot name="options-expand2"></slot>
  </div>
  <el-table
    v-bind="$attrs"
    ref="tableRef"
    :data="tableData"
    v-loading="loading"
    table-layout="fixed"
    :row-class-name="tableRowClassName"
    :height="tableHeight"
    :size="fontSize"
    @cell-click="cellClick"
    :cell-class-name="cellClassName"
    @paste="handlePaste"
    @sort-change="handleSortChange"
    :show-overflow-tooltip="true"
    border
  >
    <slot name="table-column" :size="fontSize"></slot>
  </el-table>
 
  <el-pagination
    v-if="pagination"
    ref="paginationRef"
    class="el-pagination"
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :page-sizes="[10, 20, 50, 100]"
    :background="true"
    layout="total, sizes, prev, pager, next, jumper"
    :total="total"
  />
</template>
 
<script>
/**
 * 数据表格组件
 * 布局从上往下包括了三个部分
 * 1. 查询条件FYSearchBar
 * 2. 数据表格el-table
 * 3. 分页组件el-pagination
 * 使用时需要在<slot #options>中添加自定义查询选项,在<slot #table-column>中添加自定义表格列,同时实现触发函数search
 */
export default {
  inject: ['contentMaxHeight'],
  props: {
    rowClassName: undefined,
    cellClassName: Function || String,
    pagination: {
      type: Boolean,
      default: true
    },
    // '' | 'small' | 'default' | 'large'
    size: {
      type: String,
      default: 'default'
    },
    data: {
      type: Array,
      default: () => []
    },
    totalCount: {
      type: Number,
      default: 0
    },
    // 额外的高度,用于计算表格高度
    extraHeight: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      tableHeight: '500',
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 20,
      loading: false,
      fontSize: 'default'
    };
  },
  emits: ['search', 'cellClick', 'tablePaste', 'sortChange'],
  watch: {
    currentPage(nValue, oValue) {
      if (nValue != oValue) {
        this.onSearch();
      }
    },
    pageSize(nValue, oValue) {
      if (nValue != oValue) {
        this.onSearch();
      }
    },
    size: {
      handler(nValue, oValue) {
        if (nValue != oValue) {
          this.fontSize = nValue;
        }
      },
      immediate: true
    },
    data(nValue, oValue) {
      if (nValue != oValue) {
        this.tableData = nValue;
      }
    },
    totalCount(nValue, oValue) {
      if (nValue != oValue) {
        this.total = nValue;
      }
    },
    extraHeight: {
      handler(nValue, oValue) {
        if (nValue != oValue) {
          this.tableHeight = this.calcTableHeight();
        }
      },
    }
  },
  computed: {},
  methods: {
    /**
     * 表格数据查询,传递两组参数,分页信息和回调函数
     * 分页信息包括当前页码currentPage和每页数据量pageSize
     * 回调函数接收一个对象,包括表格数据数组data和数据总数total
     */
    onSearch() {
      this.loading = true;
      this.$emit(
        'search',
        {
          currentPage: this.currentPage,
          pageSize: this.pageSize
        },
        (res) => {
          if (res) {
            if (res.data) {
              this.tableData = res.data;
            }
            if (res.total) {
              this.total = res.total;
            }
          }
          this.loading = false;
          this.doLayout();
        }
      );
    },
    calcTableHeight() {
      const h1 = this.$refs.searchRef.$el.offsetHeight;
      const h2 = this.$refs.paginationRef
        ? this.$refs.paginationRef.$el.offsetHeight
        : 0;
      const h3 = this.$refs.expandRef.$el.offsetHeight;
      const h4 = this.$refs.expand2Ref.offsetHeight;
 
      const h = h1 + h2 + h3 + h4 + this.extraHeight;
      return this.contentMaxHeight.value - h + 'px';
      // return `calc(100vh - ${h}px - 60px - var(--el-main-padding) * 2)`;
    },
    tableRowClassName({ row }) {
      if (this.rowClassName) {
        if (typeof this.rowClassName == 'string') {
          return this.rowClassName;
        } else if (typeof this.rowClassName == 'function') {
          return this.rowClassName({ row });
        }
      } else {
        return row.extension1 != '0' ? 'online-row' : 'offline-row';
      }
    },
    cellClick(row, column, cell, event) {
      this.$emit('cellClick', row, column, cell, event);
    },
    handlePaste(event) {
      this.$emit('tablePaste', event);
    },
    doLayout() {
      this.$refs.tableRef.doLayout();
    },
    handleSortChange({ column, prop, order }) {
      this.$emit('sortChange', { column, prop, order });
    },
    clearSort() {
      this.$refs.tableRef.clearSort();
    }
  },
  mounted() {
    this.tableHeight = this.calcTableHeight();
    this.onSearch();
  }
};
</script>
 
<style>
.el-table .online-row {
  /* background-color: rgb(4, 202, 21); */
}
 
.el-table .offline-row {
  background-color: var(--el-disabled-bg-color);
  color: var(--el-disabled-text-color);
}
 
.el-table .cell {
  white-space: nowrap;
}
 
.el-pagination {
  background-color: var(--el-color-white);
  padding-top: 20px;
  border-top: 1px solid rgba(0, 0, 0, 0.096);
  /* margin-top: 2px; */
}
</style>