Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Table): expand row #1036

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/components/content/examples/TableExampleExpandable.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<script setup>
const people = [{
id: 1,
name: 'Lindsay Walton',
title: 'Front-end Developer',
email: 'lindsay.walton@example.com',
role: 'Member'
}, {
id: 2,
name: 'Courtney Henry',
title: 'Designer',
email: 'courtney.henry@example.com',
role: 'Admin'
}, {
id: 3,
name: 'Tom Cook',
title: 'Director of Product',
email: 'tom.cook@example.com',
role: 'Member'
}, {
id: 4,
name: 'Whitney Francis',
title: 'Copywriter',
email: 'whitney.francis@example.com',
role: 'Admin'
}, {
id: 5,
name: 'Leonard Krasner',
title: 'Senior Designer',
email: 'leonard.krasner@example.com',
role: 'Owner'
}, {
id: 6,
name: 'Floyd Miles',
title: 'Principal Designer',
email: 'floyd.miles@example.com',
role: 'Member'
}]
</script>

<template>
<UTable :rows="people">
<template #expand="{ row }">
<Placeholder class="h-36" />
</template>
</UTable>
</template>
14 changes: 14 additions & 0 deletions docs/content/4.data/1.table.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,20 @@ componentProps:
---
::

### Expandable

You can use the `expand` slot to display extra information about a row. You will have access to the `row` property in the slot scope.

::component-example{class="grid"}
---
padding: false
component: 'table-example-expandable'
componentProps:
class: 'flex-1'
---
::


### Loading

Use the `loading` prop to display a loading state.
Expand Down
67 changes: 55 additions & 12 deletions src/runtime/components/data/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<UCheckbox :checked="indeterminate || selected.length === rows.length" :indeterminate="indeterminate" aria-label="Select all" @change="onChange" />
</th>

<th v-if="$slots.expand" scope="col" :class="ui.tr.base">
<span class="sr-only">Expand</span>
</th>

<th v-for="(column, index) in columns" :key="index" scope="col" :class="[ui.th.base, ui.th.padding, ui.th.color, ui.th.font, ui.th.size, column.class]">
<slot :name="`${column.key}-header`" :column="column" :sort="sort" :on-sort="onSort">
<UButton
Expand Down Expand Up @@ -49,17 +53,42 @@
</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, 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, 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>
</td>
</tr>
<template v-for="(row, index) in rows" :key="index">
<tr :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-if="$slots.expand"
:class="[ui.td.base, ui.td.padding, ui.td.color, ui.td.font, ui.td.size]"
>
<UButton
color="gray"
variant="ghost"
:padded="false"
size="2xs"
:icon="openedRows.includes(index) ? 'i-heroicons-chevron-up' : 'i-heroicons-chevron-down'"
@click="toggleOpened(index)"
/>
</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, 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>
</td>
</tr>

Choose a reason for hiding this comment

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

Suggested change
</tr>
</tr>
<slot :name="after-row" :row="row" :index="index" is-opened="openedRows.includes(index)" />

or so, to add some more flexibility
for example: dynamically loaded content that renders subsequent table rows while maintaining the width of the original table's columns

currently we have only 'expand' slot which is content of single cell with 100% table width + don't have access to style that tr + td element

<tr v-if="openedRows.includes(index)">
<td colspan="100%">
<slot
name="expand"
:row="row"
:index="index"
/>
</td>
</tr>
</template>
</template>
</tbody>
</table>
Expand Down Expand Up @@ -177,8 +206,12 @@ export default defineComponent({

const sort = useVModel(props, 'sort', emit, { passive: true, defaultValue: defu({}, props.sort, { column: null, direction: 'asc' }) })

const openedRows = ref([])

const defaultSort = { column: sort.value.column, direction: null }
const savedSort = { column: sort.value.column, direction: null }


const rows = computed(() => {
if (!sort.value?.column || props.sortMode === 'manual') {
return props.rows
Expand Down Expand Up @@ -280,6 +313,14 @@ export default defineComponent({
return get(row, rowKey, defaultValue)
}

function toggleOpened (index: number) {
if (openedRows.value.includes(index)) {
openedRows.value = openedRows.value.filter((i) => i !== index)
} else {
openedRows.value.push(index)
}
}

return {
// eslint-disable-next-line vue/no-dupe-keys
ui,
Expand All @@ -296,11 +337,13 @@ export default defineComponent({
emptyState,
// eslint-disable-next-line vue/no-dupe-keys
loadingState,
openedRows,
isSelected,
onSort,
onSelect,
onChange,
getRowData
getRowData,
toggleOpened
}
}
})
Expand Down