PersonTable.vue 3.3 KB
Newer Older
V
vben 已提交
1 2
<template>
  <div>
3
    <BasicTable @register="registerTable" @edit-change="handleEditChange">
4 5 6 7
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="createActions(record, column)" />
        </template>
V
vben 已提交
8 9
      </template>
    </BasicTable>
V
vben 已提交
10
    <a-button block class="mt-5" type="dashed" @click="handleAdd"> 新增成员 </a-button>
V
vben 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import {
    BasicTable,
    useTable,
    TableAction,
    BasicColumn,
    ActionItem,
    EditRecordRow,
  } from '/@/components/Table';

  const columns: BasicColumn[] = [
    {
      title: '成员姓名',
      dataIndex: 'name',
V
vben 已提交
28
      editRow: true,
V
vben 已提交
29 30 31 32
    },
    {
      title: '工号',
      dataIndex: 'no',
V
vben 已提交
33
      editRow: true,
V
vben 已提交
34 35 36 37
    },
    {
      title: '所属部门',
      dataIndex: 'dept',
V
vben 已提交
38
      editRow: true,
V
vben 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    },
  ];

  const data: any[] = [
    {
      name: 'John Brown',
      no: '00001',
      dept: 'New York No. 1 Lake Park',
    },
    {
      name: 'John Brown2',
      no: '00002',
      dept: 'New York No. 2 Lake Park',
    },
    {
      name: 'John Brown3',
      no: '00003',
      dept: 'New York No. 3Lake Park',
    },
  ];
  export default defineComponent({
V
vben 已提交
60
    components: { BasicTable, TableAction },
V
vben 已提交
61 62 63 64 65 66 67 68 69
    setup() {
      const [registerTable, { getDataSource }] = useTable({
        columns: columns,
        showIndexColumn: false,
        dataSource: data,
        actionColumn: {
          width: 160,
          title: '操作',
          dataIndex: 'action',
70
          // slots: { customRender: 'action' },
V
vben 已提交
71 72 73 74 75
        },
        pagination: false,
      });

      function handleEdit(record: EditRecordRow) {
V
vben 已提交
76
        record.onEdit?.(true);
V
vben 已提交
77 78 79
      }

      function handleCancel(record: EditRecordRow) {
V
vben 已提交
80
        record.onEdit?.(false);
V
vben 已提交
81 82 83 84 85 86 87 88
        if (record.isNew) {
          const data = getDataSource();
          const index = data.findIndex((item) => item.key === record.key);
          data.splice(index, 1);
        }
      }

      function handleSave(record: EditRecordRow) {
V
vben 已提交
89
        record.onEdit?.(false, true);
V
vben 已提交
90 91
      }

92 93 94 95
      function handleEditChange(data: Recordable) {
        console.log(data);
      }

V
vben 已提交
96 97 98 99 100 101 102 103
      function handleAdd() {
        const data = getDataSource();
        const addRow: EditRecordRow = {
          name: '',
          no: '',
          dept: '',
          editable: true,
          isNew: true,
V
vben 已提交
104
          key: `${Date.now()}`,
V
vben 已提交
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
        };
        data.push(addRow);
      }

      function createActions(record: EditRecordRow, column: BasicColumn): ActionItem[] {
        if (!record.editable) {
          return [
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
            },
          ];
        }
        return [
          {
            label: '保存',
            onClick: handleSave.bind(null, record, column),
          },
          {
            label: '取消',
            popConfirm: {
              title: '是否取消编辑',
              confirm: handleCancel.bind(null, record, column),
            },
          },
        ];
      }

      return {
        registerTable,
        handleEdit,
        createActions,
        handleAdd,
        getDataSource,
142
        handleEditChange,
V
vben 已提交
143 144 145 146
      };
    },
  });
</script>