data-grid

Data grid with draggable rows

Data grid with draggable rows

Загрузка превью…

src/data-grid/c-data-grid-14.vue

<script setup lang="ts">
import { computed, h, ref } from "vue"
import { DataGrid, DataGridContainer, DataGridPagination, DataGridScrollArea, DataGridTableDndRowHandle, DataGridTableDndRows } from "@/components/reui/data-grid"
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
import { DataGridRowDndEvent } from "@/components/reui/data-grid"
import { getCoreRowModel, getSortedRowModel, useVueTable } from "@tanstack/vue-table"
import type { ColumnDef } from "@tanstack/vue-table"

interface IData {
  id: string
  name: string
  availability: "online" | "away" | "busy" | "offline"
  avatar: string
  status: "active" | "inactive"
  flag: string
  email: string
  company: string
  role: string
  joined: string
  location: string
  balance: number
}

const demoData: IData[] = [
  {
    id: "1",
    name: "Alex Johnson",
    availability: "online",
    avatar: "https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?w=96&h=96&dpr=2&q=80",
    status: "active",
    flag: "us",
    email: "alex@apple.com",
    company: "Apple",
    role: "CEO",
    joined: "Jan, 2024",
    location: "United States",
    balance: 5143.03,
  },
  {
    id: "2",
    name: "Sarah Chen",
    availability: "away",
    avatar: "https://images.unsplash.com/photo-1519699047748-de8e457a634e?w=96&h=96&dpr=2&q=80",
    status: "inactive",
    flag: "gb",
    email: "sarah@openai.com",
    company: "OpenAI",
    role: "CTO",
    joined: "Mar, 2023",
    location: "United Kingdom",
    balance: 4321.87,
  },
  {
    id: "3",
    name: "Michael Rodriguez",
    availability: "busy",
    avatar: "https://images.unsplash.com/photo-1584308972272-9e4e7685e80f?w=96&h=96&dpr=2&q=80",
    status: "active",
    flag: "ca",
    email: "michael@meta.com",
    company: "Meta",
    role: "Designer",
    joined: "Jun, 2022",
    location: "Canada",
    balance: 7654.98,
  },
  {
    id: "4",
    name: "Emma Wilson",
    availability: "offline",
    avatar: "https://images.unsplash.com/photo-1485893086445-ed75865251e0?w=96&h=96&dpr=2&q=80",
    status: "inactive",
    flag: "au",
    email: "emma@tesla.com",
    company: "Tesla",
    role: "Developer",
    joined: "Sep, 2024",
    location: "Australia",
    balance: 3456.45,
  },
  {
    id: "5",
    name: "David Kim",
    availability: "online",
    avatar: "https://images.unsplash.com/photo-1607990281513-2c110a25bd8c?w=96&h=96&dpr=2&q=80",
    status: "active",
    flag: "de",
    email: "david@sap.com",
    company: "SAP",
    role: "Lawyer",
    joined: "Nov, 2023",
    location: "Germany",
    balance: 9876.54,
  },
  {
    id: "6",
    name: "Aron Thompson",
    availability: "away",
    avatar: "https://images.unsplash.com/photo-1527980965255-d3b416303d12?w=96&h=96&dpr=2&q=80",
    status: "active",
    flag: "my",
    email: "aron@keenthemes.com",
    company: "Keenthemes",
    role: "Director",
    joined: "Feb, 2022",
    location: "Malaysia",
    balance: 6214.22,
  },
]

const data = ref<IData[]>(demoData)

const dataIds = computed(() => data.value.map(({ id }) => id))

function handleDragEnd(event: DataGridRowDndEvent) {
  const { active, over } = event
  if (active && over && active.id !== over.id) {
    const oldIndex = dataIds.value.indexOf(active.id)
    const newIndex = dataIds.value.indexOf(over.id)
    const next = data.value.slice()
    const [moved] = next.splice(oldIndex, 1)
    next.splice(newIndex, 0, moved as IData)
    data.value = next
  }
}

const columns: ColumnDef<IData>[] = [
  {
    id: "drag",
    cell: () => h(DataGridTableDndRowHandle),
    size: 30,
  },
  {
    accessorKey: "name",
    id: "name",
    header: "Name",
    cell: (info) =>
      h("div", { class: "flex items-center gap-2" }, [
        h(Avatar, { class: "size-6" }, () => [
          h(AvatarImage, { src: info.row.original.avatar, alt: info.row.original.name }),
          h(AvatarFallback, () => info.row.original.name.split(" ").map((n: string) => n[0]).join("")),
        ]),
        h("a", { href: "#", class: "text-foreground hover:text-primary font-medium" }, info.row.original.name),
      ]),
    size: 170,
    enableSorting: true,
    enableHiding: false,
  },
  {
    accessorKey: "email",
    id: "email",
    header: "Email",
    cell: (info) => h("a", { href: `mailto:${info.getValue()}`, class: "hover:text-primary text-truncate hover:underline" }, info.getValue() as string),
    size: 150,
    meta: {
      headerClassName: "",
      cellClassName: "",
    },
  },
  {
    accessorKey: "location",
    id: "location",
    header: "Location",
    cell: (info) =>
      h("div", { class: "flex items-center gap-1.5" }, [
        h("img", {
          src: `https://flagcdn.com/${info.row.original.flag.toLowerCase()}.svg`,
          alt: info.row.original.flag,
          class: "size-4 rounded-full object-cover",
        }),
        h("div", { class: "text-foreground font-medium" }, info.row.original.location),
      ]),
    size: 170,
    meta: {
      headerClassName: "",
      cellClassName: "text-start",
    },
  },
]

const table = useVueTable({
  get data() {
    return data.value
  },
  columns,
  getRowId: (row: IData) => row.id,
  getCoreRowModel: getCoreRowModel(),
  getSortedRowModel: getSortedRowModel(),
})
</script>

<template>
  <DataGrid :table="table" :record-count="data?.length || 0" :table-layout="{ rowsDraggable: true }">
    <div class="w-full space-y-2.5">
      <DataGridContainer>
        <DataGridScrollArea>
          <DataGridTableDndRows :data-ids="dataIds" :on-drag-end="handleDragEnd" />
        </DataGridScrollArea>
      </DataGridContainer>
      <DataGridPagination />
    </div>
  </DataGrid>
</template>

Установка

npx shadcn-vue@latest add https://revueui.rootapi.dev/r/c-data-grid-14.json

Зависимости реестра

npm-зависимости

  • @tanstack/vue-table

Источник: порт из ReUI (Keenthemes, MIT)