Vue Smart Table Component
Dynamic table component.
Features
- 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
- Load with initial filters and sorter state
- Loading state visualization
- Default header labels generation based on column names
Basic usage
Avram Tarasios | 2018/02/01 | Staff | Banned | ||
User since: 2018/02/01 | |||||
Nehemiah Tatius | 2018/02/01 | Staff | Banned | ||
User since: 2018/02/01 | |||||
Paĉjo Jadon | 2018/02/01 | Staff | Banned | ||
User since: 2018/02/01 | |||||
Zbyněk Phoibos | 2018/02/01 | Staff | Banned | ||
User since: 2018/02/01 | |||||
Ford Prefect | 2001/05/25 | Alien | Don't panic! | ||
User since: 2001/05/25 | |||||
<template>
<CSmartTable
clickableRows
:tableProps="{
striped: true,
hover: true,
}"
:tableHeadProps="{
color: 'danger'
}"
:activePage="1"
footer
header
:items="items"
:columns="columns"
columnFilter
tableFilter
cleaner
itemsPerPageSelect
:itemsPerPage="5"
columnSorter
:sorterValue="{ column: 'status', state: 'asc' }"
pagination
>
<template #status="{item}">
<td><CBadge :color="getBadge(item.status)">{{item.status}}</CBadge></td>
</template>
<template #show_details="{item, index}">
<td class="py-2">
<CButton
color="primary"
variant="outline"
square
size="sm"
@click="toggleDetails(item, index)"
>
{{Boolean(item._toggled) ? 'Hide' : 'Show'}}
</CButton>
</td>
</template>
<template #details="{item}">
<CCollapse :visible="this.details.includes(item._id)">
<CCardBody>
<h4>
{{item.username}}
</h4>
<p class="text-muted">User since: {{item.registered}}</p>
<CButton size="sm" color="info" class="">
User Settings
</CButton>
<CButton size="sm" color="danger" class="ml-1">
Delete
</CButton>
</CCardBody>
</CCollapse>
</template>
</CSmartTable>
</template>
<script>
export default {
data: () => {
return {
columns: [
{ key: 'name', _style: { width: '40%'}, _props: { color: 'primary', class: 'fw-semibold'} },
'registered',
{ key: 'role', filter: false, sorter: false, _style: { width: '20%'} },
{ key: 'status', _style: { width: '20%'} },
{
key: 'show_details',
label: '',
_style: { width: '1%' },
filter: false,
sorter: false,
_props: { color: 'primary', class: 'fw-semibold'}
}
],
details: [],
items: [
{name: 'John Doe', registered: '2018/01/01', role: 'Guest', status: 'Pending'},
{name: 'Samppa Nori', registered: '2018/01/01', role: 'Member', status: 'Active', _props: { color: 'primary', align: 'middle'}},
{name: 'Estavan Lykos', registered: '2018/02/01', role: 'Staff', status: 'Banned', _cellProps: { all: { class: 'fw-semibold'}, 'name': { color: 'info' }}},
{name: 'Chetan Mohamed', registered: '2018/02/01', role: 'Admin', status: 'Inactive'},
{name: 'Derick Maximinus', registered: '2018/03/01', role: 'Member', status: 'Pending'},
{name: 'Friderik Dávid', registered: '2018/01/21', role: 'Staff', status: 'Active'},
{name: 'Yiorgos Avraamu', registered: '2018/01/01', role: 'Member', status: 'Active'},
{name: 'Avram Tarasios', registered: '2018/02/01', role: 'Staff', status: 'Banned', _props: { color: 'warning', align: 'middle'}},
{name: 'Quintin Ed', registered: '2018/02/01', role: 'Admin', status: 'Inactive'},
{name: 'Enéas Kwadwo', registered: '2018/03/01', role: 'Member', status: 'Pending'},
{name: 'Agapetus Tadeáš', registered: '2018/01/21', role: 'Staff', status: 'Active'},
{name: 'Carwyn Fachtna', registered: '2018/01/01', role: 'Member', status: 'Active'},
{name: 'Nehemiah Tatius', registered: '2018/02/01', role: 'Staff', status: 'Banned'},
{name: 'Ebbe Gemariah', registered: '2018/02/01', role: 'Admin', status: 'Inactive'},
{name: 'Eustorgios Amulius', registered: '2018/03/01', role: 'Member', status: 'Pending'},
{name: 'Leopold Gáspár', registered: '2018/01/21', role: 'Staff', status: 'Active'},
{name: 'Pompeius René', registered: '2018/01/01', role: 'Member', status: 'Active'},
{name: 'Paĉjo Jadon', registered: '2018/02/01', role: 'Staff', status: 'Banned'},
{name: 'Micheal Mercurius', registered: '2018/02/01', role: 'Admin', status: 'Inactive'},
{name: 'Ganesha Dubhghall', registered: '2018/03/01', role: 'Member', status: 'Pending'},
{name: 'Hiroto Šimun', registered: '2018/01/21', role: 'Staff', status: 'Active'},
{name: 'Vishnu Serghei', registered: '2018/01/01', role: 'Member', status: 'Active'},
{name: 'Zbyněk Phoibos', registered: '2018/02/01', role: 'Staff', status: 'Banned'},
{name: 'Aulus Agmundr', registered: '2018/01/01', role: 'Member', status: 'Pending'},
{name: 'Ford Prefect', registered: '2001/05/25', role: 'Alien', status: 'Don\'t panic!'}
],
}
},
methods: {
getBadge (status) {
switch (status) {
case 'Active': return 'success'
case 'Inactive': return 'secondary'
case 'Pending': return 'warning'
case 'Banned': return 'danger'
default: 'primary'
}
},
toggleDetails (item) {
if (this.details.includes(item._id)) {
this.details = this.details.filter((_item) => _item !== item._id)
return
}
this.details.push(item._id)
}
}
}
</script>
API
CSmartTable
import { CSmartTable } from '@coreui/vue-pro'
// or
import CSmartTable from '@coreui/vue-pro/src/components/smart-table/CSmartTable'
Props
Prop name | Description | Type | Values | Default |
---|---|---|---|---|
active-page | Sets active page. If 'pagination' prop is enabled, activePage is set only initially. | number | - | 1 |
cleaner | When set, displays table cleaner above table, next to the table filter (or in place of table filter if tableFilter prop is not set)Cleaner resets tableFilterValue , columnFilterValue , sorterValue . If clean is possible it is clickable (tabIndex="0" role="button" , color="danger" ), otherwise it is not clickable and transparent. Cleaner can be customized through the cleanerIcon slot. | boolean | - | |
clickable-rows | Style table items as clickable. | boolean | - | |
column-filter | When set, displays additional filter row between table header and items, allowing filtering by specific column. Column filter can be customized, by passing prop as object with additional options as keys. Available options: - external (Boolean) - Disables automatic filtering inside component. - lazy (Boolean) - Set to true to trigger filter updates only on change event. | boolean | ColumnFilter | - | |
column-filter-value | Value of table filter. To set pass object where keys are column names and values are filter strings e.g.: { user: 'John', age: 12 } | ColumnFilterValue | - | - |
columns | Prop for table columns configuration. If prop is not defined, table will display columns based on the first item keys, omitting keys that begins with underscore (e.g. '_props') In columns prop each array item represents one column. Item might be specified in two ways: String: each item define column name equal to item value. Object: item is object with following keys available as column configuration: - key (required)(String) - define column name equal to item key. - filter (Boolean) - removes filter from column when set to false. - label (String) - define visible label of column. If not defined, label will be generated automatically based on column name, by converting kebab-case and snake_case to individual words and capitalization of each word. - sorter (Boolean) - disables sorting of the column when set to false - _props (String/Array/Object) - add props to CTableHeaderCell .- _style (String/Array/Object) - adds styles to the column header (useful for defining widths) | Column[] | string[] | - | |
column-sorter | Enables table sorting by column value. Sorting will be performed corectly only if values in column are of one type: string (case insensitive) or number. Sorter can be customized, by passing prop as object with additional options as keys. Available options: - external (Boolean) - Disables automatic sorting inside component. - resetable (Boolean) - If set to true clicking on sorter have three states: ascending, descending and null. That means that third click on sorter will reset sorting, and restore table to original order. | boolean | Sorter | - | - |
footer | Displays table footer, which mirrors table header. (without column filter). | boolean | - | |
header | Set to false to remove table header. | boolean | - | true |
items | Array of objects, where each object represents one item - row in table. Additionally, you can customize each row by passing them by _props key and single cell by _cellProps. Examples: - _props: { color: 'primary', align: 'middle'} - _cellProps: { all: { class: 'fw-semibold'}, 'name': { color: 'info' }} | Item[] | - | () => [] |
items-per-page | Number of items per site, when pagination is enabled. | number | - | |
items-per-page-label | Label for items per page selector. | string | - | 'Items per page:' |
items-per-page-options | Items per page selector options. | number[] | - | () => [5, 10, 20, 50] |
items-per-page-select | Adds select element over table, which is used for control items per page in pagination. 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 - external (Boolean) - disables automatic 'itemsPerPage' change (use to change pages externaly by 'pagination-change' event). | boolean | ItemsPerPageSelect | - | - |
loading | When set, table will have loading style: loading spinner and reduced opacity. When 'small' prop is enabled spinner will be also smaller. | boolean | - | |
no-items-label | ReactNode or string for passing custom noItemsLabel texts. | string | - | 'No items found' |
pagination | Enables default pagination. Set to true for default setup or pass an object with additional CPagination props. Default pagination will always have the computed number of pages that cannot be changed. The number of pages is generated based on the number of passed items and 'itemsPerPage' prop. If this restriction is an obstacle, you can make external CPagination instead. | boolean | - | |
pagination-props | Properties to CSmartPagination component. | object | - | - |
selectable | Add checkboxes to make table rows selectable. | boolean | - | |
sorter-value | State of the sorter. Name key is column name, direction can be 'asc' or 'desc'. eg.: { column: 'status', state: 'asc' } | SorterValue | - | - |
table-body-props | Properties to CTableBody component. | object | - | - |
table-foot-props | Properties to CTableFoot component. | object | - | - |
table-filter | When set, displays table filter above table, allowing filtering by specific column. Column filter can be customized, by passing prop as object with additional options as keys. Available options: - external (Boolean) - Disables automatic filtering inside component. - lazy (Boolean) - Set to true to trigger filter updates only on change event. | boolean | TableFilter | - | |
table-filter-label | The element represents a caption for a component. | string | - | 'Filter:' |
table-filter-placeholder | Specifies a short hint that is visible in the search input. | string | - | 'type string...' |
table-filter-value | Value of table filter. | string | - | - |
table-head-props | Properties to CTableHead component. | object | - | - |
table-props | Properties to CTable component. | object | - | - |
Events
Event name | Description | Properties |
---|---|---|
active-page-change | Page change callback. | page number - active page number |
column-filter-change | Column filter change callback. | ColumnFilterValue object - {[key: string]: string | number} |
filtered-items-change | Filtered items change callback. | items array - undefined |
items-per-page-change | Pagination change callback. | itemsPerPageNumber number - items per page number |
row-click | Row click callback. | item object - undefinedindex number - undefinedcolumnName string - undefinedevent event - undefined |
selected-items-change | Selected items change callback. | items array - undefined |
sorter-change | Sorter value change callback. | SorterValue object - { column?: string, state?: number | string} |
table-filter-change | Table filter change callback. | tableFilterValue string - undefined |