ant design vue 之a-table表格的封装

在这里插入图片描述

ListTable组件

<template>
  <div class="list_table">
    <!-- :scroll="{ y: 540 }" loading -->
    <a-table
      :columns="tableTitle"
      :data-source="tableData"
      bordered
      size="small"
      :row-selection="rowSelection"
      :pagination="false"
    >
      <!-- 插槽 按钮 -->
      <template slot-scope="text,record" slot="action">
        <slot :scope="record" name="operation"></slot>
      </template>
    </a-table>
  </div>
</template>

<script>
/**
 * 组件:Table
 * 参数:
 * tableTitle:表头的数据;tableData:表格的数据;
 * rowSelection: 是否有复选框  -> :rowSelection="{ selectedRowKeys: selectedRowKeys,   onChange: onSelectChange }"
 * 插槽使用方式:
 * * tableTitle表头数据之中 添加 { title: "操作", dataIndex: "operate", width: 100, scopedSlots: { customRender: "action" }},
 * *  <ListTable><template v-slot:operation="scopeVal">
        <button class="blue" @click="view(scopeVal.scope)">详情</button>
        <button class="blue" @click="edit(scopeVal.scope)">编辑</button>
      </template></ListTable >
 */
export default {
  name: "ListTable",
  props: {
    tableTitle: {
      type: Array,
      default() {
        return [];
      },
      required: true,
    },
    tableData: {
      type: Array,
      default() {
        return [];
      },
      required: true,
    },
    rowSelection: {
      default: undefined,
    },
  },
  components: {},
  data() {
    return {};
  },
  methods: {
  },
};
</script>
<style  lang="scss" scoped>
.list_table {
  // 修改表头
  // ::v-deep .ant-table-thead > tr{
  //   background: #ff0!important;
  // }
}
</style>

使用组件

<template>
  <div>
    <ListTable
      :tableTitle="tableTitle"
      :tableData="tableData"
      :rowSelection="{
        selectedRowKeys: selectedRowKeys,
        onChange: onSelectChange,
      }"
    >
      <template v-slot:operation="scopeVal">
        <button class="blue" @click="view(scopeVal.scope)">详情</button>
        <button class="blue" @click="edit(scopeVal.scope)">编辑</button>
      </template>
    </ListTable>

    <ListTable
      :tableTitle="tableTitle2"
      :tableData="tableData"
      :rowSelection="{
        selectedRowKeys: selectedRowKeys2,
        onChange: onSelectChange2,
      }"
    >
      <template v-slot:operation="scopeVal">
        <button class="blue" @click="add(scopeVal.scope)">新增</button>
      </template>
    </ListTable>

    <ListTable :tableTitle="tableTitle3" :tableData="tableData"></ListTable>

    <div class="paginationWrap">
      <a-pagination
        class="paginationBtn"
        show-size-changer
        show-quick-jumper
        :total="100"
        :pageSizeOptions="['10', '20', '50', '100']"
        :current="currentPage"
        :pageSize="currentPageSize"
        @change="pageChange"
        @showSizeChange="pageSizeChange"
      />
    </div>
  </div>
</template>

<script>
import ListTable from "./childCom/ListTable.vue";
export default {
  name: "About1",
  components: {
    ListTable,
  },
  data() {
    return {
      tableTitle: [
        {
          title: "Name",
          dataIndex: "name",
          key: "name",
          scopedSlots: { customRender: "name" },
        },
        {
          title: "Age",
          dataIndex: "age",
          key: "age",
          width: 80,
        },
        {
          title: "Address",
          dataIndex: "address",
          key: "address 1",
          ellipsis: true,
        },
        {
          title: "Long Column Long Column Long Column",
          dataIndex: "address",
          key: "address 2",
          ellipsis: true,
        },
        {
          title: "Long Column Long Column",
          dataIndex: "address",
          key: "address 3",
          ellipsis: true,
        },
        {
          title: "Long Column",
          dataIndex: "address",
          key: "address 4",
          ellipsis: true,
        },
        {
          title: "操作",
          dataIndex: "operate",
          width: 200,
          scopedSlots: { customRender: "action" },
        },
      ],
      tableData: [
        {
          key: "1",
          name: "John Brown",
          age: 32,
          address: "New York No. 1 Lake Park, New York No. 1 Lake Park",
          tags: ["nice", "developer"],
        },
        {
          key: "2",
          name: "Jim Green",
          age: 42,
          address: "London No. 2 Lake Park, London No. 2 Lake Park",
          tags: ["loser"],
        },
        {
          key: "3",
          name: "Joe Black",
          age: 32,
          address: "Sidney No. 1 Lake Park, Sidney No. 1 Lake Park",
          tags: ["cool", "teacher"],
        },
      ],
      tableTitle2: [
        {
          title: "Name",
          dataIndex: "name",
          key: "name",
          scopedSlots: { customRender: "name" },
        },
        {
          title: "Age",
          dataIndex: "age",
          key: "age",
          width: 80,
        },
        {
          title: "Address",
          dataIndex: "address",
          key: "address 1",
          ellipsis: true,
        },
        {
          title: "Long Column Long Column Long Column",
          dataIndex: "address",
          key: "address 2",
          ellipsis: true,
        },
        {
          title: "Long Column Long Column",
          dataIndex: "address",
          key: "address 3",
          ellipsis: true,
        },
        {
          title: "Long Column",
          dataIndex: "address",
          key: "address 4",
          ellipsis: true,
        },
        {
          title: "操作",
          dataIndex: "operate",
          width: 100,
          scopedSlots: { customRender: "action" },
        },
      ],
      tableTitle3: [
        {
          title: "Name",
          dataIndex: "name",
          key: "name",
          scopedSlots: { customRender: "name" },
        },
        {
          title: "Age",
          dataIndex: "age",
          key: "age",
          width: 80,
        },
        {
          title: "Address",
          dataIndex: "address",
          key: "address 1",
          ellipsis: true,
        },
        {
          title: "Long Column Long Column Long Column",
          dataIndex: "address",
          key: "address 2",
          ellipsis: true,
        },
        {
          title: "Long Column Long Column",
          dataIndex: "address",
          key: "address 3",
          ellipsis: true,
        },
        {
          title: "Long Column",
          dataIndex: "address",
          key: "address 4",
          ellipsis: true,
        },
      ],
      selectedRowKeys: [],
      selectedRowKeys2: [],

      currentPage: 1,
      currentPageSize: 20,
    };
  },
  mounted() {
    this.createBtn();
  },
  methods: {
    view(val) {
      console.log("viewviewview", val);
    },
    edit(val) {
      console.log("editeditedit", val);
    },
    add(val) {
      console.log("addaddadd", val);
    },

    onSelectChange(selectedRowKeys) {
      console.log("onSelectChange", selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
    },
    onSelectChange2(selectedRowKeys2) {
      console.log("onSelectChange2", selectedRowKeys2);
      this.selectedRowKeys2 = selectedRowKeys2;
    },
  },
};
</script>
Logo

前往低代码交流专区

更多推荐