Table props
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|
width | table 宽度 | number | — | — |
min-width | table 最小宽度(当设置了横向自适应时生效) | number | — | — |
height | table 高度 | number | — | — |
min-height | table最小高度(当设置了纵向自适应时生效) | number | — | — |
title-row-height | 表头行高 | number | — | 38px |
row-height | 表格体行高 | string | — | 40px |
footer-row-height | footer 每一行的高度 | number | — | 40 |
is-horizontal-resize | 是否开启横向自适应(开启后会随着窗口改变而改变、根据表格宽度自动填充),注意需要设置style="width:100%" | boolean | — | false |
is-vertical-resize | 是否开启纵向自适应(开启后会随着窗口改变而改变、根据表格高度自动填充) | boolean | — | false |
vertical-resize-offset | 垂直自适应的偏移量,一般是表格下方有分页时会用到,使其成为自适应的一部分 | number | — | - |
table-bg-color | 表格背景颜色 | string | — | #fff |
title-bg-color | 表头背景颜色 | string | — | #fff |
odd-bg-color | 表格体奇数行颜色 | string | — | |
even-bg-color | 表格体偶数行颜色 | string | — | |
multiple-sort | 是否开启多列排序 | boolean | — | true |
sort-always | 是否只在升序和倒序切换 | boolean | — | false |
table-data | 表格数据集合 | - | — | array |
columns | 列的集合。具体参考columns | array | — | — |
title-rows | 复杂表头集合,只有复杂表头时用到。具体参考titleRows | array | — | — |
paging-index | 当前分页序号,结合分页需要显示在表格内时(一般是分页的行号) | number | — | — |
error-content | 表体没数据时显示的内容 | html/string | — | ’暂无数据‘ |
error-content-height | 表体没数据时区域高度 | number | — | 50px |
is-loading | 是否正在加载,异步请求结束要显示的设置false | boolean | — | false |
loading-content | 加载时的内容 | html/string | — | loading 菊花效果 |
loading-opacity | 加载时透明度 | number | 0~1 | 0.6 |
row-hover-color | 表格体行浮动背景色 | string | — | - |
row-click-color | 表格体行点击后的背景色(行高亮) | string | — | — |
show-vertical-border | 是否显示垂直border,false 时列拖动失效 | boolean | — | true |
show-horizontal-border | 是否显示横向border | boolean | — | true |
column-width-drag | 是否开启列宽拖动 | boolean | — | false |
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 | 表体单元格设置className | Function({rowIndex,columnName,rowData}) | — | — |
columns(Table props)
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|
field | 对应列的字段 | string | — | — |
title | 列头显示文字 | string | — | — |
titleAlign | 表头列内容对齐方式 | string | left/center/right | — |
columnAlign | 标体列内容对齐方式 | string | left/center/right | — |
formatter | 用来格式化内容 | Function(rowData,rowIndex,pagingIndex,field) | — | — |
componentName | 自定义列传入组件的名字 | string | — | — |
titleCellClassName | 表头单元格设置className | string | — | — |
isFrozen | 此列是否要固定 | boolean | — | false |
orderBy | 排序规则 | string | asc/desc | — |
isReadonly | 是否关闭单元格编辑 | boolean | — | false |
overflowTitle | 是否内容超出显示title提示 | boolean | — | false |
filters | 数据过滤条件,具体格式见demo | array | — | - |
filterMultiple | 是否支持过滤条件多选 | boolean | — | false |
className | 列类名 | string | — | - |
dataType | 列类型默认居中对齐,字符类型1默认居中,数字类型2为右对齐 | number | — | 1 |
table-data(Table props)
table 表格数据,通过设置一些特殊属性实现某些功能,如 checkbox 多选功能
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|
_checked | 是否选中(当开启多选时有效) | boolean | — | false |
_disabled | 是否禁用选中\未选中(当开启多选时有效) | boolean | — | false |
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>