Administrator
Administrator
发布于 2025-01-03 / 84 阅读
0
0

Fast-Crud前端——FastTable

FastTable界面

FastTable组件

fast-table是Fast-Crud前端核心组件,没有之一。观察下面这个例子:

<template>
  <div id="app">
    <fast-table :option="tableOption">
      <fast-table-column prop="id" label="ID"></fast-table-column>
      <fast-table-column-img prop="avatarUrl" label="头像"></fast-table-column-img>
      <fast-table-column-input prop="name" label="姓名" first-filter></fast-table-column-input>
      <fast-table-column-number prop="age" label="年龄"></fast-table-column-number>
      <fast-table-column-select prop="sex" label="性别" :options="[{label: '男', value: '1'}, {label: '女', value: '0'}]"></fast-table-column-select>
      <fast-table-column-date-picker prop="createTime" label="创建时间" type="datetime" :editable="false" :quick-filter="true"></fast-table-column-date-picker>
    </fast-table>
  </div>
</template>
<script>
import {FastTableOption} from 'fast-crud-ui'
export default {
  name: 'App',
  data() {
    return {
      tableOption: new FastTableOption({
        module: 'student'
      })
    }
  }
}
</script>

fast-table 组件只提供了一个配置: option,是一个FastTableOption实例,通过json配置构造这个实例。完整的配置项如下表:

FastTableOption配置项

配置项

描述

值类型

可选值

默认值

context

使用<fast-table>的组件上下文,如果你要在下面的钩子函数中使用this, 务必配置此值为this

Vue实例

-

-

title

标题,如果配置了值,将会在表格顶部显示此值

String

-

-

module

模块。此值对应标准接口的path前缀, 例如"student"。如果配置了此值,其它url配置项均内部自动拼接(无需指定),如果不配置此值,所有url都必须明确指定。

String

-

-

pageUrl

分页接口path

String

-

${module}/page

listUrl

列表接口path

String

-

${module}/list

insertUrl

单条数据新增接口path

String

-

${module}/insert

batchInsertUrl

数据批量新增接口path

String

-

${module}/insert/batch

updateUrl

单条数据更新接口path

String

-

${module}/update

batchUpdateUrl

数据批量更新接口path

String

-

${module}/update/batch

deleteUrl

单条数据删除接口path

String

-

${module}/delete

batchDeleteUrl

数据批量删除接口path

String

-

${module}/delete/batch

uploadUrl

FastTableColumnImg和FastTableColumnFile文件上传接口path

String

-

${module}/upload

exportUrl

数据导出接口path

String

-

${module}/export

enableDbClickEdit

启用双击编辑

Boolean

true/false

true

enableMulti

启用多选(勾选)

Boolean

true/false

true

enableColumnFilter

启用动态筛选——列过滤

Boolean

true/false

true

lazyLoad

分页懒加载:初始状态不加载分页数据

Boolean

true/false

false

editType

编辑类型——是表格行内编辑还是弹窗编辑

String

inline/form

inline

insertable

是否允许新增

Boolean

true/false

true

updatable

是否允许更新

Boolean

true/false

true

deletable

是否允许删除

Boolean

true/false

true

sortField

初始默认的排序字段名,无则不排序。通常设为createTime这样的字段。

String

-

-

sortDesc

是否倒序,配合sortField使用

Boolean

true/false

true

pagination

分页配置。

Object

-

-

  • layout

分页布局,参考原生的element分页layout

String

-

total, sizes, prev, pager, next, jumper

  • page-sizes

可选的每页条数

Array

-

[10, 20, 50, 100, 200]

  • size

默认的每页条数

Number

-

10

style

相关样式配置

Object

-

-

  • flexHeight

表格是否弹性高度。如果设置为true, FastTable会在内部计算表格的高度,从而在表格内部实现滚动条。当希望FastTable是“顶天立地”占满全屏时非常有用(需要父容器是顶天立地的)。

Boolean

true/false

false

  • bodyRowHeight

表格数据行的高度

String

-

50px

  • size

表格内相关元素的大小。影响范围除了表格自身,还有搜索控件、按钮、甚至弹出的表单控件。此值确保FastTable尺寸一致性。

String

default/medium

/small/mini

medium

  • formLabelWidth

表单项label的宽度。影响范围是快筛和弹窗表单。

String

-

auto

  • formLayout

弹窗表单的布局。不配置此值或此值为空(空串、null)时,弹窗的编辑表单控件默认按照fast-table-column*的排序顺序展示,且每个独占一行。如果需要定制化顺序和布局(多列),可通过此值配置。例如:"avatarUrl, name|age, sex|createTime"。效果将展示为:第一行是avatarUrl控件独占一行,第二行是name和age均分,第三行时sex和createTime均分。

String

-

null

conds

预置且固定的筛选条件,用户无法通过界面取消(1.1.2+)

Array<Cond>或Array<JSON>

-

[]

render

render函数,渲染FastTable内的FastTableColumn*,用在pick方法或FastTableColumnObject,在常规FastTable显示时,此属性无效。(1.1.2+)

Function<h> => Array<VNode>

-

beforeLoad

分页接口请求前执行。可用于二次加工过滤参数或取消分页加载(reject)

Function({query})

=> Promise

-

({query}) => Promise.resolve()

loadSuccess

分页接口请求成功后执行。可用于数据显示前的数据处理和格式转换之类的。若非resolve,数据将无法传入表格。resolve的数据必须是指定格式,例如:Promise.resolve({records: records, total: total});

Function({query, data, res}) => Promise

-

({query, data, res}) => Promise.resolve(data)

loadFail

分页接口请求失败后执行。可用于自定义错误提示

Function({query, error}) => Promise

-

({query, error}) => Promise.resolve()

beforeToInsert

新增行/表单激活前执行。可用于动态判断是否允许新增

Function() => Promise

-

() => Promise.resolve()

beforeInsert

新增接口请求前执行。可用于动态判断是否允许新增(可获取当前新增的数据)

将resolve的数据作为提交数据(1.1.6)

Function({fatRows,rows,editRows}) => Promise

-

({fatRows, rows, editRows}) => Promise.resolve(editRows)

insertSuccess

新增接口请求成功后执行。可用于自定义提示。

Function({fatRows,rows,editRows,res}) => Promise

-

({fatRows, rows, editRows, res}) => Promise.resolve()

insertFail

新增接口请求失败后执行。可用于自定义提示。

Function({fatRows,rows,editRows,error}) => Promise

-

({fatRows, rows, editRows, error}) => Promise.resolve()

beforeToUpdate

行内更新/更新表单激活前执行。可用于动态判断是否允许更新。

Function({fatRows,rows}) => Promise

-

({fatRows, rows}) => Promise.resolve()

beforeUpdate

更新接口请求前执行。可用于动态判断是否允许更新。

将resolve的数据作为提交数据(1.1.6)

Function({fatRows,rows, editRows}) => Promise

-

({fatRows, rows, editRows}) => Promise.resolve(editRows)

updateSuccess

更新接口请求成功后执行。可用于自定义提示。

Function({fatRows,rows, editRows,res}) => Promise

-

({fatRows, rows, editRows, res}) => Promise.resolve()

updateFail

更新接口请求失败后执行。可用于自定义提示。

Function({fatRows,rows, editRows,error}) => Promise

-

({fatRows, rows, editRows, error}) => Promise.resolve()

beforeDeleteTip

删除提示前执行。

Function({fatRows, rows}) => Promise

-

({fatRows, rows}) => Promise.resolve()

beforeDelete

删除接口调用前执行。

将resolve的数据作为提交数据(1.1.6)

Function({fatRows, rows}) => Promise

-

({fatRows, rows}) => Promise.resolve(rows)

deleteSuccess

删除接口调用成功后执行。

Function({fatRows, rows, res}) => Promise

-

({fatRows, rows, res}) => Promise.resolve()

deleteFail

删除接口调用失败后执行。

Function({fatRows, rows, error}) => Promise

-

({fatRows, rows, error}) => Promise.resolve()

beforeCancel

取消前执行。无论新增、编辑,还是行内或者表单,取消前触发。

Function({fatRows, rows, status}) => Promise

-

({fatRows, rows, status}) => Promise.resolve()

注意:

  • 针对所有钩子函数,你都需要返回一个Promise! 如果希望后续步骤正常进行,则返回 Promise.resolve();如果希望中断后续步骤,则返回Promise.reject()。

  • 只有loadSuccess在resolve时需要指定参数和格式,其它钩子函数resolve()即可。

  • 钩子函数中, row表示原始数据(即每个student的json数据),而fatRow 表示针对原始数据的封装,详细结构参见下文。而rows和fatRows即是对应的数组。

关于context

context的唯一作用是让你在钩子函数里获取到当前组件this, 以便获取当前组件的data或method,建议配置

关于钩子函数

针对钩子函数,务必注意以下几点:

  • 所有钩子函数都需要返回一个Promise! 如果希望后续步骤正常进行,则返回 Promise.resolve();如果希望中断后续步骤,则返回Promise.reject()。

  • 只有loadSuccess在resolve时需要指定参数和格式,其它钩子函数resolve()即可,reject()均是空即可,任意值也行,无所谓。

关于fatRow

由于要实现灵活的表格行内编辑,因此必须对el-table原生接受的行数据进行包装。一个fatRow格式如下:

{
  row: {...}, // 原始行数据,例如一个student数据对象,包含name, age等
  editRow: {...}, // 正在编辑的原始行数据。相当于行内编辑时用户修改的值,如果status=insert,则是新增时用户填写的值。
  status: 'update',// 当前行的状态: 是新增 or 更新
  config: {...} // 描述输入控件的元配置
}

FastTableOption完整配置参考

{
    context: this, // important! 否则钩子函数里无法获取当当前组件实例上下文
    title: '',
    module: 'student', // 配置了这个, 默认分页接口就是: student/page, 新增接口就是: student/insert, 其它同理。除非定制化, 其它url配置可省略
    insertUrl: 'student/insert',
    batchInsertUrl: 'student/insert/batch', 
    updateUrl: 'student/update',
    batchUpdateUrl: 'student/update/batch',
    deleteUrl: 'student/delete',
    batchDeleteUrl: 'student/delete/batch',
    uploadUrl: 'student/upload',
    exportUrl: 'student/export',
    enableDblClickEdit: true,
    enableMulti: true,
    enableColumnFilter: true,
    lazyLoad: false,
    editType: 'inline',
    insertable: true,
    updatable: true,
    deletable: true,
    sortField: 'createTime',
    sortDesc: true,
    pagination: {
      size: 10,
      "page-sizes": [5, 10, 20, 50, 100]
    },
    style: {
      flexHeight: true,
      size: 'medium',
      bodyRowHeight: '45px',
      formLabelWidth: 'auto', // 默认为auto
      formLayout: 'avatarUrl, name|age, sex|createTime'
    },
    beforeLoad({query}) {
        // 更改query的值,相当于二次编辑分页请求的参数
        query.addCond(new Cond('name', 'like', '利威尔'));
        return Promise.resolve();
    },
    loadSuccess({query, data, res}) {
      const {records, total} = data;
      // 可篡改records里的数据
      return Promise.resolve(data);
    },
    loadFail({query, error}) {
      console.log('哦豁, 分页加载失败了:' + JSON.stringify(error));
      return Promise.reject(); // reject将取消内置错误提示
    },
    beforeInsert({fatRows, rows, editRows}) {
      if (editRows.findIndex(r => r.name === '李四') > -1) {
        console.log('不允许添加李四');
        return Promise.reject();
      }
      return Promise.resolve(editRows);
    },
    insertSuccess({fatRows, rows, editRows, res}) {
      console.log('啧啧啧, 插入成功啦!');
      return Promise.reject(); // 取消内置的插入成功提示
    },
    insertFail({fatRows, rows, editRows, error}) {
      console.log('哦豁, 插入失败了!');
      return Promise.reject(); // reject将取消内置提示
    },
    beforeToUpdate({fatRows, rows}) {
      if (rows.findIndex(r => r.name === '阿明') > -1) {
        console.log("阿明不允许编辑")
        return Promise.reject();
      }
      return Promise.resolve();
    },
    beforeUpdate({fatRows, rows, editRows}) {
      if (editRows.findIndex(r => r.name === '张三') > -1) {
        console.log('名字不允许改为张三');
        return Promise.reject();
      }
      return Promise.resolve(editRows);
    },
    updateSuccess({fatRows, rows, editRows, res}) {
      console.log('恭喜更新成功!')
      return Promise.reject(); // 取消内置提示
    },
    updateFail({fatRows, rows, editRows, error}) {
      console.log('哦豁,更新失败!')
      return Promise.reject(); // 取消内置提示
    },
    beforeDeleteTip({fatRows, rows}) {
      if (rows.findIndex(r => r.name === '利威尔') > -1) {
        console.log('不能删除利威尔');
        return Promise.reject();
      }
      return Promise.resolve();
    },
    beforeDelete({fatRows, rows}) {
      if (rows.findIndex(r => r.name === '珊莎') > -1) {
        console.log('删除记录中包含珊莎, 你已勾选不能删除珊莎');
        return Promise.reject();
      }
      return Promise.resolve(rows);
    },
    deleteSuccess({fatRows, rows, res}) {
      if (rows.findIndex(r => r.name === '艾伦') > -1) {
        console.log('恭喜恭喜! 删除对象中包含艾伦');
        return Promise.reject(); // 通过reject覆盖默认的删除成功提示
      }
      return Promise.resolve();
    },
    deleteFail({fatRows, rows, error}) {
      console.log('哦豁, 删除失败了! ' + JSON.stringify(error));
      return Promise.reject(); // 通过reject覆盖默认的删除失败提示
    },
    beforeCancel({fatRows, rows, status}) {
      if (status === 'update') {
        console.log('更新时不允许取消')
        return Promise.reject();
      }
      return Promise.resolve();
    }
}

FastTable Methods

除了上述FastTableOption可以配置FastTable外,还可以通过给FastTable组件添加ref从而主动调用其中的方法。支持的方法列表如下:

方法名

说明

参数

reRender

重新渲染表格部分

-

pageLoad

根据FastTable组件内的过滤条件触发分页请求

-

toInsert

触发新增状态,将根据editType决定是新增一个编辑状态的空行,还是弹窗一个空表单

-

addForm

打开新增表单弹窗。无视editType配置

(row: Object) 可不穿,也可以为某些属性给初始值

addRow

新增一个编辑状态的空行。无视editType配置

(row: Object) 可不传,也可以为某些属性给初始值

addRows

新增多个编辑状态的空行。无视editType配置

(rows: Array) 可不穿,如果传了一定要穿对象数组,可为每个元素某些属性给初始值

setChoseRow

设置选中行

(index: Number) 表格第几行, 从0开始

updateForm

打开更新表单弹窗。无视editType

(fatRow: Object) 要编辑行的数据,注意是fatRow

updateRow

使指定行进入行内编辑状态。无视editType配置

(fatRow: Object) 要编辑的行数据,注意是fatRow

activeBatchEdit

激活批量编辑。将当前页所有行激活为行内编辑状态。

-

cancelEditStatus

主动取消编辑状态。效果等同于“取消”按钮

saveEditRows

主动保存编辑状态的行

-

FastTable Events

事件名

说明

参数

备注

current-change

当前选中行发生改变(注意不是勾选)

({fatRow, row})

fatRow和row可能为null

select

当用户手动勾选数据行的 Checkbox 时触发的事件

({fatRows, rows, fatRow, row})

selection-change

当选择项发生变化时会触发该事件

({fatRows, rows})

select-all

当用户手动勾选全选 Checkbox 时触发的事件

({fatRows, rows})

row-click

当某一行被点击时会触发该事件

({fatRow, column, event, row})

row-dblclick

当某一行被双击时会触发该事件

({fatRow, column, event, row})

FastTable Slots

slot名

说明

参数

default

默认插槽,就是安置FastTableColumn*用的

-

button

表格右上角操作按钮扩展位

{size, choseRow, checkedRows, editRows}

moreButton

表格“更多”下拉按钮扩展位

{size, choseRow, checkedRows, editRows}

foot

表格底部分页前面的扩展位

{size, choseRow, checkedRows, editRows}


评论