Skip to content

EditableProTable ​

可编辑表格 EditableProTable 与 ProTable 的功能基本相同,为了方便使用 EditableProTable,增加了一些预设,关掉了查询表单、操作栏和分页。

基础用法 ​

API ​

EditableProTable 在 el-table 上进行了一层封装,支持了一些预设。这里只列出与 el-table 不同的 API。

EditableProTable Attributes ​

参数说明类型默认值
dataSource表格数据DefaultRow[]—
recordCreatorProps新建一行数据的相关配置;false 隐藏按钮boolean | RecordCreatorProps—
maxLength最大行数,达到后隐藏新增按钮number—
editable可编辑表格的相关配置TableRowEditable—
rowKey行数据的 Keystring | ((row) => string)—
name可编辑表格嵌入表单时,需要校验时必填string—
classNameel-table 类名string—
loading加载状态boolean—
tablePropsel-table 属性TableProps—
tableEventsel-table 事件TableEmits—
columns列定义EditableColumnsConfig[]—
defaultSize默认组件尺寸'' | 'large' | 'default' | 'small'—
ts
type DefaultRow = Record<string | number | symbol, any>

EditableProTable Methods ​

方法名说明类型
getFormRef获取内部表单 ref() => FormInstance | undefined
getTableRef获取 el-table ref() => TableInstance | undefined
validateRow校验指定行(index: number) => Promise<boolean>
getRowData获取行数据(rowIndex: number | RecordKey) => DefaultRow | undefined
getRowsData获取全部行数据() => DefaultRow[]
setRowData设置行数据(rowIndex: number | RecordKey, data: DefaultRow) => void

recordCreatorProps ​

参数说明类型默认值
position顶部添加还是末尾添加'top' | 'bottom'bottom
newRecordType新增一行的方式,默认是缓存,取消后就会消失。设置为 dataSource,取消后也不会消失,只能删除'dataSource' | 'cache'cache
record新增行的默认值T | ((index, dataSource) => T)—
creatorButtonText设置按钮文案string添加一行数据
style按钮的样式设置,可以设置按钮是否显示,这样可以做最大行限制和最小行限制之类的功能CSSProperties—
buttonPropsel-button propsButtonProps—
onlyAddOneLineAlertMessage只能同时新增一行的提示,newRecordType 设置 cache 时会校验string只能新增一行

新增一行的时候要保证 recordCreatorProps.record key 唯一,不然会导致编辑出错。

tsx
<EditableProTable
  rowKey="id"
  :recordCreatorProps={
    // 每次新增的时候需要Key
    record: () => ({ id: getNewId() }),
  }
/>

editable ​

参数说明类型默认值
type可编辑表格的类型,单行编辑或者多行编辑'single' | 'multiple'single
editableKeys正在编辑的行,受控属性。 默认 key 会使用 rowKey 的配置,如果没有配置会使用 index,建议使用 rowKeyRecordKey[]—
onChange行数据被修改的时候触发(editableKeys: RecordKey[], editableRows: T[]) => void—
onSave保存一行的时候触发(recordKey: RecordKey, editRow: T, originRow?: T) => Promise<any>—
saveText保存一行的文字string保存
onDelete删除一行的时候触发(recordKey: RecordKey, editRow: T) => Promise<any>—
deleteText删除按钮文本string删除
onCancel取消编辑一行时触发(recordKey: RecordKey, editRow: T, originRow?: T) => Promise<any>—
cancelText取消按钮文本string取消
actionRender自定义编辑模式的操作栏( row: T, config: ActionRenderConfig<T>, defaultDom: ActionRenderDefaultDom ) => VNode[]—
deletePopconfirmMessage删除时弹出的确认框提示消息string删除此项?
onlyOneLineEditorAlertMessage只能编辑一行的的提示string只能同时编辑一行
ts
interface ActionRenderDefaultDom {
  save: VNode
  delete: VNode
  cancel: VNode
}

interface ActionRenderConfig<T extends DefaultRow = DefaultRow> {
  editableKeys?: TableRowEditable<T>['editableKeys']
  setEditableRowKeys: (value: RecordKey[]) => void
  recordKey: RecordKey
  preEditRow: T
  index?: number
  onSave: NonNullable<TableRowEditable<T>['onSave']>
  onDelete?: TableRowEditable<T>['onDelete']
  onCancel: NonNullable<TableRowEditable<T>['onCancel']>
  cancelEditable: (key: RecordKey) => void
  newLineConfig?: NewLineConfig<T>
  saveText?: string | VNode
  deleteText?: string | VNode
  cancelText?: string | VNode
  deletePopconfirmMessage: NonNullable<TableRowEditable<T>['deletePopconfirmMessage']>
  addEditRecord?: (row: T, options?: AddLineOptions) => boolean
  tableName?: string
}

interface ActionConfig {
  startEditable: (rowKey: RecordKey) => boolean
  cancelEditable: (rowKey: RecordKey) => Promise<boolean>
  addEditRecord: (record: any, newLine?: Partial<RecordCreatorProps<any>>) => void
}

columnsConfig ​

在 el-table-column attributes 的基础上新增了以下 API。

参数说明类型默认值
formItemProps编辑态 el-form-item 配置FormItemProps—
valueType表单元素类型;option 为操作列见 valueType—
renderField自定义编辑单元格(data: { form: any, formItem: ProFormItemProps, recordKey: RecordKey, index: number }) => VNode—
fieldProps表单元素 propsRecord<string, any>—
fieldEvents表单元素 eventsRecord<string, any>—
valueEnum选择器枚举 / 只读展示Record | Map—
optionLoader异步加载选项() => Promise<Record<string, any>[]>—
hideInTable在表格隐藏boolean—
renderCellHeader自定义表头(scope: { column: TableColumnCtx<DefaultRow>, $index: number }) => VNode—
renderCell自定义表格内容,操作栏多一个 actions(scope: any, actions?: ActionConfig) => VNode | VNode[]—
editable是否可编辑(cellValue: any, record: DefaultRow, index: number) => boolean—
readonly只读boolean—
keyvue 需要的 key,如果是 type: index|selection|expand 或已经设置了唯一的 prop,可以忽略这个属性string—

表单元素绑定 column.prop;操作列需 valueType: 'option'。

fieldEvents 封装了一层,添加了一个额外的 options 对象实参,包含了 recordKey、row 和 index 等 key。

valueType slot ​

name描述
[prop]自定义编辑单元格,参数 { form: any, formItem: ProFormItemProps, recordKey: RecordKey, index: number }