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): add v-model:sort prop #803

Merged
merged 16 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 31 additions & 1 deletion docs/content/4.data/1.table.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ componentProps:
---
::

You can specify the default direction of each column through the `direction` property. It can be either `asc` or `desc` and defaults to `asc`.
You may specify the default direction of each column through the `direction` property. It can be either `asc` or `desc`, but it will default to `asc`.

You can specify a default sort for the table through the `sort` prop. It's an object with the following properties:

Expand Down Expand Up @@ -156,6 +156,36 @@ Use the `sort-desc-icon` prop to set a different icon or change it globally in `
You can also customize the entire header cell, read more in the [Slots](#slots) section.
::

You can hear any changes on sorting using the `onSort` event, which fires with the `column` and `direction` of the sorting. :u-badge{label="New" class="!rounded-full" variant="subtle"}

For example, we can take advantage of `useLazyRefresh` computed URL to automatically refresh the sorting column and direction every time the `order` reactive element changes.

```vue
<script setup>
const order = reactive({
column: null,
direction: 'asc'
})

const columns = [
// ...
]

const { data, pending } = useLazyFetch(() => {
return `/api/users?orderBy=${order.column}&order=${order.direction}`
})
</script>

<template>
<UTable
:loading="pending"
:columns="columns"
:rows="data"
@onSort="value => Object.assign(order, value)"
DarkGhostHunter marked this conversation as resolved.
Show resolved Hide resolved
/>
</template>
```

### Selectable

Use a `v-model` to make the table selectable. The `v-model` will be an array of the selected rows.
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/components/data/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export default defineComponent({
default: undefined
}
},
emits: ['update:modelValue'],
emits: ['update:modelValue', 'onSort'],
DarkGhostHunter marked this conversation as resolved.
Show resolved Hide resolved
DarkGhostHunter marked this conversation as resolved.
Show resolved Hide resolved
setup (props, { emit, attrs: $attrs }) {
const { ui, attrs } = useUI('table', toRef(props, 'ui'), config, toRef(props, 'class'))

Expand Down Expand Up @@ -232,6 +232,8 @@ export default defineComponent({
} else {
sort.value = { column: column.key, direction: column.direction || 'asc' }
}

emit('onSort', sort.value)
DarkGhostHunter marked this conversation as resolved.
Show resolved Hide resolved
}

function onSelect (row) {
Expand Down