Vben Admin BasicTable用法,自定义查询条件表格数据

文章描述了一个使用Vue编写的表格组件,展示了如何使用Select和DatePicker组件进行数据筛选,以及如何实现动态加载和操作(如编辑、删除)。内容涉及表格配置、API调用和数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<BasicTable
   @register="registerTable"
   id="table"
   style="height: 100%"
>
   <!-- 表格search栏 -->
   <template #form-templateCode>
      <Select
         v-model:value="templateCode"
         show-search
         :filterOption="filterOption"
         allowClear
         :options="templateAllList"
         @change="changeTemplateCode()"
         placeholder="全部"
      />
   </template>
   <template #form-beginTime>
      <DatePicker v-model:value="beginTime" @change="changeBeginTime()" />      
   </template>
   <template #form-endTime>
      <DatePicker v-model:value="endTime" @change="changeEndTime()" />
   </template>
   <template #form-add>
      <a-button type="primary" @click="handleAdd" style="margin-left: 10px; float: right">
        增加
      </a-button>
      <a-button type="primary" @click="getTableData" style="margin-left: 10px; float: right">
        查询
      </a-button>
   </template>
   <!-- 表格操作栏 -->
   <template #bodyCell="{ column, record }">
      <TableAction
         v-if="column.dataIndex && column.dataIndex == 'action']"
         :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
                placement: 'left',
              },
            },
          ]"
       />
    </template>
</BasicTable>


<script lang="ts" setup>
  const tableData = ref([] as any);
  const tableColumns = [
    {
      title: '模板名称',
      dataIndex: 'templateName',
    },
    {
      title: '住院ID',
      dataIndex: 'visitId',
    },
    {
      title: '记录时间',
      dataIndex: 'recordTime',
    },
    {
      title: '操作',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: 'right',
      width: 90,
    },
  ];
  formSchemas: FormSchema[] = [
    {
      field: 'templateId',
      component: 'Select',
      label: '模板',
      colProps: {
        span: 8,
      },
      labelWidth: 80,
      slot: 'templateCode',
    },
    {
      field: 'beginTime',
      component: 'DatePicker',
      label: '开始时间',
      colProps: {
        span: 4,
      },
      labelWidth: 80,
      slot: 'beginTime',
    },
    {
      field: 'endTime',
      component: 'DatePicker',
      label: '结束时间',
      colProps: {
        span: 4,
      },
      labelWidth: 80,
      slot: 'endTime',
    },
    {
      field: '',
      component: 'Select',
      label: '',
      colProps: {
        span: 8,
      },
      slot: 'add',
      show: true,
    },
  ]
  const tablePaginationRight = reactive({
    total: 0,
    current: 1,
    pageSize: 10,
    showTotal: (total) => `总共 ${total} 项`,
    defaultPageSize: 10,
    pageSizeOptions: ['10', '50', '80', '100'], // 可不设置使用默认
    showSizeChanger: true, // 是否显示pagesize选择
    showQuickJumper: false, // 是否显示跳转窗
  });
  const [registerTable, { setTableData, setColumns, setProps }] = useTable({
    title: tableTitle?.value,
    dataSource: tableData.value,//如果数据需要处理
    api: getTenantList,//如果数据不需要处理,可以直接写请求接口
    columns: tableColumns,
    formConfig: {
      labelWidth: 130,
      schemas: formSchemas,
      autoSubmitOnEnter: true, // 回车事件;
      showActionButtonGroup: false,
    },
    canResize: true,
    striped: false,
    useSearchForm: true,
    // showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    pagination: tablePaginationRight,
  });
  function getTableData() {
    tableData.value = [];
    getTemplateDataPage(tableSearchParams).then((res) => {
      if (res) {
        tableSearchParams.total = res['total'];
        tablePaginationRight.current = res['current'];
        tablePaginationRight.total = res['total'];
        tablePaginationRight.pageSize = res['records'].length;
        res['records'].forEach((val) => {
          let arr = {} as any;
          dataIndexList.value.forEach((key) => {
            arr[key] = val[key] ? val[key] : '';
            arr['id'] = val['id'];
            arr['showTime'] = val['showTime'];
          });
          tableData.value.push(arr);
        });
      }
      setTableData(tableData.value);
      setProps({ pagination: tablePaginationRight });
    });
  }
</script>

<think>我们正在使用VbenAdmin框架,并且想要在表单组件中添加和使用自定义插槽。根据引用[1],在BasicTable中,搜索区域(即表单区域)使用的是BasicForm组件,并且通过具名插槽来实现自定义。具体来说,在BasicForm组件中,使用了一个计算属性`getFormSlotKeys`来获取所有以`form-`开头的插槽名称。因此,如果我们想在表单中添加自定义插槽,需要按照以下步骤:1.在表单组件的插槽部分,使用`form-`作为前缀来命名插槽。2.在模板中使用这些插槽。下面是一个具体的例子:假设我们有一个表单,其中有一个字段叫做`customField`,我们想要为这个字段添加一个自定义的插槽。在组件的模板中,我们可以这样写: ```vue<template><BasicForm@register="registerForm"><!--自定义插槽,注意插槽名称为form-customField--><template#form-customField><!--这里放置自定义内容,比如一个特殊的输入组件--><a-input-groupcompact><a-select style="width:30%"><a-select-option value="http">http://</a-select-option><a-select-option value="https">https://</a-select-option></a-select><a-inputstyle="width:70%" /></a-input-group></template></BasicForm> </template>```在组件的脚本部分,我们需要使用`useForm`来注册表单,并且在表单配置中定义`customField`字段。注意:在表单配置中,我们需要将`customField`字段的`slot`属性设置为`'customField'`(注意这里不需要写`form-`前缀,因为框架内部会自动添加前缀)。例如: ```tsimport{ defineComponent }from 'vue';import {BasicForm, useForm }from '/@/components/Form/index';import {schemas} from'./schemas';//假设表单配置在单独的文件中exportdefault defineComponent({components: {BasicForm},setup(){const [registerForm] =useForm({labelWidth:120,schemas:[{field: 'customField',component:'Input',label:'自定义字段',//使用slot属性,指定插槽名slot: 'customField',},// ...其他表单字段],});return{ registerForm };},}); ```这样,在表单渲染`customField`字段时,就会去查找名为`form-customField`的插槽,并使用我们自定义的内容。另外,引用[1]中提到,在BasicTable中搜索区域对应的是BasicForm,所以如果是在表格的搜索区域添加自定义插槽,方法也是类似的。总结步骤:1.在表单配置项中,为需要自定义的字段设置`slot`属性(值为插槽名称,不带`form-`前缀)。2.在BasicForm组件内部(或BasicTable的搜索表单区域),使用`<template#form-插槽名称>`来定义插槽内容。注意:插槽名称必须是唯一的,且不能与已有的插槽冲突。如果是在BasicTable的搜索表单中使用自定义插槽,写法类似,只是需要将插槽放在BasicTable组件的插槽中,因为BasicTable的搜索表单实际上就是BasicForm。例如,在BasicTable中: ```vue<template><BasicTable @register="registerTable"><!--搜索表单的自定义插槽--><template#form-customField><!--自定义内容 --></template></BasicTable> </template>```这样,在表格的搜索表单中,对应字段就会使用自定义插槽。希望这个解答能够帮助你。</think>在 Vben Admin 框架中为表单组件添加自定义插槽,需遵循特定的命名规则和实现方式。以下是具体步骤和示例: ### 实现步骤 1. **命名规则** 所有表单插槽必须以 `form-` 前缀开头(如 `form-customSlot`),框架会通过 `getFormSlotKeys` 方法自动识别这类插槽[^1]。 2. **表单配置** 在表单配置对象中,为需要自定义的字段设置 `slot` 属性(无需带 `form-` 前缀): ```ts const schemas = [ { field: 'customField', label: '自定义字段', component: 'Input', // 基础组件类型 slot: 'customSlot' // 指定插槽名(对应 form-customSlot) } ]; ``` 3. **模板中使用插槽** 在组件模板中通过 `<template #form-插槽名>` 实现自定义内容: ```vue <template> <BasicForm @register="registerForm"> <!-- 自定义插槽内容 --> <template #form-customSlot> <a-input-group compact> <a-select style="width: 30%"> <a-select-option value="option1">选项1</a-select-option> </a-select> <a-input style="width: 70%" placeholder="自定义输入" /> </a-input-group> </template> </BasicForm> </template> ``` ### 完整示例 ```vue <script setup lang="ts"> import { useForm } from '@vben/vbencomponents'; import { schemas } from './formSchemas'; const [registerForm] = useForm({ schemas: [ { field: 'customField', component: 'Input', label: '自定义字段', slot: 'customSlot' // 关键配置 }, // 其他字段... ] }); </script> <template> <BasicForm @register="registerForm"> <!-- 自定义插槽 --> <template #form-customSlot> <div class="custom-component"> <a-button @click="handleClick">触发操作</a-button> <a-tag color="blue">自定义UI组件</a-tag> </div> </template> </BasicForm> </template> ``` ### 关键说明 1. **插槽命名强制前缀** 框架通过以下逻辑过滤插槽[^1]: ```ts const getFormSlotKeys = computed(() => { return Object.keys(slots) .filter(key => key.startsWith('form-')) }); ``` 2. **复杂组件支持** 可嵌入任意 Vue 组件(如图标、按钮组等),参考引用[2]中自定义表头的实现方式。 3. **插槽作用域** 插槽内可直接访问表单上下文,如需操作表单数据,可通过 `useForm` 返回的方法(如 `setFieldsValue`)。 --- ### 相关问题 1. 如何在 Vben Admin 中为表格组件添加自定义表头插槽? 2. 表单插槽内如何访问当前表单字段的值? 3. 如何动态切换表单中的自定义插槽内容? [^1]: Vben Admin 通过 `form-` 前缀识别表单插槽,需严格遵循命名规范 [^2]: 自定义组件时可结合 Ant Design Vue 的组件实现复杂 UI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值