• Getting Started
  • Components
  • Github
  • Go pro
energyGetting started
  • Introduction
dComponents
  • Button Components
  • Grid Components
  • Input Components
  • Layout Components
  • Toast Components
  • Widget Components
  • Alert
  • Badge
  • Breadcrumb
  • Callout
  • Card
  • Carousel
  • Charts
  • Collapse
  • CreateElement
  • DataTable
  • Dropdown
  • ElementCover
  • Embed
  • Fade
  • Form
  • Icon
  • Img
  • Jumbotron
  • Link
  • ListGroup
  • Media
  • Modal
  • Nav
  • Navbar
  • Pagination
  • Popover
  • Progress
  • Spinner
  • Switch
  • Tabs
  • Toggler
  • Tooltip

DataTable

Advanced data table.


Usage

Example

Name
Registered
Role
Status
John Doe2018/01/01GuestPending

User since: 2018/01/01

Samppa Nori2018/01/01MemberActive

User since: 2018/01/01

Estavan Lykos2018/02/01StaffBanned

User since: 2018/02/01

Chetan Mohamed2018/02/01AdminInactive

User since: 2018/02/01

Derick Maximinus2018/03/01MemberPending

User since: 2018/03/01

Name
Registered
Role
Status
  • «
  • ‹
  • 1
  • 2
  • 3
  • 4
  • 5
  • ›
  • »

Script


  const usersData = [
    {id: 0, name: 'John Doe', registered: '2018/01/01', role: 'Guest', status: 'Pending'},
    {id: 1, name: 'Samppa Nori', registered: '2018/01/01', role: 'Member', status: 'Active'},
    {id: 2, name: 'Estavan Lykos', registered: '2018/02/01', role: 'Staff', status: 'Banned'},
    {id: 3, name: 'Chetan Mohamed', registered: '2018/02/01', role: 'Admin', status: 'Inactive'},
    {id: 4, name: 'Derick Maximinus', registered: '2018/03/01', role: 'Member', status: 'Pending'},
    {id: 5, name: 'Friderik Dávid', registered: '2018/01/21', role: 'Staff', status: 'Active'},
    {id: 6, name: 'Yiorgos Avraamu', registered: '2018/01/01', role: 'Member', status: 'Active'},
    {id: 7, name: 'Avram Tarasios', registered: '2018/02/01', role: 'Staff', status: 'Banned'},
    {id: 8, name: 'Quintin Ed', registered: '2018/02/01', role: 'Admin', status: 'Inactive'},
    {id: 9, name: 'Enéas Kwadwo', registered: '2018/03/01', role: 'Member', status: 'Pending'},
    {id: 10, name: 'Agapetus Tadeáš', registered: '2018/01/21', role: 'Staff', status: 'Active'},
    {id: 11, name: 'Carwyn Fachtna', registered: '2018/01/01', role: 'Member', status: 'Active'},
    {id: 12, name: 'Nehemiah Tatius', registered: '2018/02/01', role: 'Staff', status: 'Banned'},
    {id: 13, name: 'Ebbe Gemariah', registered: '2018/02/01', role: 'Admin', status: 'Inactive'},
    {id: 14, name: 'Eustorgios Amulius', registered: '2018/03/01', role: 'Member', status: 'Pending'},
    {id: 15, name: 'Leopold Gáspár', registered: '2018/01/21', role: 'Staff', status: 'Active'},
    {id: 16, name: 'Pompeius René', registered: '2018/01/01', role: 'Member', status: 'Active'},
    {id: 17, name: 'Paĉjo Jadon', registered: '2018/02/01', role: 'Staff', status: 'Banned'},
    {id: 18, name: 'Micheal Mercurius', registered: '2018/02/01', role: 'Admin', status: 'Inactive'},
    {id: 19, name: 'Ganesha Dubhghall', registered: '2018/03/01', role: 'Member', status: 'Pending'},
    {id: 20, name: 'Hiroto Šimun', registered: '2018/01/21', role: 'Staff', status: 'Active'},
    {id: 21, name: 'Vishnu Serghei', registered: '2018/01/01', role: 'Member', status: 'Active'},
    {id: 22, name: 'Zbyněk Phoibos', registered: '2018/02/01', role: 'Staff', status: 'Banned'},
    {id: 23, name: 'Aulus Agmundr', registered: '2018/01/01', role: 'Member', status: 'Pending'},
    {id: 42, name: 'Ford Prefect', registered: '2001/05/25', role: 'Alien', status: 'Don\'t panic!'}
  ]

  const [details, setDetails] = useState([])
  // const [items, setItems] = useState(usersData)

  const toggleDetails = (index) => {
    const position = details.indexOf(index)
    let newDetails = details.slice()
    if (position !== -1) {
      newDetails.splice(position, 1)
    } else {
      newDetails = [...details, index]
    }
    setDetails(newDetails)
  }


  const fields = [
    { key: 'name', _style: { width: '40%'} },
    'registered',
    { key: 'role', _style: { width: '20%'} },
    { key: 'status', _style: { width: '20%'} },
    {
      key: 'show_details',
      label: '',
      _style: { width: '1%' },
      sorter: false,
      filter: false
    }
  ]

  const getBadge = (status)=>{
    switch (status) {
      case 'Active': return 'success'
      case 'Inactive': return 'secondary'
      case 'Pending': return 'warning'
      case 'Banned': return 'danger'
      default: return 'primary'
    }
  }

  return (
    <CDataTable
      items={usersData}
      fields={fields}
      columnFilter
      tableFilter
      footer
      itemsPerPageSelect
      itemsPerPage={5}
      hover
      sorter
      pagination
      scopedSlots = {{
        'status':
          (item)=>(
            <td>
              <CBadge color={getBadge(item.status)}>
                {item.status}
              </CBadge>
            </td>
          ),
        'show_details':
          (item, index)=>{
            return (
              <td className="py-2">
                <CButton
                  color="primary"
                  variant="outline"
                  shape="square"
                  size="sm"
                  onClick={()=>{toggleDetails(index)}}
                >
                  {details.includes(index) ? 'Hide' : 'Show'}
                </CButton>
              </td>
              )
          },
        'details':
            (item, index)=>{
              return (
              <CCollapse show={details.includes(index)}>
                <CCardBody>
                  <h4>
                    {item.username}
                  </h4>
                  <p className="text-muted">User since: {item.registered}</p>
                  <CButton size="sm" color="info">
                    User Settings
                  </CButton>
                  <CButton size="sm" color="danger" className="ml-1">
                    Delete
                  </CButton>
                </CCardBody>
              </CCollapse>
            )
          }
      }}
    />
  )
  


# Features

All features are optional:

  • Filter items by one or all columns
  • Sort items by column
  • Integrated with CPagination component by default
  • Customize style of specific rows, columns and cells,
  • Customize display of columns (headers, filter, and items separately) by scoped slots
  • Load with initial filters and sorter state
  • Loading state visualization

# DataTable API

NameRequiredTypeDefault Value
innerRef(object | Function | string)
ref to the main tag
overTableSlotany
over table slot content
columnHeaderSlotobject{}
object with column header slots [col name]
sortingIconSlotFunction
sorting function
columnFilterSlotobject{}
object with column filter slots [col name]
noItemsViewSlotany
no items content
noItemsViewobject
object with no items messages
captionSlotany
caption
underTableSlotany
under table slot content
scopedSlotsobject{}
object with functions ((item, index)=>{})
theadTopSlotany
above thead content
loadingSlotany
loadingboolean
fieldsany[]
Prop for specific column declaration. If prop is not passed columns will be generated automatically based on keys of the first passed item except keys beginning with underscore ('_') There are two ways to set columns by fields prop: -String: array item define column name equal to item key. -Object: array item is object with following keys available as column functionalities: -key (required) - define column name equal to item key. -label - define visible label (override automatically generated labels) _classes - adds classes to all cels in column -_style - adds styles to the column header (useful for defining widths) -sorter - disables sorting of the column when set to false -filter - removes filter from column when set to false If label option (in column defined by object) is not used, labels will be generated automatically by converting kebab-case and snake_case to individual words and capitalizes each word.
pagination(boolean | object)
Enables default pagination. Set to true for default setup or pass object with additional CPagination props.
activePagenumber
Sets active page, in case of using external pagination.
itemsPerPagenumber10
Number of items per site, when pagination is enabled
itemsany[]
Array of objects, where each object represents one item - row in table. Additionally, you can add style classes to each row by passing them by '_classes' key and to single cell by '_cellClasses'.
sorter(boolean | object)
Enables table sorting by column value. Sorting will be performed corectly only if values in column are of one type: string or number. Sorter can be customized, by passing prop as object with additional options as keys. Available options: -external - Disable automatic sorting inside component. -resetable - Clicking on sorter have three states: ascending, descending and null. That means that third click on sorter will disable sorting, and restore table to original order.
clickableRowsboolean
Style table items as clickable.
columnFilter(boolean | object)
When set, displays additional filter row between table header and items, allowing filtering by specific column.
tableFilterValuestring
Value of table filter. Set .sync modifier to track changes.
tableFilter(boolean | object)
When set, displays table filter above table, allowing filtering by specific column.
addTableClasses(string | any[] | object)
Adds classes to table element.
sizestring
table size
darkboolean
dark table scheme
stripedboolean
Sets style of the table.
hoverboolean
Sets style of the table.
borderboolean
Sets style of the table.
outlinedboolean
Sets style of the table.
responsivebooleantrue
Sats styling behavior to responsive.
footerboolean
Displays table footer, which mirrors table header. (without column filter)
itemsPerPageSelect(boolean | object)
Adds select element over table, which is used for control items per page in pagination. In case of using external pagination listen to 'pagination-change' event. If you want to customize this element, pass object with optional values: -label - string - replaces default label text -values - array - custom array of pagination values
sorterValueobject{}
sorter value object
columnFilterValueobject
sorter value object
headerbooleantrue
show header
onRowClickFunction
on row click event callback
onSorterValueChangeFunction
on
onPaginationChangeFunction
on page change event callback
onColumnFilterChangeFunction
on filter change event callback
onPagesChangeFunction
on pages change event callback
onTableFilterChangeFunction
on table filter change event callback
onPageChangeFunction
on page change event callback
onFilteredItemsChangeFunction
filtered items change callback

  • Components
    • Usage
      • Features
      • API
CoreUI © 2020 creativeLabs.core-logo