DataTable

Advanced data table component with sorting, filtering, pagination, and selection features for displaying tabular data efficiently.

Import

import { DataTable } from '@akitectio/aki-ui'

Basic Usage

NameEmailRoleStatus
John Doejohn@example.comAdminActive
Jane Smithjane@example.comUserActive
Bob Johnsonbob@example.comUserInactive
Alice Brownalice@example.comEditorActive
Charlie Wilsoncharlie@example.comUserPending
const data = [
  { id: 1, name: 'John Doe', email: 'john@example.com', role: 'Admin', status: 'Active' },
  { id: 2, name: 'Jane Smith', email: 'jane@example.com', role: 'User', status: 'Active' },
  // ... more data
]

const columns = [
  { key: 'name', header: 'Name', sortable: true },
  { key: 'email', header: 'Email', sortable: true },
  { key: 'role', header: 'Role', sortable: true },
  { key: 'status', header: 'Status', sortable: true },
]

<DataTable 
  data={data}
  columns={columns}
  pagination
  searchable
  selectable
/>

Features

🔍 Search & Filter

Built-in search functionality with customizable filters for each column.

🔄 Sorting

Multi-column sorting with ascending/descending order indicators.

📄 Pagination

Efficient pagination with customizable page sizes and navigation.

✅ Selection

Row selection with checkboxes and bulk action support.

Advanced Usage

Custom Cell Renderers

const columns = [
  { 
    key: 'name', 
    header: 'Name', 
    cell: (value, row) => (
      <div className="flex items-center">
        <img className="w-8 h-8 rounded-full mr-3" src={row.avatar} alt="" />
        <span className="font-medium">{value}</span>
      </div>
    )
  },
  { 
    key: 'status', 
    header: 'Status',
    cell: (value) => (
      <Badge variant={value === 'Active' ? 'success' : 'error'}>
        {value}
      </Badge>
    )
  },
  {
    key: 'actions',
    header: 'Actions',
    cell: (_, row) => (
      <div className="flex space-x-2">
        <Button size="sm" onClick={() => editUser(row.id)}>Edit</Button>
        <Button size="sm" variant="error" onClick={() => deleteUser(row.id)}>Delete</Button>
      </div>
    )
  }
]

Props

PropTypeDefaultDescription
dataArray<object>[]Array of data objects to display in the table
columnsArray<Column>[]Column configuration array
paginationbooleanfalseEnable pagination controls
searchablebooleanfalseEnable global search functionality
selectablebooleanfalseEnable row selection with checkboxes
pageSizenumber10Number of rows per page when pagination is enabled
loadingbooleanfalseShow loading state
onRowClickfunction-Callback when a row is clicked

Example with Real Data

import { useState, useEffect } from 'react'
import { DataTable, Badge, Button } from '@akitectio/aki-ui'

function UserTable() {
  const [users, setUsers] = useState([])
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    fetchUsers()
  }, [])

  const fetchUsers = async () => {
    setLoading(true)
    const response = await fetch('/api/users')
    const data = await response.json()
    setUsers(data)
    setLoading(false)
  }

  const columns = [
    { key: 'name', header: 'Name', sortable: true },
    { key: 'email', header: 'Email', sortable: true },
    { 
      key: 'role', 
      header: 'Role', 
      sortable: true,
      cell: (value) => (
        <Badge variant={value === 'admin' ? 'primary' : 'secondary'}>
          {value}
        </Badge>
      )
    },
    {
      key: 'actions',
      header: 'Actions',
      cell: (_, row) => (
        <div className="flex space-x-2">
          <Button size="sm" onClick={() => editUser(row.id)}>
            Edit
          </Button>
          <Button size="sm" variant="error" onClick={() => deleteUser(row.id)}>
            Delete
          </Button>
        </div>
      )
    }
  ]

  const handleRowClick = (row) => {
    console.log('Row clicked:', row)
  }

  const handleSelectionChange = (selectedRows) => {
    console.log('Selected rows:', selectedRows)
  }

  return (
    <DataTable
      data={users}
      columns={columns}
      loading={loading}
      pagination
      pageSize={20}
      searchable
      selectable
      onRowClick={handleRowClick}
      onSelectionChange={handleSelectionChange}
    />
  )
}