Ben Lin
2025-03-08 858b9bccead46cdefc99325b7c956d50a2964309
src/views/demo/page/form/high/PersonTable.vue
@@ -3,15 +3,14 @@
    <BasicTable @register="registerTable" @edit-change="handleEditChange">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction :actions="createActions(record, column)" />
          <TableAction :actions="createActions(record)" />
        </template>
      </template>
    </BasicTable>
    <a-button block class="mt-5" type="dashed" @click="handleAdd"> 新增成员 </a-button>
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
<script lang="ts" setup>
  import {
    BasicTable,
    useTable,
@@ -19,7 +18,7 @@
    BasicColumn,
    ActionItem,
    EditRecordRow,
  } from '/@/components/Table';
  } from '@/components/Table';
  const columns: BasicColumn[] = [
    {
@@ -56,92 +55,80 @@
      dept: 'New York No. 3Lake Park',
    },
  ];
  export default defineComponent({
    components: { BasicTable, TableAction },
    setup() {
      const [registerTable, { getDataSource }] = useTable({
        columns: columns,
        showIndexColumn: false,
        dataSource: data,
        actionColumn: {
          width: 160,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
        },
        scroll: { y: '100%' },
        pagination: false,
      });
      function handleEdit(record: EditRecordRow) {
        record.onEdit?.(true);
      }
      function handleCancel(record: EditRecordRow) {
        record.onEdit?.(false);
        if (record.isNew) {
          const data = getDataSource();
          const index = data.findIndex((item) => item.key === record.key);
          data.splice(index, 1);
        }
      }
      function handleSave(record: EditRecordRow) {
        record.onEdit?.(false, true);
      }
      function handleEditChange(data: Recordable) {
        console.log(data);
      }
      function handleAdd() {
        const data = getDataSource();
        const addRow: EditRecordRow = {
          name: '',
          no: '',
          dept: '',
          editable: true,
          isNew: true,
          key: `${Date.now()}`,
        };
        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,
        handleEditChange,
      };
  const [registerTable, { getDataSource }] = useTable({
    columns: columns,
    showIndexColumn: false,
    dataSource: data,
    actionColumn: {
      width: 160,
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
    },
    scroll: { y: '100%' },
    pagination: false,
  });
  // 暴露getDataSource 供父组件使用
  defineExpose({ getDataSource });
  function handleEdit(record: EditRecordRow) {
    record.onEdit?.(true);
  }
  function handleCancel(record: EditRecordRow) {
    record.onEdit?.(false);
    if (record.isNew) {
      const data = getDataSource();
      const index = data.findIndex((item) => item.key === record.key);
      data.splice(index, 1);
    }
  }
  function handleSave(record: EditRecordRow) {
    record.onEdit?.(false, true);
  }
  function handleEditChange(data: Recordable) {
    console.log(data);
  }
  function handleAdd() {
    const data = getDataSource();
    const addRow: EditRecordRow = {
      name: '',
      no: '',
      dept: '',
      editable: true,
      isNew: true,
      key: `${Date.now()}`,
    };
    data.push(addRow);
  }
  function createActions(record: EditRecordRow): ActionItem[] {
    if (!record.editable) {
      return [
        {
          label: '编辑',
          onClick: handleEdit.bind(null, record),
        },
        {
          label: '删除',
        },
      ];
    }
    return [
      {
        label: '保存',
        onClick: handleSave.bind(null, record),
      },
      {
        label: '取消',
        popConfirm: {
          title: '是否取消编辑',
          confirm: handleCancel.bind(null, record),
        },
      },
    ];
  }
</script>