amis CRUD组件深度剖析:增删改查一体化解决方案

amis CRUD组件深度剖析:增删改查一体化解决方案

【免费下载链接】amis 前端低代码框架,通过 JSON 配置就能生成各种页面。 【免费下载链接】amis 项目地址: https://gitcode.com/GitHub_Trending/am/amis

还在为每个业务模块重复编写增删改查页面而烦恼吗?每次新增一个数据管理功能都要重新设计表格、表单、弹窗、分页和搜索功能?amis CRUD组件正是为解决这一痛点而生,通过JSON配置即可快速构建功能完整的数据管理界面。

什么是CRUD组件?

CRUD(Create、Read、Update、Delete)即增删改查,是业务系统中最常见的操作模式。amis CRUD组件是一个高度集成的数据管理解决方案,支持:

  • 📊 多种展示模式:表格(Table)、列表(List)、卡片(Cards)
  • 🔍 智能搜索过滤:条件搜索、快速过滤、列搜索
  • 📝 数据操作:新增、编辑、查看、删除、批量操作
  • 📄 分页管理:标准分页、加载更多、一次性加载
  • 🌳 树形数据:嵌套展示、懒加载子节点
  • 🎯 交互体验:拖拽排序、行选择、操作栏定制

核心架构解析

数据流架构

mermaid

组件结构层次

mermaid

基础配置详解

最小化配置示例

{
  "type": "crud",
  "api": "/api/mock2/sample",
  "syncLocation": false,
  "columns": [
    {
      "name": "id",
      "label": "ID",
      "sortable": true
    },
    {
      "name": "engine",
      "label": "Rendering engine",
      "searchable": true
    },
    {
      "name": "browser",
      "label": "Browser"
    },
    {
      "type": "operation",
      "label": "操作",
      "buttons": [
        {
          "label": "编辑",
          "type": "button",
          "actionType": "drawer",
          "drawer": {
            "title": "编辑表单",
            "body": {
              "type": "form",
              "api": "post:/api/mock2/sample/${id}",
              "body": [
                {
                  "type": "input-text",
                  "name": "engine",
                  "label": "Engine"
                }
              ]
            }
          }
        },
        {
          "label": "删除",
          "type": "button",
          "actionType": "ajax",
          "confirmText": "确认删除?",
          "api": "delete:/api/mock2/sample/${id}"
        }
      ]
    }
  ]
}

数据接口规范

CRUD组件要求后端接口返回特定的数据结构:

{
  "status": 0,
  "msg": "",
  "data": {
    "items": [
      {
        "id": 1,
        "engine": "WebKit",
        "browser": "Safari",
        "platform": "Mac OS X",
        "version": "4.0",
        "grade": "A"
      }
    ],
    "total": 100
  }
}
字段名类型说明
itemsArray数据列表数组
totalNumber数据总数(用于分页)
pageNumber当前页码(可选)
hasNextBoolean是否有下一页(简单分页)

高级功能特性

1. 多种展示模式对比

模式适用场景特点配置示例
Table数据表格支持固定表头、列排序、列过滤"mode": "table"
List列表展示适合移动端、简洁列表"mode": "list"
Cards卡片布局可视化强、信息密度高"mode": "cards"

2. 查询过滤系统

自动生成查询条件
{
  "autoGenerateFilter": {
    "columnsNum": 2,
    "showBtnToolbar": true,
    "defaultCollapsed": false
  },
  "columns": [
    {
      "name": "name",
      "label": "名称",
      "searchable": {
        "type": "input-text",
        "name": "name",
        "label": "名称搜索",
        "placeholder": "输入名称"
      }
    },
    {
      "name": "status",
      "label": "状态",
      "searchable": {
        "type": "select",
        "name": "status",
        "label": "状态筛选",
        "options": [
          {"label": "启用", "value": "enabled"},
          {"label": "禁用", "value": "disabled"}
        ]
      }
    }
  ]
}
自定义过滤表单
{
  "filter": {
    "title": "高级搜索",
    "body": [
      {
        "type": "input-text",
        "name": "keywords",
        "label": "关键词",
        "clearable": true
      },
      {
        "type": "date-range",
        "name": "dateRange",
        "label": "时间范围"
      },
      {
        "type": "select",
        "name": "category",
        "label": "分类",
        "options": "/api/categories"
      }
    ],
    "actions": [
      {
        "type": "reset",
        "label": "重置"
      },
      {
        "type": "submit",
        "label": "搜索"
      }
    ]
  }
}

3. 批量操作能力

{
  "bulkActions": [
    {
      "label": "批量删除",
      "actionType": "ajax",
      "api": "delete:/api/sample/${ids|raw}",
      "confirmText": "确定要删除选中的 ${count} 项数据?"
    },
    {
      "label": "批量启用",
      "actionType": "ajax",
      "api": "post:/api/sample/batch-enable",
      "data": {
        "ids": "${ids|raw}",
        "status": "enabled"
      }
    },
    {
      "label": "导出数据",
      "actionType": "download",
      "api": "/api/sample/export?ids=${ids|raw}"
    }
  ]
}

4. 工具栏定制化

顶部工具栏配置
{
  "headerToolbar": [
    {
      "type": "button",
      "label": "新增",
      "actionType": "dialog",
      "level": "primary",
      "dialog": {
        "title": "新增数据",
        "body": {
          "type": "form",
          "api": "post:/api/sample",
          "body": [
            {"type": "input-text", "name": "name", "label": "名称"}
          ]
        }
      }
    },
    "bulkActions",
    {
      "type": "columns-toggler",
      "align": "right"
    },
    {
      "type": "pagination",
      "align": "right"
    }
  ]
}
底部工具栏配置
{
  "footerToolbar": [
    "statistics",
    "switch-per-page",
    "pagination"
  ]
}

实战案例:用户管理系统

完整配置示例

{
  "type": "crud",
  "api": "/api/users",
  "syncLocation": true,
  "perPage": 20,
  "mode": "table",
  "filterTogglable": true,
  "filterDefaultVisible": true,
  "autoJumpToTopOnPagerChange": true,
  
  "filter": {
    "title": "用户筛选",
    "body": [
      {
        "type": "input-text",
        "name": "keyword",
        "label": "关键词",
        "placeholder": "姓名/邮箱/手机号"
      },
      {
        "type": "select",
        "name": "status",
        "label": "状态",
        "options": [
          {"label": "全部", "value": ""},
          {"label": "启用", "value": "active"},
          {"label": "禁用", "value": "inactive"}
        ]
      },
      {
        "type": "date-range",
        "name": "createTime",
        "label": "创建时间"
      }
    ]
  },
  
  "bulkActions": [
    {
      "label": "批量启用",
      "actionType": "ajax",
      "api": "post:/api/users/batch-active",
      "confirmText": "确定启用选中的用户?"
    },
    {
      "label": "批量禁用",
      "actionType": "ajax",
      "api": "post:/api/users/batch-inactive",
      "confirmText": "确定禁用选中的用户?"
    }
  ],
  
  "columns": [
    {
      "name": "id",
      "label": "ID",
      "width": 80,
      "sortable": true
    },
    {
      "name": "username",
      "label": "用户名",
      "searchable": true,
      "sortable": true
    },
    {
      "name": "email",
      "label": "邮箱",
      "searchable": true
    },
    {
      "name": "phone",
      "label": "手机号"
    },
    {
      "name": "status",
      "label": "状态",
      "type": "mapping",
      "map": {
        "active": "<span class='text-success'>启用</span>",
        "inactive": "<span class='text-danger'>禁用</span>"
      },
      "filterable": {
        "options": [
          {"label": "启用", "value": "active"},
          {"label": "禁用", "value": "inactive"}
        ]
      }
    },
    {
      "name": "createTime",
      "label": "创建时间",
      "type": "date",
      "format": "YYYY-MM-DD HH:mm:ss"
    },
    {
      "type": "operation",
      "label": "操作",
      "buttons": [
        {
          "label": "编辑",
          "type": "button",
          "actionType": "drawer",
          "drawer": {
            "title": "编辑用户",
            "body": {
              "type": "form",
              "api": "put:/api/users/${id}",
              "body": [
                {
                  "type": "input-text",
                  "name": "username",
                  "label": "用户名",
                  "required": true
                },
                {
                  "type": "input-email",
                  "name": "email",
                  "label": "邮箱"
                }
              ]
            }
          }
        },
        {
          "label": "禁用",
          "type": "button",
          "actionType": "ajax",
          "api": "post:/api/users/${id}/disable",
          "confirmText": "确定禁用该用户?",
          "visibleOn": "data.status == 'active'"
        },
        {
          "label": "启用",
          "type": "button",
          "actionType": "ajax",
          "api": "post:/api/users/${id}/enable",
          "confirmText": "确定启用该用户?",
          "visibleOn": "data.status == 'inactive'"
        }
      ]
    }
  ],
  
  "headerToolbar": [
    {
      "type": "button",
      "label": "新增用户",
      "actionType": "dialog",
      "level": "primary",
      "dialog": {
        "title": "新增用户",
        "body": {
          "type": "form",
          "api": "post:/api/users",
          "body": [
            {
              "type": "input-text",
              "name": "username",
              "label": "用户名",
              "required": true
            },
            {
              "type": "input-email",
              "name": "email",
              "label": "邮箱",
              "required": true
            }
          ]
        }
      }
    },
    "bulkActions",
    {
      "type": "columns-toggler",
      "align": "right"
    },
    {
      "type": "pagination",
      "align": "right"
    }
  ],
  
  "footerToolbar": [
    "statistics",
    "switch-per-page",
    "pagination"
  ]
}

功能特性说明

功能模块实现方式优势
数据展示Table模式 + 分页支持大数据量,用户体验好
搜索过滤自定义过滤表单灵活的条件组合搜索
批量操作bulkActions配置高效处理多条数据
单条操作operation列按钮精细化的单数据管理
状态管理Mapping类型列直观的状态显示
分页统计内置分页组件完整的分页信息展示

性能优化建议

1. 前端一次性加载

对于数据量不大的场景,可以使用前端过滤和分页:

{
  "loadDataOnce": true,
  "api": "/api/all-data",
  "filter": {
    // 过滤条件
  }
}

2. 懒加载树形数据

{
  "api": "/api/tree-data",
  "deferApi": "/api/tree-children?parentId=${id}",
  "columns": [
    // 列配置
  ]
}

3. 数据轮询更新

{
  "interval": 5000,
  "stopAutoRefreshWhen": "this.dialogOpen",
  "api": "/api/realtime-data"
}

常见问题解决方案

1. 数据格式不匹配

问题:后端返回的数据结构与CRUD期望的不一致

解决方案:使用数据映射

{
  "api": {
    "url": "/api/custom-data",
    "data": {
      "items": "${rows}",
      "total": "${totalCount}"
    }
  }
}

2. 复杂查询条件

问题:需要复杂的查询条件组合

解决方案:自定义filter表单 + 数据映射

{
  "filter": {
    "body": [
      {
        "type": "input-text",
        "name": "complexQuery",
        "label": "复杂查询"
      }
    ]
  },
  "api": {
    "url": "/api/data",
    "data": {
      "query": "complexCondition=${complexQuery}"
    }
  }
}

3. 权限控制

问题:不同用户看到不同的操作按钮

解决方案:使用visibleOn表达式

{
  "buttons": [
    {
      "label": "管理员操作",
      "actionType": "ajax",
      "api": "/api/admin-action",
      "visibleOn": "user.role == 'admin'"
    }
  ]
}

总结

amis CRUD组件通过JSON配置的方式,极大地简化了数据管理页面的开发工作。它提供了:

  • 🚀 开发效率提升:配置即开发,减少重复编码
  • 🎨 丰富的UI组件:多种展示模式,满足不同场景需求
  • 强大的功能:搜索、过滤、排序、分页、批量操作一应俱全
  • 🔧 高度可定制:灵活的配置选项,适应各种业务需求
  • 📱 响应式设计:自动适配桌面和移动端

通过本文的深度剖析,相信您已经掌握了amis CRUD组件的核心用法和高级特性。无论是简单的数据表格还是复杂的企业级管理系统,CRUD组件都能为您提供优雅的解决方案。

立即尝试使用amis CRUD组件,让数据管理页面的开发变得简单而高效!

【免费下载链接】amis 前端低代码框架,通过 JSON 配置就能生成各种页面。 【免费下载链接】amis 项目地址: https://gitcode.com/GitHub_Trending/am/amis

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

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

抵扣说明:

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

余额充值