当前位置:   article > 正文

vue3封装Element表格_vue-element表格封装

vue-element表格封装

请添加图片描述

  1. 配置表头
  2. 配置多选
  3. 配置序号
  4. 自定义操作列按钮

封装表格 Table.vue

<template>
  <el-table
    :data="tableData"
    width="100%"
    :maxHeight="maxHeight"
    v-bind="$attrs"
    @selection-change="handleSelectChange"
    @row-click="handleRowClick"
  >
    <!-- 是否需要多选列表的情况 -->
    <el-table-column
      v-if="haveCheckBox"
      align="center"
      type="selection"
      fixed="left"
      :reserveSelection="reserveSelection"
    />
    <!-- 是否需要展示序号 -->
    <el-table-column
      v-if="haveIndex"
      label="序号"
      align="center"
      type="index"
      width="60px"
    />
    <el-table-column
      v-for="(column, index) in columns"
      :key="index"
      :align="column.align || 'left'"
      v-bind="column"
      min-width="100px"
    >
      <template #default="scope">
        <template v-if="column.operate && column.operate.length">
          <el-button
            v-for="operate in column.operate"
            :key="index"
            :icon="operate.icon"
            @click.stop="operate.click(scope.row)"
            :type="operate.type"
            >{{ operate.label }}
          </el-button>
        </template>
        <slot v-else :name="column.slotName" :row="scope.row">
          {{ scope.row[column.prop] }}
        </slot>
      </template>
    </el-table-column>
    <slot />
  </el-table>
</template>

<script lang="ts" setup>
const props = defineProps({
  tableData: {
    type: Array,
    default() {
      return [];
    },
  },
  haveCheckBox: {
    type: Boolean,
    default: false,
  },
  haveIndex: {
    type: Boolean,
    default: false,
  },
  columns: {
    type: Array,
    default() {
      return [];
    },
    required: true,
  },
  maxHeight: {
    type: [Number, String],
    default: "50vh",
  },
  reserveSelection: {
    type: Boolean,
    default: false,
  },
});
const emit = defineEmits(["select-change", "row-click"]);
// 勾选表格数改变触发的函数
const handleSelectChange = (val) => {
  emit("select-change", val);
};
// 表格行内点击
const handleRowClick = (row, column, event) => {
  emit("row-click", row, column, event);
};
</script>

  • 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

调用组件示例

<template>
  <Table
    :tableData="tableData"
    :haveCheckBox="true"
    :haveIndex="true"
    :columns="tableColumn"
    :stripe="true"
    :border="true"
    @select-change="handleSelectChange"
    @row-click="handleRowClick"
  />
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";

const tableData = ref([
  {
    date: "2016-05-03",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
    userid: "123456789",
    username: "admin",
    password: "123456",
    role: "管理员",
    status: "正常",
    createTime: "2023-03-01 12:00:00",
    updateTime: "2023-03-01 12:00:00",
    remark: "备注",
  },
  {
    date: "2016-05-02",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
    userid: "123456789",
    username: "admin",
    password: "123456",
    role: "管理员",
    status: "正常",
    createTime: "2023-03-01 12:00:00",
    updateTime: "2023-03-01 12:00:00",
    remark: "备注",
  },
  {
    date: "2016-05-04",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
    userid: "123456789",
    username: "admin",
    password: "123456",
    role: "管理员",
    status: "正常",
    createTime: "2023-03-01 12:00:00",
    updateTime: "2023-03-01 12:00:00",
    remark: "备注",
  },
  {
    date: "2016-05-01",
    name: "Tom",
    address: "No. 189, Grove St, Los Angeles",
    userid: "123456789",
    username: "admin",
    password: "123456",
    role: "管理员",
    status: "正常",
    createTime: "2023-03-01 12:00:00",
    updateTime: "2023-03-01 12:00:00",
    remark: "备注",
  },
]);

const tableColumn = reactive([
  {
    prop: "date",
    label: "日期",
    width: "180",
    align: "center",
  },
  {
    prop: "name",
    label: "姓名",
    width: "180",
    align: "center",
  },
  {
    prop: "address",
    label: "地址",
    width: "280",
    align: "left",
  },
  {
    prop: "userid",
    label: "用户ID",
    width: "180",
    align: "center",
  },
  {
    prop: "username",
    label: "用户名",
    width: "180",
    align: "center",
  },
  {
    prop: "password",
    label: "密码",
    width: "180",
    align: "center",
  },
  {
    prop: "role",
    label: "角色",
    width: "180",
    align: "center",
  },
  {
    prop: "status",
    label: "状态",
    width: "180",
    align: "center",
  },
  {
    prop: "createTime",
    label: "创建时间",
    width: "180",
    align: "center",
  },
  {
    prop: "updateTime",
    label: "更新时间",
    width: "180",
    align: "center",
  },
  {
    prop: "remark",
    label: "备注",
    width: "180",
    align: "center",
  },
  {
    prop: "operation",
    label: "操作",
    width: "280",
    align: "center",
    fixed: "right",
    operate: [
      {
        label: "编辑",
        icon: 'Edit',
        type: "primary",
        click: (row: any) => {
          ElMessage.success("点击了编辑" + row.name);
        }
      },
      {
        label: "删除",
        icon: 'Delete',
        type: "danger",
        click: (row: any) => {
          ElMessage.error("点击了删除" + row.name);
        }
      }
    ]
  },
]);

const handleSelectChange = (selection: any) => {
  ElMessage.success("选择了" + selection[0].name);
  console.log(selection);
};
const handleRowClick = (row, column, event) => {
  ElMessage.success("点击了" + row.name);
  console.log(row);
  console.log(column);
  console.log(event);
};
</script>
<style lang="scss" scoped></style>

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/684248
推荐阅读
相关标签
  

闽ICP备14008679号