mirror of
https://github.com/nuxt/ui.git
synced 2026-09-14 19:51:10 +08:00
63 lines
1.4 KiB
Vue
63 lines
1.4 KiB
Vue
<script setup lang="ts">
|
|
import type { AvatarProps, TableColumn } from '@nuxt/ui'
|
|
|
|
const UAvatar = resolveComponent('UAvatar')
|
|
|
|
type User = {
|
|
id: number
|
|
name: string
|
|
username: string
|
|
email: string
|
|
avatar: AvatarProps
|
|
company: { name: string }
|
|
}
|
|
|
|
const { data, status } = useLazyFetch<User[]>('https://jsonplaceholder.typicode.com/users', {
|
|
key: 'table-users',
|
|
transform: (data) => {
|
|
return data?.map(user => ({
|
|
...user,
|
|
avatar: { src: `https://i.pravatar.cc/120?img=${user.id}`, alt: `${user.name} avatar` }
|
|
})) || []
|
|
},
|
|
server: false
|
|
})
|
|
|
|
const columns: TableColumn<User>[] = [{
|
|
accessorKey: 'id',
|
|
header: 'ID'
|
|
}, {
|
|
accessorKey: 'name',
|
|
header: 'Name',
|
|
cell: ({ row }) => {
|
|
return h('div', { class: 'flex items-center gap-3' }, [
|
|
h(UAvatar, {
|
|
...row.original.avatar,
|
|
loading: 'lazy',
|
|
size: 'lg'
|
|
}),
|
|
h('div', undefined, [
|
|
h('p', { class: 'font-medium text-highlighted' }, row.original.name),
|
|
h('p', { class: '' }, `@${row.original.username}`)
|
|
])
|
|
])
|
|
}
|
|
}, {
|
|
accessorKey: 'email',
|
|
header: 'Email'
|
|
}, {
|
|
accessorKey: 'company',
|
|
header: 'Company',
|
|
cell: ({ row }) => row.original.company.name
|
|
}]
|
|
</script>
|
|
|
|
<template>
|
|
<UTable
|
|
:data="data"
|
|
:columns="columns"
|
|
:loading="status === 'pending' || status === 'idle'"
|
|
class="flex-1 h-80"
|
|
/>
|
|
</template>
|