vben admin表格常用配置

1.表格列属性

复制代码
/**
 * 定义表格列的配置
 * @param onActionClick 操作列点击回调函数
 * @param onStatusChange 状态改变回调函数
 * @returns 表格列配置数组
 */
export function useColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'id', // 字段名
      title: $t('system.tabletest.id'), // 列标题(ID)
      // width: 250, // 列宽度
      sortable: true, // 可排序
      showOverflow: true, // 超出显示省略号
      fixed: 'left', // 固定在左侧
    },
    {
      field: 'gsId', // 字段名
      title: $t('gsId'),
      // width: 200, // 列宽度
    },
    {
      field: 'code', // 字段名
      title: $t('system.tabletest.code'),
      // width: 200, // 列宽度
    },
    {
      field: 'name', // 字段名
      title: $t('system.tabletest.name'),
      // width: 200, // 列宽度
    },
    {
      field: 'status', // 字段名
      title: $t('system.role.status'), // 列标题(状态)
      // width: 100, // 列宽度
    },
    {
      align: 'center', // 居中对齐

      field: 'operation', // 字段名
      fixed: 'right', // 固定在右侧
      title: $t('system.role.operation'), // 列标题(操作)
      // width: 130, // 列宽度
    },
  ];
}
  • sortable 可排序

  • showOverflow 超出显示省略号

  • fixed 固定在左侧

  • width 列宽度 auto

  • type 渲染类型

  • formatter 格式化内容

  • editRender 单元格编辑渲染配置项

2.表格配置

html 复制代码
// 表格本身的配置选项
  gridOptions: {
    // 使用 useColumns 函数定义表格列传入两个回调函数:onActionClick(操作列点击)和 onStatusChange(状态改变)
    columns: useColumns(),
    // 表格高度自适应
    height: 'auto',
    // 保留原生数据,即使刷新页面也不会丢失已编辑的数据
    keepSource: true,
    // 导出功能配置
    exportConfig: {},
    // 代理配置,用于处理数据请求
    proxyConfig: {
      // AJAX 请求配置
      ajax: {
        // page 包含分页信息,formValues 是表单查询条件
        query: async ({ page }, formValues) => {
          // 调用 getRoleList API 获取角色列表数据
          const result = await getTableTestList({
            page: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
          return result;
        },
      },
    },
    // 行配置,指定每行数据的唯一标识字段为 id
    rowConfig: {
      keyField: 'id',
    },

    // 工具栏配置
    toolbarConfig: {
      custom: true, // 自定义列
      export: true, // 导出数据
      refresh: true, // 刷新数据
      search: true, // 搜索功能
      zoom: true, // 全屏缩放
      print: true, // 打印
    },
    // 使用类型断言指定配置的类型SystemRoleApi.SystemRole 是数据的类型定义
  } as VxeTableGridOptions<TableTestApi.TableTest>,

toolbarConfig 工具栏配置

html 复制代码
<template>
  <Page auto-content-height>
    <FormDrawer />
    <Grid table-title="测试表格">
      <template #toolbar-tools>
        <a-button type="primary"> 左侧按钮 </a-button>
      </template>

      <template #toolbar-actions>
        <a-button type="primary"> 右侧按钮 </a-button>
      </template>
    </Grid>
  </Page>
</template>

行拖拽配置

html 复制代码
//表格列中添加
    {
      dragSort: true,
      title: '行拖拽',
      width: 60,
    },
//表格配置中行配置添加
    // 行配置,指定每行数据的唯一标识字段为 id
    rowConfig: {
      keyField: 'id',
      drag: true, // 允许拖拽排序
    },

行编辑配置

html 复制代码
    editConfig: {
      mode: 'row',
      trigger: 'click',
    },

    onEditClosed: ({
      row,
      column,
    }: {
      column: any;
      row: BreakStockManagementApi.BreakStockManagement;
    }) => {
      // 设置默认值
      if (column.field === 'reasonType' && row.reasonType === '') {
        // message.info(
        //   `编辑结束 - 字段: ${column.field}, 值: ${row[column.field]}`,
        // );
        row.reasonType = 1;
      }
      // console.log(`编辑结束 - 字段: ${column.field}, 值: ${row[column.field]}`);
      updateBreakStockManagement(row);
    },

导出功能配置

html 复制代码
    exportConfig: {
      types: ['xlsx', 'csv'], // 导出类型
      filename: 'data', // 默认文件名
      mode: 'current', // 导出模式
      modes: ['current', 'selected'],
      download: true,

      message: true,
      useStyle: false,
}

框架全量导出数据为空,可以自定义导出按钮实现

html 复制代码
// 导出所有数据
async function onExportAll() {
  message.success('正在导出所有数据');
  try {
    // 首先获取所有数据
    const form = await gridApi.formApi.getValues();

 
    // 调用API获取所有数据(不分页)
    const result = await getBreakStockManagementList({
      ...form,
      page: 1,
      pageSize: 999_999, // 设置一个足够大的数字来获取所有数据
    });

 
    if (result.total === 0) {
      message.warning('没有数据可以导出');
      return;
    }

 
    // 使用获取到的所有数据进行导出
    gridApi.grid.exportData({
      data: result.items || result.records || result,
      type: 'xlsx',
      filename: 'data',
      sheetName: 'data',
      isHeader: true,
      isColgroup: true,
    });
  } catch (error) {
    message.error(`导出失败: ${(error as Error).message}`);
  }
}    <template #toolbar-tools>
        <button class="btn btn-primary" @click="onExportAll">全量导出</button>
      </template>自定义导出实现

全量数据导出,数据量特别大的情况使用这种方式前端会卡住,可以在后端进行导出操作直接在后端生成文件导出

相关推荐
IamZJT_1 分钟前
拒绝做 AI 的“饲养员” ❌:前端程序员在 AI 时代的生存与进化指南 🚀
前端·ai编程
MM_MS6 分钟前
Halcon控制语句
java·大数据·前端·数据库·人工智能·算法·视觉检测
程序员Agions17 分钟前
程序员武学修炼手册(二):进阶篇——小有所成,从能跑就行到知其所以然
前端·程序员
Van_Moonlight21 分钟前
RN for OpenHarmony 实战 TodoList 项目:任务完成进度条
javascript·开源·harmonyos
小画家~21 分钟前
第四十六: channel 高级使用
java·前端·数据库
Van_Moonlight29 分钟前
RN for OpenHarmony 实战 TodoList 项目:深色浅色主题切换
javascript·开源·harmonyos
小贵子的博客31 分钟前
Ant Design Vue <a-table>
前端·javascript·vue.js·anti-design-vue
m0_5027249532 分钟前
vue动态设置背景图片后显示异常
前端·css
天天进步201533 分钟前
【Nanobrowser源码分析4】交互篇: 从指令到动作:模拟点击、滚动与输入的底层实现
开发语言·javascript·ecmascript
console.log('npc')40 分钟前
vue2中子组件父组件的修改参数
开发语言·前端·javascript