Vben Admin 自学记录 —— Drawer组件的基本使用及练习(持续更新中...)

文章详细介绍了如何对antv的Drawer组件进行封装,添加拖拽、全屏和自适应高度等特性。在Vue项目中,创建了一个ViewDrawer.vue组件,用于独立使用Drawer,并配置了表单字段。在BasicTable.vue中,展示了如何在表格中使用这个封装后的Drawer组件,实现查看单条表格数据的功能。
摘要由CSDN通过智能技术生成

Drawer 抽屉组件

对 antv 的 drawer 组件进行封装,扩展拖拽,全屏,自适应高度等功能。

Drawer相关使用及概念

练习 —— 在之前table基础上,添加查看功能,点击查看按钮,弹出抽屉显示单条表格数据,且不可修改

之前相关记录: Table组件的基本使用及练习

src/views/myComponents/tableTest中添加ViewDrawer.vue,用来独立使用Drawer,写内部代码

在这里插入图片描述
data.ts 中配置Drawer中表单字段

export const schemasView: FormSchema[] = [
  {
    field: 'divider-bill',
    component: 'Divider',
    label: '信息',
    colProps: {
      span: 24,
    },
  },
  {
    field: 'name',
    component: 'Input',
    label: '姓名',
    required:true,
  },
  {
    field: 'sex',
    component: 'Select',
    label: '性别',
    required:true,
    componentProps: {
      options: sexOption,
    },
  },
  {
    field: 'dt',
    component: 'DatePicker',
    label: '出生日期',
    required:true,
    componentProps: {
      style: {width: '100%',},
      valueFormat: 'YYYYMMDD',
    },
  },
  {
    field: 'age',
    component: 'InputNumber',
    label: '年龄',
    required:true,
    componentProps: {
      style: {width: '100%',},
      step: 1,
      min: 0,
      max: 150,
    },
  },
  {
    label: '电话',
    field: 'tel',
    component: 'Input',
    required:true,
    dynamicRules: ({ values }) => {
      if (values.tel !== undefined){
        if (values.tel.indexOf('-')>0){
          return [
            {
              required:true,
              trigger: 'change',
              message: '请输入正确的号码',
              pattern:   /(^\d{4}-\d{7}$)|(^\d{3}-\d{8}$)/,
            }
          ];
        }
        else {
          return [
            {
              required:true,
              trigger: 'change',
              message: '请输入正确的号码',
              pattern: /^1[3|4|5|7|8][0-9]{9}$/,
            },
          ];
        }
      }
      else {
        return[{required:true,message: '请输入电话',}]
      }
    },
  },
  {
    field: 'address',
    component: 'InputTextArea',
    label: '住址',
    required:true,
  },
];

ViewDrawer 代码

<template>
  <div
    :style="{
      overflow: 'hidden',
    }"
  >
    <BasicDrawer
      v-bind="$attrs"
      @register="registerDrawer"
      @visibleChange="handleVisibleChange"
      :isDetail="true"
      title="查看"
      placement="bottom"
      height="100%"
      :destroyOnClose="true"
    >
      <div>
        <BasicForm @register="registerForm"></BasicForm>
      </div>
    </BasicDrawer>
  </div>
</template>

<script lang="ts">
  import { defineComponent } from 'vue';
  import { Divider } from 'ant-design-vue';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicForm, useForm } from '/@/components/Form';
  import { schemasView } from './data';
  export default defineComponent({
    name: 'ViewDrawer',
    components: {
      BasicDrawer,
      BasicForm,
      Divider,
    },
    setup(_, { emit }) {
      // 配置Drawer
      const [registerDrawer, { closeDrawer }] = useDrawerInner(async (data) => {
        console.log('打印从table传递的数据:', data);
        setFieldsValue(data), setProps({ disabled: true });  // data值赋给表单,设置disabled
      });

      // 配置Form
      const [registerForm, { validate, setFieldsValue, setProps }] = useForm({
        labelWidth: 150,
        baseColProps: {
          offset: 1,
          span: 10,
        },
        schemas: schemasView,
        showActionButtonGroup: false,
      });

      function handleVisibleChange(visible: boolean) {
        if (!visible) {
          emit('reload');
        }
      }
      return {
        registerDrawer,
        closeDrawer,

        registerForm,
        validate,
        setFieldsValue,
        setProps,

        handleVisibleChange
      };
    },
  });
</script>

<style scoped></style>

basicTable.vue中使用

<template>
  <div
    :style="{
      overflow: 'hidden',
      position: 'relative',
      height: '100%',
    }"
  >
    <!-- 注册table -->
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              tooltip: '查看',
              icon: 'clarity:info-standard-line',
              onClick: handleView.bind(null, { data: record }), 
            },
            {
              tooltip: '编辑',
              icon: 'clarity:note-edit-line',
              // onClick: handleEdit.bind(null, { data: record,}),
            },
            {
              tooltip: '删除',
              color: 'error',
              icon: 'ant-design:delete-outlined',
              // popConfirm: {
              //   title: '是否确定删除?',
              //   confirm: handleDel.bind(null, record),
              // },
            },
          ]"
        />
      </template>
      <template #toolbar>
        <a-button type="primary">{{ '新增' }}</a-button>
      </template>
    </BasicTable>

	<!-- 页面引入 -->
    <ViewDrawer @reload="handleReload" @register="registerDrawer" />
  </div>
</template>

<script lang="ts">
  import { defineComponent } from 'vue';
  import { formConfig, columns, initData } from './data';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import ViewDrawer from './ViewDrawer.vue';
  export default defineComponent({
    name: 'tableTest',
    components: {
      BasicTable,
      TableAction,
      ViewDrawer,
    },
    setup() {
      const data = initData();

      // 设置table
      const [registerTable, { reload }] = useTable({
        title: '查询结果',
        dataSource: data,
        columns: columns,
        bordered: true,
        useSearchForm: true, //开启搜索区域
        formConfig: formConfig,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
        rowSelection: { type: 'radio' },
        pagination: { pageSize: 10 },
        showTableSetting: true,
        tableSetting: { fullScreen: true },
        showIndexColumn: true,
        indexColumnProps: { fixed: 'left' },
      });

      // 注册Drawer
      const [registerDrawer, { openDrawer }] = useDrawer();

      // 查看按钮
      function handleView({ data }) {
      	// 弹出抽屉,传递data
        openDrawer(true, data);
      }

      function handleReload() {
        reload();
      }
      return {
        registerTable,
        reload,
        registerDrawer,
        handleView,
        handleReload,
      };
    },
  });
</script>

<style scoped></style>

页面效果

Drawer抽屉

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Morgan_Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值