cm-el v1.0.3
cm-ui
##使用cm-ui组件必须同时安装element-UI
npm i cm-el element-ui -S
引用组件
import ElementUI from 'element-ui'
Vue.use(ElementUI)
import 'element-ui/lib/theme-chalk/index.css'
import CmUI from 'cm-el'
Vue.use(CmUI)
Table 表格
用于展示多条结构类似的数据,可对数据进行排序、筛选、对比或其他自定义操作。
基础表格
基础的表格展示用法。
:::demo 在cm-table
元素中注入data
和columns
对象数组,columns
中用prop
属性来对应data
数组对象中的键名,用label
属性来定义表格的列名。
<template>
<cm-table :data="data" :columns="columns" />
</template>
<script>
export default {
data () {
return {
columns: [{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '出生日期',
prop: 'date'
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16'
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20'
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10'
}
]
}
}
}
</script>
:::
表格isIndex和列的type:index
表格isIndex和列的type:index用法。
:::demo 在cm-table
元素中注入isIndex
或在columns
数组对象中添加type
属性为index
的项会显示索引列。索引默认从1
开始,也可以在columns
数组对象中的index
属性中传入一个方法自定义。
<template>
<cm-table :data="data" :columns="columns" :isIndex="isIndex" />
</template>
<script>
export default {
data () {
return {
isIndex: true,
columns: [{
type: 'index',
label: '默认索引'
},
{
type: 'index',
label: '自定义索引',
index: this.indexMethod
},
{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '出生日期',
prop: 'date'
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16'
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20'
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10'
}
]
}
},
methods: {
indexMethod ({index}) {
return (index+1) * 2
}
}
}
</script>
:::
表格isSelection和列的type:selection
表格isSelection和列的type:selection属性用法。
:::demo 在cm-table
元素中注入isSelection
或在columns
数组对象中添加type
属性为selection
的项会显示多选列。通过selectable
传入方法的返回值用来决定这一行的 CheckBox
是否可以勾选。reserve-selection
为 true
则会在数据更新之后保留之前选中的数据(需在cm-table
中指定 row-key
)
<template>
<div>
<cm-table caption="多选表格示例1" :data="data" :columns="columns1" :isSelection="true" />
<cm-table caption="多选表格示例2" :data="data" :columns="columns2" />
</div>
</template>
<script>
export default {
data () {
return {
columns1: [
{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '出生日期',
prop: 'date'
}
],
columns2: [
{
type: 'selection',
label: '是否可选',
selectable: this.selectable,
},
{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '出生日期',
prop: 'date'
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16'
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20'
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10'
}
]
}
},
methods: {
selectable ({ row, index }) {
return index === 1
}
}
}
</script>
:::
表格isExpand和列的type:expand属性
表格isExpand和列的type:expand属性用法。
:::demo 在cm-table
元素中注入isExpand
或在columns
数组对象中添加type
属性为expand
的项会显示展开符,配合Scoped slot
使用。
<template>
<div>
<cm-table caption="表格展开示例1" :data="data" :columns="columns1" :isExpand="true">
<div slot="expand" slot-scope="{index,row,column}">
1111afsdgdfgfdhhf==={{index}}
</div>
</cm-table>
<cm-table caption="表格展开示例2" :data="data" :columns="columns2" :is-index="false">
<div slot="expand" slot-scope="{index,row,column}">
1111afsdgdfgfdhhf==={{index}}
</div>
</cm-table>
</div>
</template>
<script>
export default {
data () {
return {
columns1: [
{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '出生日期',
prop: 'date'
}
],
columns2: [
{
type:'expand'
},
{
type: 'selection',
label: '是否可选',
selectable: this.selectable,
},
{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '出生日期',
prop: 'date'
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16'
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20'
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10'
}
]
}
},
methods: {
selectable ({ row, index }) {
return index === 1
}
}
}
</script>
:::
列的render属性
表格列render属性用法。
:::demo 在columns
数组对象中的render
属性中传入一个Function
返回所需要的元素及内容,参数h,{index,column,row}
<template>
<cm-table :data="data" :columns="columns" :isIndex="true" />
</template>
<script>
export default {
data () {
return {
isIndex: true,
columns: [{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '出生日期',
prop: 'date'
},
{
label: '年龄',
render: this.renderAge
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16'
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20'
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10'
}
]
}
},
methods: {
renderAge (h, {
row
}) {
return h('span', this.getAge(row.date))
},
getAge (str) {
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})/)
if (r == null) return false
var d = new Date(r[1], r[3] - 1, r[4])
var returnStr = '输入的日期格式错误!'
if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
var date = new Date()
var yearNow = date.getFullYear()
var monthNow = date.getMonth() + 1
var dayNow = date.getDate()
var largeMonths = [1, 3, 5, 7, 8, 10, 12] // 大月, 用于计算天,只在年月都为零时,天数有效
var lastMonth = monthNow - 1 > 0 ? monthNow - 1 : 12 // 上一个月的月份
var isLeapYear = false // 是否是闰年
var daysOFMonth = 0 // 当前日期的上一个月多少天
if ((yearNow % 4 === 0 && yearNow % 100 !== 0) || yearNow % 400 === 0) { // 是否闰年, 用于计算天,只在年月都为零时,天数有效
isLeapYear = true
}
if (largeMonths.indexOf(lastMonth) > -1) {
daysOFMonth = 31
} else if (lastMonth === 2) {
if (isLeapYear) {
daysOFMonth = 29
} else {
daysOFMonth = 28
}
} else {
daysOFMonth = 30
}
var Y = yearNow - parseInt(r[1])
var M = monthNow - parseInt(r[3])
var D = dayNow - parseInt(r[4])
if (D < 0) {
D = D + daysOFMonth // 借一个月
M--
}
if (M < 0) { // 借一年 12个月
Y--
M = M + 12 //
}
if (Y < 0) {
returnStr = '出生日期有误!'
} else if (Y === 0) {
if (M === 0) {
returnStr = D + 'D'
} else {
returnStr = M + 'M'
}
} else {
if (M === 0) {
returnStr = Y + '岁'
} else {
returnStr = Y + '岁' + M + '月'
}
}
}
return returnStr
}
}
}
</script>
:::
列的formatter属性
表格列formatter属性用法。
:::demo 传入一个方法,参数为{index,row,column}
,返回值为string
。
<template>
<cm-table :data="data" :columns="columns" :isIndex="true" />
</template>
<script>
export default {
data () {
return {
isIndex: true,
columns: [
{
label: '姓名',
prop: 'name',
align:'center'
},
{
label: '性别',
prop: 'sex',
align:'center'
},
{
label: '出生日期',
prop: 'date',
sortable:true,
align:'center'
},
{
label:'formatter属性',
prop:'test',
formatter:({index,row,column})=>{
return column.prop+index
}
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16'
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20'
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10'
}
]
}
},
methods: {
indexMethod ({index}) {
return (index+1) * 2
}
}
}
</script>
:::
列的model属性和format属性
表格列的model属性和format属性用法。
:::demo model
表示数据格式化方式,可选值有date
和number
<template>
<cm-table :data="data" :columns="columns" :isIndex="true" />
</template>
<script>
export default {
data () {
return {
isIndex: true,
columns: [
{
label: '姓名',
prop: 'name',
align:'center'
},
{
label: '性别',
prop: 'sex',
align:'center'
},
{
label: '出生日期',
prop: 'date',
sortable:true,
align:'center',
model:'date',
format:'yyyy-MM-dd hh:mm:ss'
},
{
label:'年收入',
prop:'money',
model:'amount',
format:'$',
pointCount:2
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16',
money:7242340000
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20',
money:9247340000
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10',
money:62423040000
}
]
}
}
}
</script>
:::
列的slot属性和slotHeader属性
表格列的slot属性和slotHeader属性用法。
:::demo 用slot
属性表示这一列的数据需要在组件使用时自定义,返回的参数{index,row,column}
;用slotHeader
属性表示这一列的数据需要在组件使用时自定义,返回的参数{cIndex,column}
。
<template>
<div>
<cm-table
:current-page.sync="current"
:page-size.sync="size"
:columns="columns"
:data="tableData"
border
:total='400'
@pagination="getCount">
<div slot="test" slot-scope="{index,row,column}">
<div>{{index}}</div>
</div>
<template slot="slotHeader">这是自定义的表头</template>
</cm-table>
</div>
</template>
<script>
export default {
data() {
return {
size: 10,
current: 4,
columns: [
{
label: 'ID',
prop: 'id'
},
{
label: '姓名',
prop: 'name'
},
{
label: '数值1',
prop: 'amount1',
sortable: true
},
{
label: '数值2',
prop: 'amount2',
sortable: true
},
{
label: '数值3',
prop: 'amount3',
sortable: true
},
{
label: '测试slot',
slot: 'test'
},
{
slotHeader: 'slotHeader',
prop: 'id'
}
],
tableData: [{
id: '12987122',
name: '王小虎',
amount1: '234',
amount2: '3.2',
amount3: 10
}, {
id: '12987123',
name: '王小虎',
amount1: '165',
amount2: '4.43',
amount3: 12
}, {
id: '12987124',
name: '王小虎',
amount1: '324',
amount2: '1.9',
amount3: 9
}, {
id: '12987125',
name: '王小虎',
amount1: '621',
amount2: '2.2',
amount3: 17
}, {
id: '12987126',
name: '王小虎',
amount1: '539',
amount2: '4.1',
amount3: 15
}]
}
},
methods: {
getCount(params) {
this.current = params.current || this.current
this.size = params.size || this.size
console.log(this.current + ';' + this.size)
}
}
}
</script>
:::
表格列其它属性
表格列属性的用法。
:::demo 参考element-ui
的列属性用法
<template>
<cm-table :data="data" :columns="columns" :isIndex="true" />
</template>
<script>
export default {
data () {
return {
isIndex: true,
columns: [
{
label: '固定宽度',
prop: 'name',
width:'140'
},
{
label: '固定在右侧',
prop: 'sex',
fixed:'right'
},
{
label: '排序',
prop: 'date',
sortable:true,
align:'center'
},
{
label:'formatter属性',
prop:'test',
formatter:({index,row,column})=>{
return column.prop+index
}
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16'
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20'
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10'
}
]
}
},
methods: {
indexMethod ({index}) {
return (index+1) * 2
}
}
}
</script>
:::
表格行样式属性
表格行样式属性row-class-name的用法。
:::demo 行的 className 的回调方法,参数{row,index}
;也可以使用字符串为所有行设置一个固定的 className。注意事项:需要去掉样式里边设置的scoped
属性。
<template>
<div>
<cm-table border caption="row-class-name为string类型" row-class-name="tableRowClassName" :data="data" :columns="columns" :isIndex="true" />
<cm-table caption="row-class-name为function类型" :row-class-name="tableRowClassName" :data="data" :columns="columns" :isIndex="true" />
</div>
</template>
<script>
export default {
data () {
return {
columns: [{
label: '姓名',
prop: 'name'
},
{
label: '性别',
prop: 'sex'
},
{
label: '出生日期',
prop: 'date'
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16'
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20'
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10'
}
]
}
},
methods: {
tableRowClassName ({row,index}) {
if (index === 0) {
return 'warning-row'
} else if (index === 2) {
return 'success-row'
}
return ''
}
}
}
</script>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
.el-table .tableRowClassName{
color: blueviolet;
}
</style>
:::
树形数据与懒加载
:::demo 支持树类型的数据的显示。当 row 中包含 children
字段时,被视为树形数据。渲染树形数据时,必须要指定 row-key
。支持子节点数据异步加载。设置 Table 的 lazy
属性为 true 与加载函数 load
。通过指定 row 中的 hasChildren
字段来指定哪些行是包含子节点。children
与 hasChildren
都可以通过 tree-props
配置。
<template>
<div>
<cm-table row-key="code" :tree-props="{children: 'subs'}" :data="data" :columns="columns" :isIndex="true" />
<cm-table row-key="code" lazy :load="getSubs" :tree-props="{ hasChildren: 'hasChildren'}" :data="data2" :columns="columns"
:isIndex="true" />
</div>
</template>
<script>
export default {
data () {
return {
isIndex: true,
columns: [{
label: '公司名称',
prop: 'name'
},
{
label: '代码',
prop: 'code'
},
{
label: '成立日期',
prop: 'date'
}
],
data: [{
name: '四川公司',
code: 'SCGS0000001',
date: '1960-10-16',
subs: [{
name: '四川子公司1',
code: 'SCGS0000001001',
date: '2001-9-20'
},
{
name: '四川子公司2',
code: 'SCGS0000001002',
date: '2018-9-20'
},
{
name: '四川子公司3',
code: 'SCGS0000001003',
date: '2011-9-20'
}
]
},
{
name: '北京公司',
code: 'BJGS0000001',
date: '1991-9-20',
subs: [{
name: '北京子公司1',
code: 'BJGS0000001001',
date: '2001-9-20'
},
{
name: '北京子公司2',
code: 'BJGS0000001002',
date: '2018-9-20'
},
{
name: '北京子公司3',
code: 'BJGS0000001003',
date: '2011-9-20'
}
]
}
],
data2: [{
name: '四川公司',
code: 'SCGS0000001',
date: '1960-10-16'
},
{
name: '北京公司',
code: 'BJGS0000001',
date: '1991-9-20',
hasChildren: true
}
]
}
},
methods: {
getSubs (tree, treeNode, resolve) {
setTimeout(() => {
resolve([{
name: '北京子公司1',
code: 'BJGS0000001001',
date: '2001-9-20'
},
{
name: '北京子公司2',
code: 'BJGS0000001002',
date: '2018-9-20'
},
{
name: '北京子公司3',
code: 'BJGS0000001003',
date: '2011-9-20'
}
])
}, 1000)
}
}
}
</script>
:::
表尾合计行
若表格展示的是各类数字,可以在表尾显示各列的合计。
:::demo 将show-summary
设置为true
就会在表格尾部展示合计行。默认情况下,对于合计行,第一列不进行数据求合操作,而是显示「合计」二字(可通过sum-text
配置),其余列会将本列所有数值进行求合操作,并显示出来。当然,你也可以定义自己的合计逻辑。使用summary-method
并传入一个方法,返回一个数组,这个数组中的各项就会显示在合计行的各列中,具体可以参考本例中的第二个表格。
<template>
<cm-table
:columns="columns"
:data="tableData"
border
show-summary
style="width: 100%"/>
<cm-table
:columns="columns"
:data="tableData"
border
height="200"
:summary-method="getSummaries"
show-summary
style="width: 100%; margin-top: 20px"/>
</template>
<script>
export default {
data() {
return {
columns: [{
label: 'ID',
prop: 'id'
},
{
label: '姓名',
prop: 'name'
},
{
label: '数值1',
prop: 'amount1',
sortable: true
},
{
label: '数值2',
prop: 'amount2',
sortable: true
},
{
label: '数值3',
prop: 'amount3',
sortable: true
}
],
tableData: [{
id: '12987122',
name: '王小虎',
amount1: '234',
amount2: '3.2',
amount3: 10
}, {
id: '12987123',
name: '王小虎',
amount1: '165',
amount2: '4.43',
amount3: 12
}, {
id: '12987124',
name: '王小虎',
amount1: '324',
amount2: '1.9',
amount3: 9
}, {
id: '12987125',
name: '王小虎',
amount1: '621',
amount2: '2.2',
amount3: 17
}, {
id: '12987126',
name: '王小虎',
amount1: '539',
amount2: '4.1',
amount3: 15
}]
};
},
methods: {
getSummaries(param) {
const { columns, data } = param;
const sums = [];
columns.forEach((column, index) => {
if (index === 0) {
sums[index] = '总价';
return;
}
const values = data.map(item => Number(item[column.property]));
if (!values.every(value => isNaN(value))) {
sums[index] = values.reduce((prev, curr) => {
const value = Number(curr);
if (!isNaN(value)) {
return prev + curr;
} else {
return prev;
}
}, 0);
sums[index] += ' 元';
} else {
sums[index] = 'N/A';
}
});
return sums;
}
}
};
</script>
:::
合并行或列
多行或多列共用一个数据时,可以合并行或列。
:::demo 通过给cm-table
传入span-method
方法可以实现合并行或列,方法的参数是一个对象,里面包含当前行row
、当前列column
、当前行号rowIndex
、当前列号columnIndex
四个属性。该函数可以返回一个包含两个元素的数组,第一个元素代表rowspan
,第二个元素代表colspan
。 也可以返回一个键名为rowspan
和colspan
的对象。
<template>
<div>
<div>
<cm-table :columns="columns" :data="tableData" :span-method="arraySpanMethod" border style="width: 100%"/>
<cm-table :columns="columns" :data="tableData" :span-method="objectSpanMethod" border style="width: 100%; margin-top: 20px"/>
</div>
</div>
</template>
<script>
export default {
data () {
return {
columns: [{
label: 'ID',
prop: 'id'
},
{
label: '姓名',
prop: 'name'
},
{
label: '数值1',
prop: 'amount1',
sortable: true
},
{
label: '数值2',
prop: 'amount2',
sortable: true
},
{
label: '数值3',
prop: 'amount3',
sortable: true
}
],
tableData: [{
id: '12987122',
name: '王小虎',
amount1: '234',
amount2: '3.2',
amount3: 10
}, {
id: '12987123',
name: '王小虎',
amount1: '165',
amount2: '4.43',
amount3: 12
}, {
id: '12987124',
name: '王小虎',
amount1: '324',
amount2: '1.9',
amount3: 9
}, {
id: '12987125',
name: '王小虎',
amount1: '621',
amount2: '2.2',
amount3: 17
}, {
id: '12987126',
name: '王小虎',
amount1: '539',
amount2: '4.1',
amount3: 15
}]
}
},
methods: {
arraySpanMethod ({
row,
column,
rowIndex,
columnIndex
}) {
if (rowIndex % 2 === 0) {
if (columnIndex === 0) {
return [1, 2]
} else if (columnIndex === 1) {
return [0, 0]
}
}
},
objectSpanMethod ({
row,
column,
rowIndex,
columnIndex
}) {
if (columnIndex === 0) {
if (rowIndex % 2 === 0) {
return {
rowspan: 2,
colspan: 1
}
} else {
return {
rowspan: 0,
colspan: 0
}
}
}
}
}
}
</script>
:::
表格分页
:::demo cm-table
默认分页,若不需要分页,可传入isPagination
不分页;当只有一页时,不显示分页组件,可以通过hideOnSinglePage
控制;当组件的page-size
或current-page
改变时,可以通过pagination
做相应的操作。
<template>
<div>
<cm-table :current-page.sync="current" :page-size.sync="size" :columns="columns" :data="data" border :total='400'
@pagination="getCount" />
</div>
</template>
<script>
export default {
data () {
return {
size: 10,
current: 4,
columns: [
{
label: '姓名',
prop: 'name',
align:'center'
},
{
label: '性别',
prop: 'sex',
align:'center'
},
{
label: '出生日期',
prop: 'date',
sortable:true,
align:'center',
model:'date',
format:'yyyy-MM-dd hh:mm:ss'
},
{
label:'年收入',
prop:'money',
model:'amount',
format:'$',
pointCount:2
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16',
money:7242340000
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20',
money:9247340000
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10',
money:62423040000
}
]
}
},
methods: {
getCount () {
console.log(this.current + ';' + this.size)
}
}
}
</script>
:::
表格loading、loading-text、loading-spinner和loading-background属性
表格loading、loading-text、loading-spinner和loading-background属性用法
:::demo loading
属性控制loading的显示隐藏,loading-text
属性值会被渲染为加载文案,并显示在加载图标的下方。类似地,loading-spinner
和loading-background
属性分别用来设定图标类名和背景色值。
<template>
<div>
<cm-table
:loading="true"
:current-page.sync="current"
:page-size.sync="size"
:columns="columns"
:data="data"
border
:total='400'
@pagination="getCount" />
</div>
</template>
<script>
export default {
data () {
return {
size: 10,
current: 4,
columns: [
{
label: '姓名',
prop: 'name',
align:'center'
},
{
label: '性别',
prop: 'sex',
align:'center'
},
{
label: '出生日期',
prop: 'date',
sortable:true,
align:'center',
model:'date',
format:'yyyy-MM-dd hh:mm:ss'
},
{
label:'年收入',
prop:'money',
model:'amount',
format:'$',
pointCount:2
}
],
data: [{
name: '赵丽颖',
sex: '女',
date: '1987-10-16',
money:7242340000
},
{
name: '胡歌',
sex: '男',
date: '1982-9-20',
money:9247340000
},
{
name: '刘诗诗',
sex: '女',
date: '1987-3-10',
money:62423040000
}
]
}
},
methods: {
getCount () {
console.log(this.current + ';' + this.size)
}
}
}
</script>
:::
Table Attributes
参数 | 说明 | 类型 | 可选值 | 默认值 | |
---|---|---|---|---|---|
caption | 表格标题 | String | — | — | |
columns | 表头数据 | Array | — | — | |
data | 显示的数据 | Array | — | — | |
is-selection | 是否多选 | Boolean | true;false | false | |
is-index | 是否显示索引 | Boolean | true;false | true | |
is-expand | 是否显示展开行 | Boolean | true;false | false | |
row-class-name | 行的 className | Function({row, index})/String | — | — | |
height | Table 的高度,默认为自动高度。如果 height 为 number 类型,单位 px;如果 height 为 string 类型,则这个高度会设置为 Table 的 style.height 的值,Table 的高度会受控于外部样式。 | String/Number | — | — | |
max-height | Table 的最大高度。合法的值为数字或者单位为 px 的高度。 | String/Number | — | — | |
stripe | 是否为斑马纹 table | Boolean | true;false | false | |
border | 是否带有纵向边框 | Boolean | true;false | false | |
size | Table 的尺寸 | String | medium / small / mini | — | |
fit | 列的宽度是否自撑开 | Boolean | true;false | true | |
show-header | 是否显示表头 | Boolean | true;false | true | |
highlight-current-row | 是否要高亮当前行 | Boolean | true;false | false | |
current-row-key | 当前行的 key,只写属性 | String,Number | — | — | |
row-class-name | 行的 className 的回调方法,也可以使用字符串为所有行设置一个固定的 className。 | Function({row, rowIndex})/String | — | — | |
row-style | 行的 style 的回调方法,也可以使用一个固定的 Object 为所有行设置一样的 Style。 | Function({row, rowIndex})/Object | — | — | |
cell-class-name | 单元格的 className 的回调方法,也可以使用字符串为所有单元格设置一个固定的 className。 | Function({row, column, rowIndex, columnIndex})/String | — | — | |
cell-style | 单元格的 style 的回调方法,也可以使用一个固定的 Object 为所有单元格设置一样的 Style。 | Function({row, column, rowIndex, columnIndex})/Object | — | — | |
header-row-class-name | 表头行的 className 的回调方法,也可以使用字符串为所有表头行设置一个固定的 className。 | Function({row, rowIndex})/String | — | — | |
header-row-style | 表头行的 style 的回调方法,也可以使用一个固定的 Object 为所有表头行设置一样的 Style。 | Function({row, rowIndex})/Object | — | — | |
header-cell-class-name | 表头单元格的 className 的回调方法,也可以使用字符串为所有表头单元格设置一个固定的 className。 | Function({row, column, rowIndex, columnIndex})/String | — | — | |
header-cell-style | 表头单元格的 style 的回调方法,也可以使用一个固定的 Object 为所有表头单元格设置一样的 Style。 | Function({row, column, rowIndex, columnIndex})/Object | — | — | |
row-key | 行数据的 Key,用来优化 Table 的渲染;在使用 reserve-selection 功能与显示树形数据时,该属性是必填的。类型为 String 时,支持多层访问:user.info.id ,但不支持 user.info[0].id ,此种情况请使用 Function 。 | Function(row)/String | — | — | |
empty-text | 空数据时显示的文本内容,也可以通过 slot="empty" 设置 | String | — | 暂无数据 | |
default-expand-all | 是否默认展开所有行,当 Table 包含展开行存在或者为树形表格时有效 | Boolean | — | false | |
expand-row-keys | 可以通过该属性设置 Table 目前的展开行,需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。 | Array | — | ||
default-sort | 默认的排序列的 prop 和顺序。它的prop 属性指定默认的排序的列,order 指定默认排序的顺序 | Object | order : ascending, descending | 如果只指定了prop , 没有指定order , 则默认顺序是ascending | |
tooltip-effect | tooltip effect 属性 | String | dark/light | dark | |
show-summary | 是否在表尾显示合计行 | Boolean | — | false | |
sum-text | 合计行第一列的文本 | String | — | 合计 | |
summary-method | 自定义的合计计算方法 | Function({ columns, data }) | — | — | |
span-method | 合并行或列的计算方法 | Function({ row, column, rowIndex, columnIndex }) | — | — | |
select-on-indeterminate | 在多选表格中,当仅有部分行被选中时,点击表头的多选框时的行为。若为 true,则选中所有行;若为 false,则取消选择所有行 | Boolean | — | true | |
indent | 展示树形数据时,树节点的缩进 | Number | — | 16 | |
lazy | 是否懒加载子节点数据 | Boolean | — | — | |
load | 加载子节点数据的函数,lazy 为 true 时生效,函数第二个参数包含了节点的层级信息 | Function(row, treeNode, resolve) | — | — | |
tree-props | 渲染嵌套数据的配置选项 | Object | — | { hasChildren: 'hasChildren', children: 'children' } | |
is-pagination | 是否显示分页 | Boolean | true;false | true | |
hide-on-single-page | 只有一页时是否隐藏 | Boolean | true;false | true | |
background | 是否为分页按钮添加背景色 | Boolean | true;false | true | |
layout | 分页组件布局 | String | sizes , prev , pager , next , jumper , -> , total , slot | sizes, prev, pager, next, jumper, ->, total, slot | |
prevText | 替代图标显示的上一页文字 | String | - | 上一页 | |
nextText | 替代图标显示的下一页文字 | String | - | 下一页 |
Table Events
事件名 | 说明 | 参数 |
---|---|---|
select | 当用户手动勾选数据行的 Checkbox 时触发的事件 | { selection, row } |
select-all | 当用户手动勾选全选 Checkbox 时触发的事件 | { selection } |
selection-change | 当选择项发生变化时会触发该事件 | { selection } |
cell-mouse-enter | 当单元格 hover 进入时会触发该事件 | { row, column, cell, event } |
cell-mouse-leave | 当单元格 hover 退出时会触发该事件 | { row, column, cell, event } |
cell-click | 当某个单元格被点击时会触发该事件 | { row, column, cell, event } |
cell-dblclick | 当某个单元格被双击击时会触发该事件 | { row, column, cell, event } |
row-click | 当某一行被点击时会触发该事件 | { row, column, event } |
row-contextmenu | 当某一行被鼠标右键点击时会触发该事件 | { row, column, event } |
row-dblclick | 当某一行被双击时会触发该事件 | { row, column, event } |
header-click | 当某一列的表头被点击时会触发该事件 | { column, event } |
header-contextmenu | 当某一列的表头被鼠标右键点击时触发该事件 | { column, event } |
sort-change | 当表格的排序条件发生变化的时候会触发该事件 | { column, prop, order } |
filter-change | 当表格的筛选条件发生变化的时候会触发该事件,参数的值是一个对象,对象的 key 是 column 的 columnKey,对应的 value 为用户选择的筛选条件的数组。 | { filters } |
current-change | 当表格的当前行发生变化的时候会触发该事件,如果要高亮当前行,请打开表格的 highlight-current-row 属性 | { currentRow, oldRow } |
header-dragend | 当拖动表头改变了列的宽度的时候会触发该事件 | { newWidth, oldWidth, column, event |
expand-change | 当用户对某一行展开或者关闭的时候会触发该事件(展开行时,回调的第二个参数为 expandedRows;树形表格时第二参数为 expanded) | { row, (expandedRows | expanded) } |
Table Methods
方法名 | 说明 | 参数 |
---|---|---|
clearSelection | 用于多选表格,清空用户的选择 | — |
toggleRowSelection | 用于多选表格,切换某一行的选中状态,如果使用了第二个参数,则是设置这一行选中与否(selected 为 true 则选中) | row, selected |
toggleAllSelection | 用于多选表格,切换所有行的选中状态 | - |
toggleRowExpansion | 用于可展开表格与树形表格,切换某一行的展开状态,如果使用了第二个参数,则是设置这一行展开与否(expanded 为 true 则展开) | row, expanded |
setCurrentRow | 用于单选表格,设定某一行为选中行,如果调用时不加参数,则会取消目前高亮行的选中状态。 | row |
clearSort | 用于清空排序条件,数据会恢复成未排序的状态 | — |
clearFilter | 不传入参数时用于清空所有过滤条件,数据会恢复成未过滤的状态,也可传入由columnKey组成的数组以清除指定列的过滤条件 | columnKey |
doLayout | 对 Table 进行重新布局。当 Table 或其祖先元素由隐藏切换为显示时,可能需要调用此方法 | — |
sort | 手动对 Table 进行排序。参数prop 属性指定排序列,order 指定排序顺序。 | prop: string, order: string |
Table Slot
name | 说明 |
---|---|
append | 插入至表格最后一行之后的内容,如果需要对表格的内容进行无限滚动操作,可能需要用到这个 slot。若表格有合计行,该 slot 会位于合计行之上。 |
Table-column Attributes
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
type | 对应列的类型。如果设置了 selection 则显示多选框;如果设置了 index 则显示该行的索引(从 1 开始计算) | string | selection/index/expand | — |
index | 如果设置了 type=index ,可以通过传递 index 属性来自定义索引 | number, Function(index) | — | — |
model | 进行格式化的数据类型 | String | amount,date | — |
format | 数据格式化后返回的模型,当model为date 时返回时间,如:yyyy-MM-dd hh:mm:ss ;当model为amount 时,可传¥ 或$ | string | — | — |
pointCount | 当model值为amount时,保留的小数点的位数,默认为2 | number | — | — |
column-key | column 的 key,如果需要使用 filter-change 事件,则需要此属性标识是哪个 column 的筛选条件 | string | — | — |
label | 显示的标题 | string | — | — |
prop | 对应列内容的字段名,也可以使用 property 属性 | string | — | — |
width | 对应列的宽度 | string | — | — |
min-width | 对应列的最小宽度,与 width 的区别是 width 是固定的,min-width 会把剩余宽度按比例分配给设置了 min-width 的列 | string | — | — |
fixed | 列是否固定在左侧或者右侧,true 表示固定在左侧 | string, boolean | true, left, right | — |
render-header | 列标题 Label 区域渲染使用的 Function | Function(h, { column, $index }) | — | — |
sortable | 对应列是否可以排序,如果设置为 'custom',则代表用户希望远程排序,需要监听 Table 的 sort-change 事件 | boolean, string | true, false, 'custom' | false |
sort-method | 对数据进行排序的时候使用的方法,仅当 sortable 设置为 true 的时候有效,需返回一个数字,和 Array.sort 表现一致 | Function(a, b) | — | — |
sort-by | 指定数据按照哪个属性进行排序,仅当 sortable 设置为 true 且没有设置 sort-method 的时候有效。如果 sort-by 为数组,则先按照第 1 个属性排序,如果第 1 个相等,再按照第 2 个排序,以此类推 | String/Array/Function(row, index) | — | — |
sort-orders | 数据在排序时所使用排序策略的轮转顺序,仅当 sortable 为 true 时有效。需传入一个数组,随着用户点击表头,该列依次按照数组中元素的顺序进行排序 | array | 数组中的元素需为以下三者之一:ascending 表示升序,descending 表示降序,null 表示还原为原始顺序 | 'ascending', 'descending', null |
resizable | 对应列是否可以通过拖动改变宽度(需要在 el-table 上设置 border 属性为真) | boolean | — | true |
formatter | 用来格式化内容 | Function({row, column, index}) | — | — |
render | 渲染表格复杂节点内容 | Function(h,{row, column, index}) | — | — |
show-overflow-tooltip | 当内容过长被隐藏时显示 tooltip | Boolean | — | false |
align | 对齐方式 | String | left/center/right | left |
header-align | 表头对齐方式,若不设置该项,则使用表格的对齐方式 | String | left/center/right | — |
class-name | 列的 className | string | — | — |
label-class-name | 当前列标题的自定义类名 | string | — | — |
selectable | 仅对 type=selection 的列有效,类型为 Function,Function 的返回值用来决定这一行的 CheckBox 是否可以勾选 | Function(row, index) | — | — |
reserve-selection | 仅对 type=selection 的列有效,类型为 Boolean,为 true 则会在数据更新之后保留之前选中的数据(需指定 row-key ) | Boolean | — | false |
filters | 数据过滤的选项,数组格式,数组中的元素需要有 text 和 value 属性。 | Array{ text, value } | — | — |
filter-placement | 过滤弹出框的定位 | String | 与 Tooltip 的 placement 属性相同 | — |
filter-multiple | 数据过滤的选项是否多选 | Boolean | — | true |
filter-method | 数据过滤使用的方法,如果是多选的筛选项,对每一条数据会执行多次,任意一次返回 true 就会显示。 | Function(value, row, column) | — | — |
filtered-value | 选中的数据过滤项,如果需要自定义表头过滤的渲染方式,可能会需要此属性。 | Array | — | — |
Table-column Scoped Slot
name | 说明 |
---|---|
column.slot | 自定义列的内容,参数为 { row, column, index } |
column.slotH4eader | 自定义表头的内容. 参数为 { column, index } |
Form 表单
基础表单
基础表单展示用法。
:::demo
<template>
<cm-form :forms="forms" />
</template>
<script>
export default {
data () {
return {
forms:[
{
formType:'input',
label:'基础input',
prop:'input',
model:'111'
}
]
}
}
}
</script>
:::
Dropdown 下拉菜单
基础用法
下拉菜单基础用法。 :::demo
<template>
<cm-dropdown :menus="menus" label="鼠标移入" @command="handleCommand"></cm-dropdown>
</template>
<script>
export default {
data () {
return {
menus: [{
value: '选项1',
label: '黄金wewq糕'
}, {
value: '选项2',
label: '双皮qwqeq奶'
}, {
value: '选项3',
label: '蚵weq仔煎'
}, {
value: '选项4',
label: '龙须weq面'
}, {
value: '选项5',
label: '北京烤qweq鸭'
}]
}
},
methods:{
handleCommand(e){
console.log(e)
}
}
}
</script>
:::
基础用法
下拉菜单基础用法。 :::demo
<template>
<cm-dropdown label="下拉slot-dropdown" @command="handleCommand">
<el-dropdown-menu slot="dropdown">
<el-dropdown-item
v-for="item in menus"
:key="item.value||item.label"
v-bind="item"
:command="item.value||item.label">
<i :class="'el-icon-'+(item.icon||'info')"></i>
{{item.label}}
</el-dropdown-item>
</el-dropdown-menu>
</cm-dropdown>
</template>
<script>
export default {
data () {
return {
menus: [{
icon:'info',
value: 'info',
label: 'el-icon-info'
},
{
icon:'question',
value: 'question',
label: 'el-icon-question'
},
{
icon:'remove',
value: 'remove',
label: 'el-icon-remove'
},
{
icon:'circle-plus',
value: 'circle-plus',
label: 'el-icon-circle-plus'
},
{
icon:'success',
value: 'success',
label: 'el-icon-success'
},
{
icon:'error',
value: 'error',
label: 'el-icon-error'
},
]
}
},
methods:{
handleCommand(e){
console.log(e)
}
}
}
</script>
:::
Dropdown Attributes
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
label | 按钮名称 | String | — | — |
menus | 下拉菜单子项数组 | Array | — | [] |
type | 菜单按钮类型,同 Button 组件(只在split-button 为 true 的情况下有效) | string | — | — |
size | 菜单尺寸,在split-button 为 true 的情况下也对触发按钮生效 | string | medium / small / mini | — |
split-button | 下拉触发元素呈现为按钮组 | boolean | — | false |
placement | 菜单弹出位置 | string | top/top-start/top-end/bottom/bottom-start/bottom-end | bottom-end |
trigger | 触发下拉的行为 | string | hover, click | hover |
hide-on-click | 是否在点击菜单项后隐藏菜单 | boolean | — | true |
show-timeout | 展开下拉菜单的延时(仅在 trigger 为 hover 时有效) | number | — | 250 |
hide-timeout | 收起下拉菜单的延时(仅在 trigger 为 hover 时有效) | number | — | 150 |
tabindex | Dropdown 组件的 tabindex | number | — | 0 |
Dropdown Slots
Name | 说明 |
---|---|
— | 触发下拉列表显示的元素。 注意: 必须是一个元素或者或者组件 |
dropdown | 下拉列表,通常是 <el-dropdown-menu> 组件 |
Dropdown Events
事件名称 | 说明 | 回调参数 |
---|---|---|
click | split-button 为 true 时,点击左侧按钮的回调 | — |
command | 点击菜单项触发的事件回调 | dropdown-item 的指令 |
visible-change | 下拉框出现/隐藏时触发 | 出现则为 true,隐藏则为 false |
Dropdown Menu Item Attributes
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
command | 指令 | string/number/object | — | — |
disabled | 禁用 | boolean | — | false |
divided | 显示分割线 | boolean | — | false |
icon | 图标类名 | string | — | — |
Dialog 弹框
基础弹框
基础弹框用法。
:::demo
<template>
<div>
<cm-button @click="showDialog=true">打开弹框</cm-button>
<cm-dialog title="基础弹框" :visible.sync="showDialog" @confirm="showDialog=false">
我是弹框内容
</cm-dialog>
</div>
</template>
<script>
export default {
data () {
return {
showDialog:false
}
}
}
</script>
:::
title(slot)用法
弹框自定义title(slot)用法。
:::demo
<template>
<div>
<cm-button @click="showDialog=true">自定义title弹框</cm-button>
<cm-dialog :visible.sync="showDialog">
<div class="title" slot="title">自定义title</div>
我是弹框内容
</cm-dialog>
</div>
</template>
<script>
export default {
data () {
return {
showDialog:false
}
}
}
</script>
:::
footer(slot)用法
弹框自定义footer(slot)用法。
:::demo
<template>
<div>
<cm-button @click="showDialog=true">自定义footer弹框</cm-button>
<cm-dialog title="自定义footer" :visible.sync="showDialog" :plain="true" :center="true">
我是弹框内容
<div slot="footer">
<cm-button @click="showDialog=false">取消</cm-button>
<cm-button type="primary" @click="showDialog=false">确认</cm-button>
</div>
</cm-dialog>
</div>
</template>
<script>
export default {
data () {
return {
showDialog:false
}
}
}
</script>
:::
弹框事件
:::demo
<template>
<div>
<cm-button @click="showDialog=true">自定义footer弹框</cm-button>
<cm-dialog title="自定义footer"
:visible.sync="showDialog"
@open="openDialog"
@opened="openedDialog"
@close="closeDialog"
@closed="closedDialog">
我是弹框内容
</cm-dialog>
</div>
</template>
<script>
export default {
data () {
return {
showDialog:false
}
},
methods:{
openDialog(){
this.$message.success('Dialog 打开的回调')
},
openedDialog(){
this.$message.success('Dialog 打开动画结束时的回调')
},
closeDialog(){
this.$message.success(' Dialog关闭的回调')
this.showDialog=false
},
closedDialog(){
this.$message.success('Dialog 关闭动画结束时的回调')
}
}
}
</script>
:::