Skip to content

Commit d05d6b6

Browse files
jonfunkhouserpi0
authored andcommitted
feat(table): support custom classes per table cell in a column
1 parent 91c7257 commit d05d6b6

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

src/components/table/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ The following field properties are recognized:
275275
| `class` | String or Array | Class name (or array of class names) to add to `<th>` **and** `<td>` in the column.
276276
| `formatter` | String or Function | A formatter callback function, can be used instead of (or in conjunction with) slots for real table fields (i.e. fields, that have corresponding data at items array). Refer to [**Custom Data Rendering**](#custom-data-rendering) for more details.
277277
| `sortable` | Boolean | Enable sorting on this column. Refer to the [**Sorting**](#sorting) Section for more details.
278-
| `tdClass` | String or Array | Class name (or array of class names) to add to `<tbody>` data `<td>` cells in the column.
278+
| `tdClass` | String or Array or Function | Class name (or array of class names) to add to `<tbody>` data `<td>` cells in the column. If custom classes per cell are required, a callback function can be specified instead.
279279
| `thClass` | String or Array | Class name (or array of class names) to add to `<thead>`/`<tfoot>` heading `<th>` cell.
280280
| `thStyle` | Object | JavaScript object representing CSS styles you would like to apply to the table `<thead>`/`<tfoot>` field `<th>`.
281281
| `variant` | String | Apply contextual class to all the `<th>` **and** `<td>` in the column - `active`, `success`, `info`, `warning`, `danger` (these variants map to classes `thead-${variant}`, `table-${variant}`, or `bg-${variant}` accordingly).

src/components/table/fixtures/table.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@ window.app = new Vue({
44
fields: {
55
name: {
66
label: 'Person Full name',
7-
sortable: true
7+
sortable: true,
8+
tdClass: 'bg-primary'
89
},
910
age: {
1011
label: 'Person age',
1112
sortable: true,
12-
formatter: 'formatAge'
13+
formatter: 'formatAge',
14+
tdClass: ['bg-primary', 'text-dark']
1315
},
1416
isActive: {
15-
label: 'is Active'
17+
label: 'is Active',
18+
tdClass: (value, key, item) => { return 'bg-danger' }
1619
},
1720
actions: {
18-
label: 'Actions'
21+
label: 'Actions',
22+
tdClass: 'formatCell'
1923
}
2024
},
2125
currentPage: 1,
@@ -131,6 +135,9 @@ window.app = new Vue({
131135
},
132136
formatAge (value) {
133137
return `${value} years old`
138+
},
139+
formatCell (value, key, item) {
140+
return ['bg-primary', 'text-light']
134141
}
135142
}
136143
})

src/components/table/table.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ export default {
864864
]
865865
},
866866
tdClasses (field, item) {
867+
const t = this
867868
let cellVariant = ''
868869
if (item._cellVariants && item._cellVariants[field.key]) {
869870
cellVariant = `${this.dark ? 'bg' : 'table'}-${
@@ -876,7 +877,7 @@ export default {
876877
: '',
877878
cellVariant,
878879
field.class ? field.class : '',
879-
field.tdClass ? field.tdClass : ''
880+
t.getTdClasses(item, field)
880881
]
881882
},
882883
rowClasses (item) {
@@ -981,6 +982,22 @@ export default {
981982
this._providerSetLocal(data)
982983
}
983984
},
985+
getTdClasses (item, field) {
986+
const key = field.key
987+
const tdClass = field.tdClass
988+
const parent = this.$parent
989+
if (tdClass) {
990+
if (typeof tdClass === 'function') {
991+
let value = get(item, key)
992+
return tdClass(value, key, item)
993+
} else if (typeof tdClass === 'string' && typeof parent[tdClass] === 'function') {
994+
let value = get(item, key)
995+
return parent[tdClass](value, key, item)
996+
}
997+
return tdClass
998+
}
999+
return ''
1000+
},
9841001
getFormattedValue (item, field) {
9851002
const key = field.key
9861003
const formatter = field.formatter

src/components/table/table.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,4 +699,41 @@ describe('table', async () => {
699699
'b-table-stacked'
700700
])
701701
})
702+
703+
it('all example tables should have custom formatted cells', async () => {
704+
const { app: { $refs } } = window
705+
706+
const tables = ['table_basic', 'table_paginated', 'table_dark']
707+
await nextTick()
708+
709+
tables.forEach((table, idx) => {
710+
const vm = $refs[table]
711+
const tbody = [...vm.$el.children].find(
712+
el => el && el.tagName === 'TBODY'
713+
)
714+
expect(tbody).toBeDefined()
715+
if (tbody) {
716+
const tr = tbody.children[0]
717+
expect(tr).toBeDefined()
718+
expect(Boolean(tr.children[0]) &&
719+
Boolean(tr.children[0].classList) &&
720+
tr.children[0].classList.contains('bg-primary'))
721+
.toBe(true)
722+
expect(Boolean(tr.children[1]) &&
723+
Boolean(tr.children[1].classList) &&
724+
tr.children[1].classList.contains('bg-primary') &&
725+
tr.children[1].classList.contains('text-dark'))
726+
.toBe(true)
727+
expect(Boolean(tr.children[2]) &&
728+
Boolean(tr.children[2].classList) &&
729+
tr.children[2].classList.contains('bg-danger'))
730+
.toBe(true)
731+
expect(Boolean(tr.children[3]) &&
732+
Boolean(tr.children[3].classList) &&
733+
tr.children[3].classList.contains('bg-primary') &&
734+
tr.children[3].classList.contains('text-light'))
735+
.toBe(true)
736+
}
737+
})
738+
})
702739
})

0 commit comments

Comments
 (0)