mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-02-03 05:37:56 +01:00
feat(Table): add ability to custom style for td and tr (#741)
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
committed by
GitHub
parent
8b7a013319
commit
874447cb41
40
docs/components/content/examples/TableExampleStyle.vue
Normal file
40
docs/components/content/examples/TableExampleStyle.vue
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<script setup>
|
||||||
|
const columns = [{
|
||||||
|
key: 'id',
|
||||||
|
label: '#'
|
||||||
|
}, {
|
||||||
|
key: 'quantity',
|
||||||
|
label: 'Quantity',
|
||||||
|
class: 'italic'
|
||||||
|
}, {
|
||||||
|
key: 'name',
|
||||||
|
label: 'Name'
|
||||||
|
}]
|
||||||
|
|
||||||
|
const items = [{
|
||||||
|
id: 1,
|
||||||
|
name: 'Apple',
|
||||||
|
quantity: { value: 100, class: 'bg-green-500/50 dark:bg-green-400/50' }
|
||||||
|
}, {
|
||||||
|
id: 2,
|
||||||
|
name: 'Orange',
|
||||||
|
quantity: { value: 0 },
|
||||||
|
class: 'bg-red-500/50 dark:bg-red-400/50 animate-pulse'
|
||||||
|
}, {
|
||||||
|
id: 3,
|
||||||
|
name: 'Banana',
|
||||||
|
quantity: { value: 30, class: 'bg-green-500/50 dark:bg-green-400/50' }
|
||||||
|
}, {
|
||||||
|
id: 4,
|
||||||
|
name: 'Mango',
|
||||||
|
quantity: { value: 5, class: 'bg-green-500/50 dark:bg-green-400/50' }
|
||||||
|
}]
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<UTable :rows="items" :columns="columns">
|
||||||
|
<template #quantity-data="{ row }">
|
||||||
|
{{ row.quantity.value }}
|
||||||
|
</template>
|
||||||
|
</UTable>
|
||||||
|
</template>
|
||||||
@@ -524,6 +524,65 @@ excludedProps:
|
|||||||
---
|
---
|
||||||
::
|
::
|
||||||
|
|
||||||
|
## Styling
|
||||||
|
|
||||||
|
You can apply styles to `tr` and `td` elements by passing a `class` to rows.
|
||||||
|
|
||||||
|
Also, you can apply styles to `th` elements by passing a `class` to columns.
|
||||||
|
|
||||||
|
::component-example
|
||||||
|
---
|
||||||
|
padding: false
|
||||||
|
---
|
||||||
|
|
||||||
|
#default
|
||||||
|
:table-example-style{class="w-full"}
|
||||||
|
|
||||||
|
#code
|
||||||
|
```vue
|
||||||
|
<script setup>
|
||||||
|
const columns = [{
|
||||||
|
key: 'id',
|
||||||
|
label: '#'
|
||||||
|
}, {
|
||||||
|
key: 'quantity',
|
||||||
|
label: 'Quantity',
|
||||||
|
class: 'italic' // Apply style to column header
|
||||||
|
}, {
|
||||||
|
key: 'name',
|
||||||
|
label: 'Name'
|
||||||
|
}]
|
||||||
|
|
||||||
|
const items = [{
|
||||||
|
id: 1,
|
||||||
|
name: 'Apple',
|
||||||
|
quantity: { value: 100, class: 'bg-green-500/50 dark:bg-green-400/50' } // Apply style to td
|
||||||
|
}, {
|
||||||
|
id: 2,
|
||||||
|
name: 'Orange',
|
||||||
|
quantity: { value: 0 },
|
||||||
|
class: 'bg-red-500/50 dark:bg-red-400/50 animate-pulse' // Apply style to tr
|
||||||
|
}, {
|
||||||
|
id: 3,
|
||||||
|
name: 'Banana',
|
||||||
|
quantity: { value: 30, class: 'bg-green-500/50 dark:bg-green-400/50' }
|
||||||
|
}, {
|
||||||
|
id: 4,
|
||||||
|
name: 'Mango',
|
||||||
|
quantity: { value: 5, class: 'bg-green-500/50 dark:bg-green-400/50' }
|
||||||
|
}]
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<UTable :rows="items" :columns="columns">
|
||||||
|
<template #quantity-data="{ row }">
|
||||||
|
{{ row.quantity.value }}
|
||||||
|
</template>
|
||||||
|
</UTable>
|
||||||
|
</template>
|
||||||
|
```
|
||||||
|
::
|
||||||
|
|
||||||
## Slots
|
## Slots
|
||||||
|
|
||||||
You can use slots to customize the header and data cells of the table.
|
You can use slots to customize the header and data cells of the table.
|
||||||
|
|||||||
@@ -49,12 +49,12 @@
|
|||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<tr v-for="(row, index) in rows" :key="index" :class="[ui.tr.base, isSelected(row) && ui.tr.selected, $attrs.onSelect && ui.tr.active]" @click="() => onSelect(row)">
|
<tr v-for="(row, index) in rows" :key="index" :class="[ui.tr.base, isSelected(row) && ui.tr.selected, $attrs.onSelect && ui.tr.active, row?.class]" @click="() => onSelect(row)">
|
||||||
<td v-if="modelValue" :class="ui.checkbox.padding">
|
<td v-if="modelValue" :class="ui.checkbox.padding">
|
||||||
<UCheckbox v-model="selected" :value="row" aria-label="Select row" @click.stop />
|
<UCheckbox v-model="selected" :value="row" aria-label="Select row" @click.stop />
|
||||||
</td>
|
</td>
|
||||||
|
|
||||||
<td v-for="(column, subIndex) in columns" :key="subIndex" :class="[ui.td.base, ui.td.padding, ui.td.color, ui.td.font, ui.td.size]">
|
<td v-for="(column, subIndex) in columns" :key="subIndex" :class="[ui.td.base, ui.td.padding, ui.td.color, ui.td.font, ui.td.size, row[column.key]?.class]">
|
||||||
<slot :name="`${column.key}-data`" :column="column" :row="row" :index="index" :get-row-data="(defaultValue) => getRowData(row, column.key, defaultValue)">
|
<slot :name="`${column.key}-data`" :column="column" :row="row" :index="index" :get-row-data="(defaultValue) => getRowData(row, column.key, defaultValue)">
|
||||||
{{ getRowData(row, column.key) }}
|
{{ getRowData(row, column.key) }}
|
||||||
</slot>
|
</slot>
|
||||||
|
|||||||
Reference in New Issue
Block a user