New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

vue-element-extends

Package Overview
Dependencies
Maintainers
1
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vue-element-extends

Extension component based on ElementUI 2.x

  • 1.2.0-beta.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
105
increased by1.94%
Maintainers
1
Weekly downloads
 
Created
Source

vue-element-extends

npm version npm downloads gzip size: JS gzip size: CSS npm license

基于 ElementUI 2.x 的扩展组件:Editable.vue、EditableColumn.vue

  • 功能点:
    • 支持只读、单元格编辑、整行编辑
    • 支持手动、单击、双击编辑模式
    • 支持渲染简化的 ElementUI 组件
    • 支持自定义渲染任意 Vue 组件
    • 支持动态列渲染
    • 支持(同步、异步)校验
    • 支持显示单元格值的修改状态
    • 支持增/删/改/查/还原
    • 支持导出 .csv 文件
    • 支持方向键和 Tab 键切换单元格
    • 支持表格右键菜单
    • 支持原 ElTable 的所有功能、参数、方法、插槽

Docs

https://xuliangzhan.github.io/vue-element-extends/

Installing

npm install xe-utils vue-element-extends

unpkgcdnjs 上获取

<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<link rel="stylesheet" href="https://unpkg.com/vue-element-extends/lib/index.css">
<!-- 引入脚本 -->
<script src="https://unpkg.com/xe-utils"></script>
<script src="https://unpkg.com/vue-element-extends"></script>

Case 1:

import Vue from 'vue'
import { Editable, EditableColumn } from 'vue-element-extends'
import 'vue-element-extends/lib/index.css'

Vue.use(Editable)
Vue.use(EditableColumn)

Case 2:

import Vue from 'vue'
import VueElementExtends from 'vue-element-extends'
import 'vue-element-extends/lib/index.css'

Vue.use(VueElementExtends)

API

Editable Attributes

<elx-editable
  ref="editable"
  :edit-config="{trigger: 'click', mode: 'cell'}"
  :edit-rules="{name: [{required: true, message: 'Please enter a name.', trigger: 'blur'}]}">
  <elx-editable-column
    prop="name"
    label="Name"
    :edit-render="{name: 'ElInput'}"></elx-editable-column>
  <elx-editable-column
    prop="age"
    label="Age"
    :edit-render="{name: 'ElInputNumber'}"></elx-editable-column>
</elx-editable>

edit-rules 校验规则配置:

属性描述类型可选值默认值
required是否必填Boolean
min校验值最小长度(如果 type=number 则比较值大小)Number
max校验值最大长度(如果 type=number 则比较值大小)Number
type类型校验Stringnumber / stringstring
pattern正则校验RegExp
validator自定义校验方法Function(rule, value, callback)
trigger触发校验方式Stringblur / changeblur

edit-config 编辑参数配置:

属性描述类型可选值默认值
trigger触发方式Stringmanual(手动触发方式,只能用于 mode=row) / click(点击触发编辑) / dblclick(双击触发编辑)click
mode编辑模式Stringcell(单元格编辑模式) / row(行编辑模式)cell
props配置节点对应属性值Object{children: 'children'}
showIcon是否显示列头编辑图标Booleantrue
showStatus是否显示单元格值的修改状态Booleantrue
activeMethod只对 type=default 的列有效,该函数 Function({row, rowIndex, column?, columnIndex?}) 的返回值用来决定这一行或列是否允许编辑Function
autoClearActive当点击其它地方后,是否自动清除最后活动行或列Booleantrue
clearActiveMethod该函数 Function({type, row, rowIndex, column?, columnIndex?}, event) 的返回值用来决定是否允许清除当前活动行或单元格Function
useDefaultValidTip如果同时使用了数据校验和 fixed 列,请设置为 true 使用默认提示Booleanfalse
validTooltip只对 useDefaultValidTip=false 有效,设置校验 tooltip 提示消息的参数Object{ offset: 10, placement: 'bottom-start' }
disabledValidTip关闭校验提示Booleanfalse
autoScrollIntoView当单元格被激活时,自动将单元格滚动到可视区域内Booleanfalse
isTabKey只对 trigger!=manual 有效,是否启用 Tab 键切换到下一个单元格Booleanfalse
isArrowKey只对 trigger!=manual 有效,是否启用箭头键切换行和单元格Booleanfalse
isCheckedEdit只对 trigger!=manual 有效,是否启用选中状态允许值覆盖式编辑Booleanfalse
checkedEditMethod只对 isCheckedEdit=true 有效,可以通过重写该函数 Function({row, rowIndex, column, columnIndex, cell}, event) 返回 false 来阻止默认值的覆盖行为Function

context-menu-config 表格右键菜单配置:

属性描述类型参数
disabledHeader是否禁用表格头部右键Boolean
headerMenus表格头部菜单配置Array{ code, name, prefixIcon, suffixIcon, visible, disabled }
disabledBody是否禁用表格内容右键Boolean
bodyMenus表格内容菜单配置Array{ code, name, prefixIcon, suffixIcon, visible, disabled }
headerVisibleMethod该函数 Function({row, rowIndex, column, columnIndex, cell}, event) 的返回值用来决定是否显示头部右键菜单Function
bodyVisibleMethod该函数 Function({row, rowIndex, column, columnIndex, cell}, event) 的返回值用来决定是否显示内容右键菜单Function

内置的菜单 code 可选值:

编码描述
CELL_RESET清除单元格数据的值
CELL_REVERT还原单元格数据
SELECT_REMOVE删除选中的行
SELECT_RESET清除选中的数据的值
SELECT_REVERT还原选中的数据
SELECT_EXPORT导出选中的数据
ROW_INSERT在当前位置插入新行
ROW_REMOVE删除当前行
ROW_RESET清除当前行数据的值
ROW_REVERT还原当前行数据
ROW_EXPORT导出当前行数据
ALL_REMOVE删除表格所有行
ALL_RESET清除表格所有数据的值
ALL_REVERT还原表格所有数据
ALL_EXPORT导出表格所有数据

Editable Events

事件名说明参数
valid-error校验不通过时会触发该事件rule,row,column,cell
edit-disabled当点击后行或单元格如果是禁用状态时会触发该事件row[,column,cell]?,event
edit-active当点击后改变为编辑状态之后会触发该事件row[,column,cell]?,event
clear-active只对 autoClearActive=true 有效,当点击其它地方后,自动清除最后活动行或列之后会触发该事件row[,column,cell]?,event
blur-active当行或者单元格失焦之后会触发该事件row[,column,cell]?,event
custom-menu-link自定义的菜单点击后触发该函数code,row,column,cell,event

Editable Methods

方法名描述参数
refresh手动刷新表格,对于非双向同步的树形结构可能会用到
reload初始化完整表格数据datas
reloadRow初始化指定行数据row
revert放弃更改,还原指定行 row 或者整个表格的数据row?rows?
insert从第一行新增一行新数据data
insertAt第二个参数 row 从指定位置新增一条数据; null 从第一行新增一行新数据;-1 从最后新增一条数据data,row
remove数据删除,指定 row 或 [row, ...] 删除多条数据row?rows?
(v1.2+废弃) removes根据多条数据删除rows
removeSelecteds删除选中行数据
clear清空表格,删除表格所有行
reset重置数据,清除指定行 row 或者 [row, ...] 或者整个表格的值row?rows?
clearActive清除所有已激活的行或列为不可编辑状态
hasActiveRow判断当前是否已激活为编辑状态的行row
getActiveRow获取当前已激活为编辑行或列的信息
setActiveRow只对 mode=row 有效,激活指定行为可编辑状态row
setActiveCell激活指定某一行的某个单元格为可编辑状态row,prop?
hasRowInsert检查是否为新增的行数据row
hasRowChange检查行或列数据是否发生改变row, prop?
updateStatus更新单元格编辑状态(只对 showStatus=true 并且使用自定义渲染时,当值发生改变时才需要调用)scope
getAllRecords获取表格数据集合
getRecords获取表格数据,也可以指定索引获取某条数据index
getInsertRecords获取新增数据
getRemoveRecords获取已删除数据
getUpdateRecords获取已修改数据
checkValid检测是否有校验不通过的列信息
validateRow对表格某一行进行校验的方法,参数为行数据和一个回调函数。该回调函数会在校验结束后被调用,并传入两个参数:(是否校验成功,最近一列未通过校验的字段)。若不传入回调函数,则会返回一个 promiserow,callback
validate对整个表格进行校验的方法,参数为一个回调函数。该回调函数会在校验结束后被调用,并传入两个参数:(是否校验成功,最近一列未通过校验的字段)。若不传入回调函数,则会返回一个 promisecallback
exportCsv将表格数据导出为 .csv 文件,说明:支持IE9+、Edge、Chrome、Firefox 等常用浏览器。IE11以下可能存在中文乱码问题,部分浏览器需要手动修改后缀名为 .csvoptions

Editable-Column Attributes

<elx-editable-column
  prop="name"
  label="Name"
  :edit-render="{name: 'ElInput'}"></elx-editable-column>

edit-render 渲染参数配置

属性描述类型可选值默认值
name渲染内置的组件名称StringElAutocomplete / ElInput / ElSelect / ElCascader / ElTimeSelect / ElTimePicker / ElDatePicker / ElInputNumber / ElSwitch / ElRate / ElColorPicker / ElSliderElInput
type渲染类型Stringdefault(组件触发后可视) / visible(组件一直可视)default
autofocus该单元格在激活后自动获取焦点(如果是渲染自定义组件,需要指定 class=elx-custom_input 才会自动获得焦点)Boolean
attrs渲染组件附加属性,参数请查看被渲染的 Component attrsObject{}
events渲染组件附加事件,参数为 ( { rule, row, column, $index }, ...Component arguments )Object{}
options只对 name=ElSelect 有效,下拉组件选项列表Array[]
optionProps只对 name=ElSelect 有效,下拉组件选项属性参数配置Object{ value: 'value', label: 'label' }
optionGroups只对 name=ElSelect 有效,下拉组件分组选项列表Array[]
optionGroupProps只对 name=ElSelect 有效,下拉组件分组选项属性参数配置Object{ options: 'options', label: 'label' }

Editable-Column Scoped Slot

name说明
自定义渲染显示内容,参数为 { row, column, $index, $render }
edit自定义渲染组件,参数为 { row, column, $index, $render }
header自定义表头的内容,参数为 { column, $index, $render }
valid自定义校验提示信息,参数为 { rule, row, column, $index, $render }

Example

Run v1.1+ demo on jsfiddle.net or runjs
Run v1.0+ demo on jsfiddle.net or runjs

😱编辑表格响应属性及渲染开销较大,不适用于一页显示海量数据的表格;适用于使用分页加载的数据表格😱
也可以把 packages 中的 editable.vue 和 editable-column.vue 组件复制到自己项目中注册,再根据项目需求去做修改
如果有更好优化建议或遇到问题欢迎提 Issues

<template>
  <div>
    <el-button @click="$refs.editable.insert({name: 'new1'})">新增</el-button>
    <el-button @click="$refs.editable.removeSelecteds()">删除选中</el-button>
    <el-button @click="$refs.editable.clear()">清空表格</el-button>

    <elx-editable
      ref="editable"
      :data.sync="tableData">
      <elx-editable-column
        type="selection"
        width="55"></elx-editable-column>
      <elx-editable-column
        type="index"
        width="55"></elx-editable-column>
      <elx-editable-column
        prop="name"
        label="只读"></elx-editable-column>
      <elx-editable-column
        prop="sex"
        label="下拉"
        :edit-render="{name: 'ElSelect', options: sexList}"></elx-editable-column>
      <elx-editable-column
        prop="num"
        label="数值"
        :edit-render="{name: 'ElInputNumber'}"></elx-editable-column>
      <elx-editable-column
        prop="date"
        label="日期"
        :edit-render="{name: 'ElDatePicker', attrs: {type: 'date', format: 'yyyy-MM-dd'}}"></elx-editable-column>
      <elx-editable-column
        prop="flag"
        label="开关"
        :edit-render="{name: 'ElSwitch', type: 'visible'}"></elx-editable-column>
      <elx-editable-column
        prop="remark"
        label="文本"
        :edit-render="{name: 'ElInput'}"></elx-editable-column>
    </elx-editable>
  </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [{
        date: 1551322088449,
        name: '小徐',
        sex: '1',
        num: '26',
        flag: false,
        remark: '备注'
      }],
      sexList: [
        {
          'label': '男',
          'value': '1'
        },
        {
          'label': '女',
          'value': '0'
        }
      ]
    }
  }
}
</script>

License

Copyright (c) 2017-present, Xu Liangzhan

Keywords

FAQs

Package last updated on 13 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc