ProductPanel.vue 2.33 KB
<template>
  <BasicTable @register="registerTable">
    <template #toolbar>
      <a-button type="primary" @click="handleCreateModal">新建</a-button>
    </template>
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'action'">
        <TableAction :actions="createActions(record)" />
      </template>
    </template>
  </BasicTable>
  <CreateModal @register="createModalRegister" @modal-success="handleModalSuccess" />
</template>
<script setup lang="ts">
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import CreateModal from './CreateModal.vue';
  import { deleteConfig, getList, saveConfig } from '/@/api/sys/config';
  import { columnsProduct } from './data';
  import { useModal } from '/@/components/Modal';

  const [createModalRegister, { openModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    api: getList,
    pagination: false,
    columns: columnsProduct,
    rowKey: 'id',
    actionColumn: {
      width: 160,
      title: 'Action',
      dataIndex: 'action',
    },
  });

  function handleCreateModal() {
    openModal(true);
  }

  function createActions(record: any): any[] {
    if (!record.editable) {
      return [
        {
          label: '编辑',
          onClick: handleEdit.bind(null, record),
        },
        {
          ...{
            label: '删除',
            // onClick: handleDelete.bind(null, record),
            popConfirm: {
              title: '确认删除?',
              confirm: handleDelete.bind(null, record),
            },
          },
        },
      ];
    }
    return [
      {
        label: '保存',
        onClick: handleSave.bind(null, record),
      },
      {
        label: '取消',
        popConfirm: {
          title: '是否取消编辑',
          confirm: handleCancel.bind(null, record),
        },
      },
    ];
  }

  async function handleSave(record) {
    await saveConfig({ id: record.id, relationValue: record.relationValue });
    handleCancel(record);
    reload();
  }

  function handleEdit(record: any) {
    record.onEdit?.(true);
  }

  function handleCancel(record) {
    record.onEdit?.(false, false);
  }

  function handleModalSuccess() {
    reload();
  }

  async function handleDelete(record) {
    await deleteConfig({ ids: [record.id] });
    reload();
  }
</script>
<style></style>