123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <template>
- <div id="data-table">
- <el-table :data="data" border stripe size="small">
- <template v-for="(item, index) in fields">
- <el-table-column :key="index" align="center" :label="item.label" :prop="item.prop" :formatter="toFormatter"></el-table-column>
- </template>
- <template v-if="opera.length > 0">
- <el-table-column label="操作" align="center">
- <template v-slot="{ row, $index }">
- <template v-for="(item, index) in opera">
- <template v-if="display(item, row)">
- <el-tooltip :key="index" effect="dark" :content="item.label" placement="bottom">
- <el-button
- :key="index"
- type="text"
- :icon="item.icon || ''"
- size="mini"
- @click="handleOpera(row, item.method, item.confirm, item.methodZh)"
- ></el-button>
- </el-tooltip>
- </template>
- </template>
- </template>
- </el-table-column>
- </template>
- </el-table>
- </div>
- </template>
- <script>
- import _ from 'lodash';
- export default {
- name: 'data-table',
- props: {
- fields: { type: Array, required: true },
- data: { type: Array, required: true },
- opera: { type: Array, default: () => [] },
- },
- components: {},
- data: () => ({}),
- created() {},
- computed: {},
- methods: {
- toFormatter(row, column, cellValue, index) {
- let this_fields = this.fields.filter(fil => fil.prop === column.property);
- if (this_fields.length > 0) {
- let format = _.get(this_fields[0], `format`, false);
- if (format) {
- let res = format(cellValue);
- return res;
- } else return cellValue;
- }
- },
- handleOpera(data, method, confirm = false, methodZh = '操作') {
- if (confirm) {
- this.$confirm(`您确认${methodZh}该数据?`, '提示', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning',
- })
- .then(() => {
- this.$emit(method, data);
- })
- .catch(() => {});
- } else {
- this.$emit(method, data);
- }
- },
- display(item, row) {
- let display = _.get(item, `display`, true);
- if (display === true) return true;
- else {
- let res = display(row);
- return res;
- }
- },
- },
- };
- </script>
- <style lang="less" scoped></style>
|