From c211f71778df133e4214eeccd5807611d9b87bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=81=8F?= <admin@aresn.com> Date: Tue, 10 Jan 2017 18:36:13 +0800 Subject: [PATCH] Table data support to set cell className Table data support to set cell className --- src/components/table/mixin.js | 9 +++++-- src/components/table/table-body.vue | 2 +- test/routers/table.vue | 38 +++++++++++++++++++++++------ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/components/table/mixin.js b/src/components/table/mixin.js index 02ed257b..71c4b2cd 100644 --- a/src/components/table/mixin.js +++ b/src/components/table/mixin.js @@ -1,9 +1,14 @@ export default { methods: { - alignCls (column, type) { + alignCls (column, type, row = {}) { + let cellClassName = ''; + if (row.cellClassName && column.key && row.cellClassName[column.key]) { + cellClassName = row.cellClassName[column.key]; + } return [ { - [`${column.className}`]: column.className && type === 'body', + [`${cellClassName}`]: cellClassName, // cell className + [`${column.className}`]: column.className && type === 'body', // column className [`${this.prefixCls}-column-${column.align}`]: column.align, [`${this.prefixCls}-hidden`]: (this.fixed === 'left' && column.fixed !== 'left') || (this.fixed === 'right' && column.fixed !== 'right') || (!this.fixed && column.fixed && (column.fixed === 'left' || column.fixed === 'right')) } diff --git a/src/components/table/table-body.vue b/src/components/table/table-body.vue index 287ba5ef..23effc75 100644 --- a/src/components/table/table-body.vue +++ b/src/components/table/table-body.vue @@ -11,7 +11,7 @@ @mouseleave.stop="handleMouseOut(row._index)" @click.stop="clickCurrentRow(row._index)" @dblclick.stop="dblclickCurrentRow(row._index)"> - <td v-for="column in columns" :class="alignCls(column, 'body')"> + <td v-for="column in columns" :class="alignCls(column, 'body', row)"> <Cell :fixed="fixed" :prefix-cls="prefixCls" diff --git a/test/routers/table.vue b/test/routers/table.vue index 45666a71..6139dadb 100644 --- a/test/routers/table.vue +++ b/test/routers/table.vue @@ -16,6 +16,18 @@ .ivu-table .table-address-col{ background: #187; } + .ivu-table .class-cell-age{ + background: #187; + color: #fff; + } + .ivu-table .class-cell-address{ + background: #f60; + color: #fff; + } + .ivu-table .class-cell-name{ + background: #2db7f5; + color: #fff; + } </style> <template> <i-table :row-class-name="rowClassName" :columns="columns1" :data="data1"></i-table> @@ -25,29 +37,38 @@ data () { return { columns1: [ + { + type: 'selection', + width: 60, + align: 'center' + }, { title: '姓名', key: 'name', - fixed: 'right', - className: 'table-name-col' +// fixed: 'right', +// className: 'table-name-col' }, { title: '年龄', key: 'age', - fixed: 'right', - className: 'table-age-col' +// fixed: 'right', +// className: 'table-age-col' }, { title: '地址', key: 'address', - className: 'table-address-col' +// className: 'table-address-col' } ], data1: [ { name: '王小明', age: 18, - address: '北京市朝阳区芍药居' + address: '北京市朝阳区芍药居', + cellClassName: { + age: 'class-cell-age', + address: 'class-cell-address' + } }, { name: '张小刚', @@ -57,7 +78,10 @@ { name: '李小红', age: 30, - address: '上海市浦东新区世纪大道' + address: '上海市浦东新区世纪大道', + cellClassName: { + name: 'class-cell-name' + } }, { name: '周小伟',