Skip to content

Commit

Permalink
feat(Table): add ability to custom style for td and tr (#741)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
  • Loading branch information
Haythamasalama and benjamincanac committed Sep 28, 2023
1 parent 8b7a013 commit 874447c
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
40 changes: 40 additions & 0 deletions docs/components/content/examples/TableExampleStyle.vue
Original file line number Diff line number Diff line change
@@ -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>
59 changes: 59 additions & 0 deletions docs/content/4.data/1.table.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

You can use slots to customize the header and data cells of the table.
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/components/data/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@
</tr>

<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">
<UCheckbox v-model="selected" :value="row" aria-label="Select row" @click.stop />
</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)">
{{ getRowData(row, column.key) }}
</slot>
Expand Down

1 comment on commit 874447c

@vercel
Copy link

@vercel vercel bot commented on 874447c Sep 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

ui – ./

ui-nuxt-js.vercel.app
ui.nuxt.com
ui-git-dev-nuxt-js.vercel.app

Please sign in to comment.