DataTable
Advanced data table.
Usage
Example
John Doe | 2018/01/01 | Guest | Pending | |
User since: 2018/01/01 | ||||
Samppa Nori | 2018/01/01 | Member | Active | |
User since: 2018/01/01 | ||||
Estavan Lykos | 2018/02/01 | Staff | Banned | |
User since: 2018/02/01 | ||||
Chetan Mohamed | 2018/02/01 | Admin | Inactive | |
User since: 2018/02/01 | ||||
Derick Maximinus | 2018/03/01 | Member | Pending | |
User since: 2018/03/01 | ||||
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
Name | Required | Type | Default Value |
---|---|---|---|
innerRef | (object | Function | string) | ||
ref to the main tag | |||
overTableSlot | any | ||
over table slot content | |||
columnHeaderSlot | object | {} | |
object with column header slots [col name] | |||
sortingIconSlot | Function | ||
sorting function | |||
columnFilterSlot | object | {} | |
object with column filter slots [col name] | |||
noItemsViewSlot | any | ||
no items content | |||
noItemsView | object | ||
object with no items messages | |||
captionSlot | any | ||
caption | |||
underTableSlot | any | ||
under table slot content | |||
scopedSlots | object | {} | |
object with functions ((item, index)=>{}) | |||
theadTopSlot | any | ||
above thead content | |||
loadingSlot | any | ||
loading | boolean | ||
fields | any[] | ||
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. | |||
activePage | number | ||
Sets active page, in case of using external pagination. | |||
itemsPerPage | number | 10 | |
Number of items per site, when pagination is enabled | |||
items | any[] | ||
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. | |||
clickableRows | boolean | ||
Style table items as clickable. | |||
columnFilter | (boolean | object) | ||
When set, displays additional filter row between table header and items, allowing filtering by specific column. | |||
tableFilterValue | string | ||
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. | |||
size | string | ||
table size | |||
dark | boolean | ||
dark table scheme | |||
striped | boolean | ||
Sets style of the table. | |||
hover | boolean | ||
Sets style of the table. | |||
border | boolean | ||
Sets style of the table. | |||
outlined | boolean | ||
Sets style of the table. | |||
responsive | boolean | true | |
Sats styling behavior to responsive. | |||
footer | boolean | ||
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 | |||
sorterValue | object | {} | |
sorter value object | |||
columnFilterValue | object | ||
sorter value object | |||
header | boolean | true | |
show header | |||
onRowClick | Function | ||
on row click event callback | |||
onSorterValueChange | Function | ||
on | |||
onPaginationChange | Function | ||
on page change event callback | |||
onColumnFilterChange | Function | ||
on filter change event callback | |||
onPagesChange | Function | ||
on pages change event callback | |||
onTableFilterChange | Function | ||
on table filter change event callback | |||
onPageChange | Function | ||
on page change event callback | |||
onFilteredItemsChange | Function | ||
filtered items change callback |