【el-talbe表格封装】

el-talbe表格封装

一、组件封装

<template>
  <div class="headerTable">
    <div>
      <slot name="leftTopButton"/>
    </div>
    <div class="tableSortable">
      <el-table
        ref="table"
        v-loading="loading"
        element-loading-text="Loading"
        :data="tableData"
        :border="border"
        tooltip-effect="dark"
        style="width:100%"
        header-row-class-name="el-table_color"
        :fit="fit"
        :stripe="stripe"
        :height="height"
        :size="small"
        @row-click="rowChangeHandler"
        @selection-change="selectionChangeHandler"
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column
          v-for="(item, index) in tableLabel"
          :key="index"
          :width="item.width ? item.width : ''"
          :align="item.align"
          :label="item.label"
          :prop="item.param"
          :sortable="item.sortable ? 'custom' : false"
        >
          <template slot-scope="scope">
            <i
              v-if="index === 0"
              class="el-icon-s-operation"
            />
            <span v-if="item.render">
              {{ item.render(scope.row) }}
            </span>
            <span v-else>{{ scope.row[item.param] }}</span>
          </template>
        </el-table-column>
        <el-table-column
          v-if="tableOption.label"
          :width="tableOption.width"
          :label="tableOption.label"
        >
          <template slot-scope="scope">
            <el-button
              v-for="(item, index) in tableOption.options"
              :key="index"
              :type="item.type"
              :icon="item.icon"
              size="mini"
              @click="handleButton(item.action, scope)"
            >
              {{ item.label }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <slot name="pagination"/>
    </div>
  </div>
</template>

<script>
// import {parseTime} from "../utils";

import Sortable from 'sortablejs';

export default {
    name: 'TableCmp',
    props: {
    // 表格数据
        tableData: {
            type: Array,
            default: () => {
                return [];
            }
        },
        // 表格表头标题(相关的属性)
        tableLabel: {
            type: Array,
            default: () => {
                return [];
            }
        },
        // 操作按钮
        tableOption: {
            type: Object,
            default: () => {
                return {};
            }
        },
        loading: {
            type: Boolean
        },
        border: {
            type: Boolean,
            default: true
        },
        fit: {
            type: Boolean,
            default: true
        },
        stripe: {
            type: Boolean,
            default: true
        },
        height: {
            type: String,
            default: 'auto'
        },
        small: {
            type: String,
            default: 'mini'
        }
    },
    data() {
        return {
            // loadingLog: true,
            showBox: false
        };
    },
    computed: {
        loadingLog() {
            return this.loading;
        }
    },
    watch: {
        loadingLog() {
            if (this.tableData) {
                setTimeout(() => {
                    this.loadingLog = false;
                }, 1500);
            }
        }
    },
    mounted() {
        this.onDragEnd();
    },
    methods: {
        handleButton(action, scope) {
            this.$emit('handleFun', action, scope);
        },
        rowChangeHandler(row) {
            this.$emit('handleRowClick', row);
        },
        selectionChangeHandler(selected) {
            this.$emit('selectionChange', selected);
        },
        onDragEnd() {
            debugger;
            const that = this;
            // eslint-disable-next-line no-undef
            const tbody = document.querySelector('.tableSortable .el-table__body-wrapper tbody');
            Sortable.create(tbody, {
                draggable: '.tableSortable .el-table__row',
                onEnd({newIndex, oldIndex}) {
                    const currRow = that.tableData.splice(oldIndex, 1)[0];
                    that.$nextTick(() => {
                        that.tableData.splice(newIndex, 0, currRow);
                    });
                }
            });
        }
    }
};
</script>

<style scoped>
.headerTable {
  margin: 20px;
}
</style>

二、组件使用

<template>
  <div>
    <vuedraggable
      :table-data="tableData"
      :table-label="tableLabel"
      :table-option="tableOption"
      :loading="loading"
      :border="true"
      @handleFun="handleFun"
      @handleRowClick="handleRowClick"
      @selectionChange="selectionChange"
    >
      <template v-slot:leftTopButton>
        <el-button>Add Row</el-button>
      </template>
    </vuedraggable>
  </div>
</template>

<script>
import vuedraggable from '@/components/draggable/vuedraggable.vue';

export default {
    components: {
        vuedraggable
    },
    data() {
    // Generate mock table data
        const generateMockData = () => {
            const data = [];
            for (let i = 0; i < 10; i++) {
                data.push({
                    id: i + 1,
                    name: `User ${i + 1}`,
                    age: Math.floor(Math.random() * 50) + 20,
                    role: i % 2 === 0 ? 'Admin' : 'User'
                });
            }
            return data;
        };

        // Generate mock table labels
        const tableLabel = [
            {label: 'ID', param: 'id', align: 'center', sortable: true},
            {label: 'Name', param: 'name', align: 'center', sortable: false},
            {label: 'Age', param: 'age', align: 'center', sortable: true},
            {label: 'Role', param: 'role', align: 'center', sortable: false}
        ];

        // Define mock table options
        const tableOption = {
            label: 'Actions',
            width: 300,
            options: [
                {label: 'Edit', type: 'primary', icon: 'el-icon-edit', action: 'edit'},
                {label: 'Delete', type: 'danger', icon: 'el-icon-delete', action: 'delete'}
            ]
        };

        return {
            tableData: generateMockData(),
            tableLabel,
            tableOption,
            loading: false
        };
    },
    methods: {
        handleFun(action, scope) {
            switch (action) {
                case 'edit':
                    console.log('Edit row:', scope.row);
                    break;
                case 'delete':
                    console.log('Delete row:', scope.row);
                    break;
                default:
                    break;
            }
        },
        handleRowClick(row) {
            console.log('Clicked row:', row);
        },
        selectionChange(selected) {
            console.log('Selected rows:', selected);
        }
    }
};
</script>

视频预览

相关推荐
前端小小王33 分钟前
React Hooks
前端·javascript·react.js
苹果醋337 分钟前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
迷途小码农零零发42 分钟前
react中使用ResizeObserver来观察元素的size变化
前端·javascript·react.js
Hello.Reader1 小时前
深入解析 Apache APISIX
java·apache
娃哈哈哈哈呀1 小时前
vue中的css深度选择器v-deep 配合!important
前端·css·vue.js
菠萝蚊鸭1 小时前
Dhatim FastExcel 读写 Excel 文件
java·excel·fastexcel
旭东怪2 小时前
EasyPoi 使用$fe:模板语法生成Word动态行
java·前端·word
007php0072 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
∝请叫*我简单先生2 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
ssr——ssss2 小时前
SSM-期末项目 - 基于SSM的宠物信息管理系统
java·ssm