0.0.5 • Published 5 years ago

@agggin/vue-easytable v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Table props

参数说明类型可选值默认值
widthtable 宽度number
min-widthtable 最小宽度(当设置了横向自适应时生效)number
heighttable 高度number
min-heighttable最小高度(当设置了纵向自适应时生效)number
title-row-height表头行高number38px
row-height表格体行高string40px
footer-row-heightfooter 每一行的高度number40
is-horizontal-resize是否开启横向自适应(开启后会随着窗口改变而改变、根据表格宽度自动填充),注意需要设置style="width:100%"booleanfalse
is-vertical-resize是否开启纵向自适应(开启后会随着窗口改变而改变、根据表格高度自动填充)booleanfalse
vertical-resize-offset垂直自适应的偏移量,一般是表格下方有分页时会用到,使其成为自适应的一部分number-
table-bg-color表格背景颜色string#fff
title-bg-color表头背景颜色string#fff
odd-bg-color表格体奇数行颜色string
even-bg-color表格体偶数行颜色string
multiple-sort是否开启多列排序booleantrue
sort-always是否只在升序和倒序切换booleanfalse
table-data表格数据集合-array
columns列的集合。具体参考columnsarray
title-rows复杂表头集合,只有复杂表头时用到。具体参考titleRowsarray
paging-index当前分页序号,结合分页需要显示在表格内时(一般是分页的行号)number
error-content表体没数据时显示的内容html/string’暂无数据‘
error-content-height表体没数据时区域高度number50px
is-loading是否正在加载,异步请求结束要显示的设置falsebooleanfalse
loading-content加载时的内容html/stringloading 菊花效果
loading-opacity加载时透明度number0~10.6
row-hover-color表格体行浮动背景色string-
row-click-color表格体行点击后的背景色(行高亮)string
show-vertical-border是否显示垂直border,false 时列拖动失效booleantrue
show-horizontal-border是否显示横向borderbooleantrue
column-width-drag是否开启列宽拖动booleanfalse
row-click行单击回调Function({rowIndex, rowData, column})
row-dblclick行双击回调Function({rowIndex, rowData, column})
title-click表头单元格单击回调Function({title,field})
title-dblclick表头单元格双击回调Function({title,field})
row-mouse-enter鼠标进入表体行的回调Function({rowIndex})
row-mouse-leave鼠标离开表体行的回调Function({rowIndex})
drag-done拉动表头宽度后的回调Function({rowIndex, rowData, column})
filter-method数据筛选的回调Function({filters})
cell-edit-formatter 1.3.5 版本废除单元格编辑格式化回调(可以对编辑的结果设置样式等)Function({newValue,oldValue,rowIndex,rowData,field})
cell-edit-done单元格编辑完成回调Function({newValue,oldValue,rowIndex,rowData,field})
cell-merge单元格合并,支持rowSpan、colSpan,返回值{colSpan: 1,rowSpan: 1,content: '',componentName: ''};合并后单元格的内容可以通过content(html)设置也可以通过,componentName(自定义组件)设置Function({rowIndex,rowData,field})
select-all全选时触发Function({selection}) 参数说明: selection 当前选中的项集合
select-change某一项 checkbox 触发Function({selection,rowData}) 参数说明:selection为当前选中的项集合,rowData为当前选中的项
select-group-change选中项发生变化触发Function({selection}) 参数说明: selection为当前选中的项集合
column-cell-class-name表体单元格设置classNameFunction({rowIndex,columnName,rowData})

columns(Table props)

参数说明类型可选值默认值
field对应列的字段string
title列头显示文字string
titleAlign表头列内容对齐方式stringleft/center/right
columnAlign标体列内容对齐方式stringleft/center/right
formatter用来格式化内容Function(rowData,rowIndex,pagingIndex,field)
componentName自定义列传入组件的名字string
titleCellClassName表头单元格设置classNamestring
isFrozen此列是否要固定booleanfalse
orderBy排序规则stringasc/desc
isReadonly是否关闭单元格编辑booleanfalse
overflowTitle是否内容超出显示title提示booleanfalse
filters数据过滤条件,具体格式见demoarray-
filterMultiple是否支持过滤条件多选booleanfalse
className列类名string-
dataType列类型默认居中对齐,字符类型1默认居中,数字类型2为右对齐number1

table-data(Table props)

table 表格数据,通过设置一些特殊属性实现某些功能,如 checkbox 多选功能

参数说明类型可选值默认值
_checked是否选中(当开启多选时有效)booleanfalse
_disabled是否禁用选中\未选中(当开启多选时有效)booleanfalse

Table Event(注意传入的‘事件名称’必须和api保持一致)

事件名称说明回调参数
on-custom-comp自定义列为组件时,子组件与父组件通讯的方法params (参数为任意类型,根据业务场景随意构造,由自定义组件设置)
sort-change点击排序回调params 参数说明: 参数为排序的列对象信息,形如:{name: "asc", tel: "desc"}

Table Methods

事件名称说明回调参数
resize强制自适应(当表格有显示隐藏切换时需要用到)
clearCurrentRow取消当前选中的行
resetOrder重置还原排序规则,同时会触发sort-change事件
scrollToTop回到顶部的方法(当有垂直滚动条时)
<template>
     <div class="p-test">
       <template v-if="isBrowser">
         <v-table
           ref="table"
           class="erp-table"
           is-horizontal-resize
           column-width-drag
           :height="600"
           style="width:100%"
           :columns="columns"
           :table-data="tableData"
           :row-click="clickHandle"
           :row-dblclick="clickHandle"
           :column-cell-class-name="columnCellClass"
           row-hover-color="#eee"
           :cell-edit-done="cellEditDone"
           :select-all="testSelect"
           :select-change="testSelect"
           :drag-done="dragDoneHandle"
           @on-custom-comp="customCompHandle"
           :select-group-change="testSelect"
           row-click-color="#edf7ff"></v-table>
       </template>
     </div>
   </template>
   <script type="es6">
     import {getConfig, getData} from '../api'
     import UMenu from '@/components/UMenu'
     import {initComponent} from '@/utils'
     import Vue from 'vue'
     import {config, itemSources} from '../mock'

     function filterConfig (config) {
       let exportConfig = [
         {width: 60, titleAlign: 'center', columnAlign: 'center', isReadonly: true, type: 'selection'},
         {width: 40, titleAlign: 'center', columnAlign: 'center', isReadonly: true, title: '操作', componentName: 'op-menus'},
         {width: 30, titleAlign: 'center', columnAlign: 'center', isReadonly: true, title: '出', componentName: 'out-btn'},
         {width: 30, titleAlign: 'center', columnAlign: 'center', isReadonly: true, title: '入', componentName: 'in-btn'},
         {width: 30, titleAlign: 'center', columnAlign: 'center', isReadonly: true, title: '报', componentName: 'post-btn'}]
       config.forEach(item => {
         if (item.IsShow === false) {
           return true
         }
         exportConfig.push({
           field: item.BindField,
           title: item.CTitle,
           width: item.ColumnWidth,
           isReadonly: !item.IsReadonly,
           className: '',
           isRequired: item.IsRequired,
           dataType: item.DataType
         })
       })
       return exportConfig
     }

     export default {
       async asyncData () {
       },
       data () {
         return {
           columns: [],
           tableData: [],
           selected: false,
           isBrowser: false,
           totalCount: 0,
           pageSize: 1,
           pageIndex: 1
         }
       },
       computed: {
         systemList () {
           return _.filter(this.columns, item => item.isSystem)
         }
       },
       methods: {
         testSelect () {},
         columnCellClass (rowIndex, columnName, rowData) {
           if (this.systemList.includes(columnName)) {
             console.log(columnName)
           }
         },
         cellEditDone (newValue, oldValue, rowIndex, rowData, field) {
           let data = this.tableData[rowIndex]
           data[field] = newValue
           this.$set(this.tableData, rowIndex, data)
           // 接下来处理你的业务逻辑,数据持久化等...
         },
         pageChange (pageIndex) {
           this.pageIndex = pageIndex
           this.getData()
         },
         pageSizeChange (pageSize) {
           this.pageIndex = 1
           this.pageSize = pageSize
           this.getTableData()
         },
         customCompHandle (e) {
         },
         async getResultList () {
           this.pageIndex = 1
           this.pageSize = 20
           this.totalCount = 20
           this.tableData = itemSources
         },
         async getConfig () {
           this.columns = filterConfig(config)
         },
         clickHandle(rowIndex, rowData, column) {
         },
         dragDoneHandle(e) {
           console.log(this.$refs.table.editRows, 'edit')
         }
       },
       created () {
       },
       components: {},
       mounted () {
         this.isBrowser = true
         this.getConfig()
         this.getResultList()
       }
     }

     function componentFactory (name, type, options) {
       let render = function(h) {
         return h('a', {
           'class': [name, type],
           on: {
             click: () => {
               this.$emit('on-custom-comp', {
                 name,
                 rowData: this.rowData,
                 field: this.field,
                 index: this.index
               })
             }
           }
         }, options.btnTxt)
       }
       let config = {
         render,
         props: {
           rowData: {
             type: Object
           },
           field: {
             type: String
           },
           index: {
             type: Number
           }
         }
       }
       return config
     }

     Vue.component('out-btn', componentFactory('out-btn', 'btn', {
       btnTxt: '出'
     }))
     Vue.component('in-btn', componentFactory('in-btn', 'btn', {
       btnTxt: '入'
     }))
     Vue.component('post-btn', componentFactory('post-btn', 'btn', {
       btnTxt: '报'
     }))
     Vue.component('op-menus', initComponent(UMenu, {
       config: [
         {
           title: '询价',
           name: 'inquiry'
         }, {
           title: '销售备货',
           name: 'xs'
         }, {
           title: '采购订货',
           name: 'cg'
         }, {
           title: '删除库存',
           name: 'delete'
         }
       ]
     }))
   </script>
   <style lang="scss">
     .erp-table {
       .v-table-body-cell {
         font-size: 12px;
         box-sizing: border-box;
         line-height: 26px;
         input {
           outline: none;
           border: none;
           background: none;
         }
       }
       .v-table-header-row {
         background-color: #e7f1fb;
         font-size: 12px;
       }
       .btn {
         cursor: pointer;
       }
     }
   </style>