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): emit expand event #2063

Open
wants to merge 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/content/2.components/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,29 @@ componentProps:
---
::

#### Custom `expand` event
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#### Custom `expand` event
#### Reactive expand


The `@update:expand` event is emitted when a row is expanded. This event provides the current state of expanded rows and the data of the row that triggered the event.

To use the `@update:expand` event, add it to your `UTable` component. The event handler will receive an object with the following properties:
- `expandedRows`: An array of indices of the currently expanded rows.
- `row`: The row data that triggered the expand/collapse action.

```vue
<script setup lang="ts">
const { data, pending } = await useLazyFetch(() => `/api/users?orderBy=${sort.value.column}&order=${sort.value.direction}`)

const handleExpand = ({ expandedRows, row }) => {
console.log('Expanded Rows:', expandedRows);
console.log('Row Data:', row);
};
</script>

<template>
<UTable :loading="pending" :rows="data" @update:expand="handleExpand" />
</template>
```

### Loading

Use the `loading` prop to indicate that data is currently loading with an indeterminate [Progress](/components/progress#indeterminate) bar.
Expand Down
13 changes: 9 additions & 4 deletions src/runtime/components/data/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<UButton
v-bind="{ ...(ui.default.expandButton || {}), ...expandButton }"
:ui="{ icon: { base: [ui.expand.icon, openedRows.includes(index) && 'rotate-180'] } }"
@click="toggleOpened(index)"
@click="toggleOpened({ index, row })"
/>
</td>

Expand Down Expand Up @@ -154,6 +154,10 @@ interface Column {
[key: string]: any
}

interface Row {
[key: string]: any
}

export default defineComponent({
components: {
UIcon,
Expand All @@ -172,7 +176,7 @@ export default defineComponent({
default: () => defaultComparator
},
rows: {
type: Array as PropType<{ [key: string]: any }[]>,
type: Array as PropType<Row[]>,
default: () => []
},
columns: {
Expand Down Expand Up @@ -236,7 +240,7 @@ export default defineComponent({
default: () => ({})
}
},
emits: ['update:modelValue', 'update:sort'],
emits: ['update:modelValue', 'update:sort', 'update:expand'],
setup (props, { emit, attrs: $attrs }) {
const { ui, attrs } = useUI('table', toRef(props, 'ui'), config, toRef(props, 'class'))

Expand Down Expand Up @@ -349,11 +353,12 @@ export default defineComponent({
return get(row, rowKey, defaultValue)
}

function toggleOpened (index: number) {
function toggleOpened ({ index, row }: { index: number, row: Row }) {
if (openedRows.value.includes(index)) {
openedRows.value = openedRows.value.filter((i) => i !== index)
} else {
openedRows.value.push(index)
emit('update:expand', { expandedRows: openedRows.value, row })
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be emitted in both case?

Copy link
Author

Choose a reason for hiding this comment

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

Should it? I think it could be either emitted in both cases and the information whether it was an "expand" or "collapse" could be added as a function parameter to the user or there could be another emit like update:collapse. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Since you send the openedRows in the event, we should emit each time the openedRows.value changes I guess.

}
}

Expand Down