Skip to content

Commit 35b3304

Browse files
feat(VTable): add striped prop (#19735)
1 parent 092e64a commit 35b3304

File tree

6 files changed

+156
-1
lines changed

6 files changed

+156
-1
lines changed

packages/api-generator/src/locale/en/VTable.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"fixedFooter": "Use the fixed-footer prop together with the height prop to fix the footer to the bottom of the table.",
44
"fixedHeader": "Use the fixed-header prop together with the height prop to fix the header to the top of the table.",
55
"height": "Use the height prop to set the height of the table.",
6-
"hover": "Will add a hover effect to a table's row when the mouse is over it."
6+
"hover": "Will add a hover effect to a table's row when the mouse is over it.",
7+
"striped": "Applies a background to either **even** or **odd** rows."
78
},
89
"slots": {
910
"bottom": "Slot to add content below the table.",
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<template>
2+
<v-table striped="even">
3+
<thead>
4+
<tr>
5+
<th class="text-left">
6+
Name
7+
</th>
8+
<th class="text-left">
9+
Calories
10+
</th>
11+
</tr>
12+
</thead>
13+
<tbody>
14+
<tr
15+
v-for="item in desserts"
16+
:key="item.name"
17+
>
18+
<td>{{ item.name }}</td>
19+
<td>{{ item.calories }}</td>
20+
</tr>
21+
</tbody>
22+
</v-table>
23+
</template>
24+
25+
<script setup>
26+
import { ref } from 'vue'
27+
28+
const desserts = ref([
29+
{
30+
name: 'Frozen Yogurt',
31+
calories: 159,
32+
},
33+
{
34+
name: 'Ice cream sandwich',
35+
calories: 237,
36+
},
37+
{
38+
name: 'Eclair',
39+
calories: 262,
40+
},
41+
{
42+
name: 'Cupcake',
43+
calories: 305,
44+
},
45+
{
46+
name: 'Gingerbread',
47+
calories: 356,
48+
},
49+
{
50+
name: 'Jelly bean',
51+
calories: 375,
52+
},
53+
{
54+
name: 'Lollipop',
55+
calories: 392,
56+
},
57+
{
58+
name: 'Honeycomb',
59+
calories: 408,
60+
},
61+
{
62+
name: 'Donut',
63+
calories: 452,
64+
},
65+
{
66+
name: 'KitKat',
67+
calories: 518,
68+
},
69+
])
70+
</script>
71+
72+
<script>
73+
export default {
74+
data () {
75+
return {
76+
desserts: [
77+
{
78+
name: 'Frozen Yogurt',
79+
calories: 159,
80+
},
81+
{
82+
name: 'Ice cream sandwich',
83+
calories: 237,
84+
},
85+
{
86+
name: 'Eclair',
87+
calories: 262,
88+
},
89+
{
90+
name: 'Cupcake',
91+
calories: 305,
92+
},
93+
{
94+
name: 'Gingerbread',
95+
calories: 356,
96+
},
97+
{
98+
name: 'Jelly bean',
99+
calories: 375,
100+
},
101+
{
102+
name: 'Lollipop',
103+
calories: 392,
104+
},
105+
{
106+
name: 'Honeycomb',
107+
calories: 408,
108+
},
109+
{
110+
name: 'Donut',
111+
calories: 452,
112+
},
113+
{
114+
name: 'KitKat',
115+
calories: 518,
116+
},
117+
],
118+
}
119+
},
120+
}
121+
</script>

packages/docs/src/pages/en/components/tables.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ Use the **height** prop to set the height of the table.
6666
Use the **fixed-header** prop together with the **height** prop to fix the header to the top of the table.
6767

6868
<ExamplesExample file="v-table/prop-fixed-header" />
69+
70+
#### Striped
71+
72+
By applying the **striped** prop, you can have a background applied to either the **even** or **odd** rows of the table. Color can be further adjusted using sass variables.
73+
74+
<ExamplesExample file="v-table/prop-striped" />

packages/vuetify/src/components/VTable/VTable.sass

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@
4848
background: $table-hover-color
4949
pointer-events: none
5050
@include tools.absolute(true)
51+
52+
&.v-table--striped-even
53+
> .v-table__wrapper
54+
> table
55+
> tbody
56+
> tr:nth-child(even)
57+
background-color: $table-stripe-color
58+
59+
&.v-table--striped-odd
60+
> .v-table__wrapper
61+
> table
62+
> tbody
63+
> tr:nth-child(odd)
64+
background-color: $table-stripe-color
5165

5266
&.v-table--fixed-header
5367
> .v-table__wrapper

packages/vuetify/src/components/VTable/VTable.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,28 @@ import { makeThemeProps, provideTheme } from '@/composables/theme'
1010
// Utilities
1111
import { convertToUnit, genericComponent, propsFactory, useRender } from '@/util'
1212

13+
// Types
14+
import type { PropType } from 'vue'
15+
1316
export type VTableSlots = {
1417
default: never
1518
top: never
1619
bottom: never
1720
wrapper: never
1821
}
1922

23+
export type Striped = null | 'odd' | 'even'
24+
2025
export const makeVTableProps = propsFactory({
2126
fixedHeader: Boolean,
2227
fixedFooter: Boolean,
2328
height: [Number, String],
2429
hover: Boolean,
30+
striped: {
31+
type: String as PropType<Striped>,
32+
default: null,
33+
validator: (v: any) => ['even', 'odd'].includes(v),
34+
},
2535

2636
...makeComponentProps(),
2737
...makeDensityProps(),
@@ -49,6 +59,8 @@ export const VTable = genericComponent<VTableSlots>()({
4959
'v-table--has-top': !!slots.top,
5060
'v-table--has-bottom': !!slots.bottom,
5161
'v-table--hover': props.hover,
62+
'v-table--striped-even': props.striped === 'even',
63+
'v-table--striped-odd': props.striped === 'odd',
5264
},
5365
themeClasses.value,
5466
densityClasses.value,

packages/vuetify/src/components/VTable/_variables.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ $table-border: thin solid $table-border-color !default;
1818
$table-hover-color: rgba(var(--v-border-color), var(--v-hover-opacity)) !default;
1919
$table-line-height: 1.5 !default;
2020
$table-column-padding: 0 16px !default;
21+
$table-stripe-color: rgba(var(--v-border-color), var(--v-hover-opacity)) !default;
2122
$table-transition-duration: 0.28s !default;
2223
$table-transition-property: box-shadow, opacity, background, height !default;
2324
$table-transition-timing-function: settings.$standard-easing !default;

0 commit comments

Comments
 (0)