diff --git a/docs/app/components/content/examples/table/TableRowSelectionEventExample.vue b/docs/app/components/content/examples/table/TableRowSelectionEventExample.vue new file mode 100644 index 0000000000..41d5c4841a --- /dev/null +++ b/docs/app/components/content/examples/table/TableRowSelectionEventExample.vue @@ -0,0 +1,141 @@ + + + diff --git a/docs/content/3.components/table.md b/docs/content/3.components/table.md index 98f972b510..aac9dd5fc1 100644 --- a/docs/content/3.components/table.md +++ b/docs/content/3.components/table.md @@ -267,6 +267,24 @@ class: '!p-0' ::tip You can use the `row-selection` prop to control the selection state of the rows (can be binded with `v-model`). :: +### With @select event + +You can also add a select listener on your Table to make the rows clickable. The function will receive the TableRow as the first argument and second argument optional the event. + +You can use this to navigate to a page, open a modal or even to select the row manually. + + +::component-example +--- +prettier: true +collapse: true +name: 'table-row-selection-event-example' +highlights: + - 55 + - 70 +class: '!p-0' +--- +:: ### With column sorting diff --git a/src/runtime/components/Table.vue b/src/runtime/components/Table.vue index 809dfc4119..d0298f224f 100644 --- a/src/runtime/components/Table.vue +++ b/src/runtime/components/Table.vue @@ -44,6 +44,7 @@ const table = tv({ extend: tv(theme), ...(appConfig.ui?.table || {}) }) type TableVariants = VariantProps export type TableColumn = ColumnDef +export type TableRow = Row export interface TableData { [key: string]: any @@ -58,6 +59,7 @@ export interface TableProps { * @defaultValue false */ sticky?: boolean + onSelect?: (row: TableRow, e?: Event) => void /** Whether the table should be in loading state. */ loading?: boolean loadingColor?: TableVariants['loadingColor'] @@ -197,6 +199,13 @@ function valueUpdater>(updaterOrValue: T, ref: Ref) { ref.value = typeof updaterOrValue === 'function' ? updaterOrValue(ref.value) : updaterOrValue } +function handleRowSelect(row: TableRow, e: Event) { + if (!props.onSelect) + return + e.preventDefault() + e.stopPropagation() + props.onSelect(row, e) +} defineExpose({ tableApi }) @@ -229,7 +238,7 @@ defineExpose({