The component is used for displaying tabular data. Features include sorting, searching, pagination and row selection.
Features
[x] Visual feedback - When user interacts with component they should see result of their actions (:active, :hover, :focus selectors and ...).
[ ] Keyboard navigation.
Docs
[ ] Do proper documentation page (#17).
Testing
[ ] Demos for all cases.
Props
[x] custom-sort function` - Function used to sort items
[ ] expand-icon string` - Icon used for expand toggle button.
[ ] expanded array ` - Array of expanded items. Can be used with .sync modifier
[x] headers DataTableHeader[]` - An array of objects that each describe a header column
DataTableHeader: {
label: string,
key: string,
align?: 'start' | 'center' | 'end',
sortable?: boolean,
class?: string | string[],
cellClass?: string | string[],
sort?: (a: any, b: any) => number
}
[x] hide-default-header Boolean` - Hide the default headers
[ ] item-class string | function` - Property on supplied items that contains item’s row class or function that takes an item as an argument and returns the class of corresponding row
[ ] item-key string ` - The property on each item that is used as a unique key
[x] items array ` - The array of items to display
[x] items-per-page number ` - Changes how many items per page should be visible.
[x] loading Boolean` - If true and no items are provided,
[ ] loading-text string ` - Text shown when loading is true and no items are provided
[ ] multi-sort Boolean` - If true then one can sort on multiple properties
[x] no-data-text string` - Text shown when no items are provided to the component
[x] no-results-text string` - Text shown when search prop is used and there are no results
[x] page number ` - The number of page
[ ] search string` - Text input used to filter items
[x] server-items-length number` - Used only when data is provided by a server.
[ ] show-expand Boolean` - Shows the expand toggle in default rows
[ ] show-select Boolean` - Shows the select checkboxes in both the header and rows (if using default rows)
[ ] single-expand Boolean` - Changes expansion mode to single expand
[ ] single-select Boolean` - Changes selection mode to single select
[x] sort-by string | array` - Changes which item property (or properties) should be used for sort order.
[x] sort-desc boolean | array` - Changes which direction sorting is done.
Slots
[ ] body - Slot to replace the default table
[x] body.append - Appends elements to the end of the default table
[x] body.prepend - Prepends elements to the start of the default table
[ ] expanded-item - Slot to customize expanded rows
[x] foot - Slot to add a element after the . Not to be confused with the footer slot
[x] header - Slot to add a
element
[x] header.<name> - Slot to customize a specific header column
[ ] header.data-table-select - Slot to replace the default va-checkbox in header
[ ] item.<name> - Slot to customize a specific column
[ ] item.data-table-expand - Slot to replace the default v-icon used when expanding rows
[ ] item.data-table-select - Slot to replace the default v-simple-checkbox used when selecting rows
[ ] loading - Defines content for when loading is true and no items are provided
[ ] no-data - Defines content for when no items are provided
[ ] no-results - Defines content for when search is provided but no results are found
Emits
[ ] click:row - Emits when a table row is clicked.
[ ] item-expanded - Event emitted when an item is expanded or closed
[ ] item-selected - Event emitted when an item is selected or deselected
[x] update:page - Event emitted when a page is changed
[ ] toggle-select-all - Event emitted when all items is selected or deselected
In other words, the items prop must always be an array (if provided) of zero-or-more length with each item being an object with string keys and values of any type.
The va-data-table automatically calculates the amount and the titles of columns (if not specified otherwise with the columns prop) based on these object's keys. So e.g. if your items looks like:
[{a: 1}, {a: 2, b: 3}, {c: 4, a: 5}]
then the resulting table will have a total of 3 columns called A, B and C and will look like this:
When calculating the columns' names based on the item's objects' keys va-data-table uses Lodash's startCase internally, so that myProp becomes My Prop, userFullName becomes User Full Name and so on.
The items' actual values are stringified before being displayed. Falthy values are asserted to the empty string.
Automatically built columns are not always the desirable behavior, so va-data-table also optionally accepts the columns prop.
columns: (string | ITableColumn)[]
ITableColumn {
key: string; // name of an item's property
label?: string; // what to display in the respective heading
headerTitle?: string; // <th>'s `title` attribute's value
sortable?: boolean, // whether the table can be sorted by that column
sortingFn?: (a: any, b: any) => number; // a custom sorting function. `a` and `b` are currently compared cells' original values (sources). Must return a number (see the standard JS's Array.prototype.sort)
alignHead?: TAlignOptions; // horizontal alignment of the column's heading
verticalAlignHead?: TVerticalAlignOptions; // vertical alignment of the column's heading
align?: TAlignOptions; // horizontal <td>'s alignment
verticalAlign?: TVerticalAlignOptions; // vertical <td>'s alignment
}
In other words, the columns prop, if provided, must be an array of either strings or objects implementing the ITableColumn interface.
The component internally builds instances similar to (but not exactly to) the ITableColumn interface. When a string is provided, its value is used to simultaneously build the columns title (using the same startCase method) and as an indication of which field should be used to render the value of a given row.
Note how the columnTrhree is ignored. If it wasn't for the columns props (if the columns were generated automatically based on the items' value, then it would exist).
Instead of strings, you can also provide objects of the ITableColumn interface. Sometimes it's the only option since only that form allows to enable certain column features (such as sorting).
The only mandatory field for the ITableColumns is the key. It acts the same way as the string-value of the columns array does.
va-data-tables allows to apply a single table-wise filter with the filter prop.
filter: string
When the filter is provided, only the rows where at least one cell contains the specified value will be rendered. To runtime-disable filtering (clear the filter), provide an empty string.
To use a custom filtering function, provide the va-data-table with the filtering-fn prop.
The function takes the initial value of a currently being checked cell (the source formal parameter) and must return a boolean, indicating, whether to include the row containing that cell or not. The following example shows the use of a custom filtering function (which looks for the exact match rather than the standard one which checks for substring-inclusion) when the respective checkbox is checked:
va-data-table emits the filter event each time filtering is applied (and when the filter is cleared), passing the number of filtered items as the first (and the only) argument.
You can specify which columns should be sortable by providing a column definition object (see the columns prop above) with the sortable: true field.
Making a column sortable means allowing to click the column's header to toggle the sorting by that column's values:
You can also provide a custom sorting function for a given column using the sortingFn field on the column definition object.
The following GIF show runtime-toggling the custom sorting function which sorts the ID column's values as number instead of as strings:
sortingFn: (a: any, b: any) => number;
The function takes two cells' initial values (a, b) (note: initial! (i.e. in the form the user provided them, rather than stringified)) and must return a number (-1, 0, +1) indicating whether the two rows shuld be swapped the places or not. See the standard JS's Array.prototype.sort for details.
If you want to runtime-disable the custom function and start making use of the built-in one, pass the undefined to the sortingFn.
Each time the table's sorting changes, the sort event is thrown, with the following param:
...indicating the column's key the table is currently sorted by and the sorting order: ascending, descending and null for un-sorted table.
va-data-table also optionally accepts the sortBy and the sortingOrder modeled props, which allow users to change sorting settings from-outside and to model the changes introduced to the table's sorting by interacting with the table itself. They also allow to provide initial sorting values. Don't use them without v-models.
Use the selectable prop to indicate whether the va-data-table should have selectable rows or not.
selectable: boolean
We provide 2 selection modes: multiple (default) and single. Single selection, as its name implies, allows for only a single row to be selected at a time, while multiple mode allows to select multiple rows by clicking on checkboxes or using the ctrl/shift keys when clicking rows.
When select-mode is set to multiple, an additional header-checkbox is also rendered, allowing to select/un-select all the rows simultaneously.
The highlighting color of the selected row might be changed with the selected-color prop, taking a string with one of the possible color options: "primary", "danger" and so on:
The selection may optionally be attached to a model. The standard v-model (using the modelValue prop and the @update:modelValue event). This also allows to set the initial selection on the va-data-table:
Don't use the :modelValue prop without v-model.
Except for the update:modelValue event, the selection-change event is also thrown each time the selection changes.
It provides the following object as its only argument:
Use the perPage and currentPage props to enable pagination.
perPage: number
currentPage: number
Those 2 indicate the amount of items which should be shown on a page and the number of the page respectively.
The va-data-table component is paginator-agnostic. I.e. it can work with any pagination component you'd like. In the example above it uses standard (native) inputs.
The component also provide no protection whatsoever for the cases when the currentPage is set higher the the actual number of pages, so make sure to check it yourself.
colgroup
The contents of this slot is wrapped (if provided) inside the <colgroup> tag, allowing to specify certain column options. Bound to columns (not the prop, but their internal representation: TableColumn[]).
head.prepend
Allows to insert custom rows in table's header. Isn't bound to anything.
head
Targets all the table's headings. Is bound to column.
head(key)
Targets a specific table header by the column's key. Is bound to column
Basically, almost the same as the column definition object you pass to the columns prop.
TableCell:
readonly source: any; // the initial column's value (not stringified)
readonly row: TableRow; // the TableRow instance the cell belongs to
readonly column: TableColumn; // the TableColumn instance the cell belongs to
readonly value: string; // stringified value
And here's the TableRow:
readonly source: ITableItem; // the initial ITableItem definition (string if the column was defined using the string-syntax)
readonly initialIndex: number; // initial row position
readonly cells: TableCell[]; // the array of the row's cells
Should be specified on columns (see the columns prop above).
Allows to provide different values for headers and for columns' cells. Supported values for horiz. alignment are: left, center, right, for vert. - top, middle, bottom. Pretty much the same as for text-align and vertical-align css properties.
Description
Features
Docs
Testing
Props
custom-sort
function` - Function used to sort itemsexpand-icon
string` - Icon used for expand toggle button.expanded
array ` - Array of expanded items. Can be used with .sync modifierheaders
DataTableHeader[]` - An array of objects that each describe a header column DataTableHeader: { label: string, key: string, align?: 'start' | 'center' | 'end', sortable?: boolean, class?: string | string[], cellClass?: string | string[], sort?: (a: any, b: any) => number }hide-default-header
Boolean` - Hide the default headersitem-class
string | function` - Property on supplied items that contains item’s row class or function that takes an item as an argument and returns the class of corresponding rowitem-key
string ` - The property on each item that is used as a unique keyitems
array ` - The array of items to displayitems-per-page
number ` - Changes how many items per page should be visible.loading
Boolean` - If true and no items are provided,loading-text
string ` - Text shown when loading is true and no items are providedmulti-sort
Boolean` - If true then one can sort on multiple propertiesno-data-text
string` - Text shown when no items are provided to the componentno-results-text
string` - Text shown when search prop is used and there are no resultspage
number ` - The number of pagesearch
string` - Text input used to filter itemsserver-items-length
number` - Used only when data is provided by a server.show-expand
Boolean` - Shows the expand toggle in default rowsshow-select
Boolean` - Shows the select checkboxes in both the header and rows (if using default rows)single-expand
Boolean` - Changes expansion mode to single expandsingle-select
Boolean` - Changes selection mode to single selectsort-by
string | array` - Changes which item property (or properties) should be used for sort order.sort-desc
boolean | array` - Changes which direction sorting is done.Slots
body
- Slot to replace the default tablebody.append
- Appends elements to the end of the default tablebody.prepend
- Prepends elements to the start of the default tableexpanded-item
- Slot to customize expanded rowsfoot
- Slot to add a element after the . Not to be confused with the footer slotheader
- Slot to add aheader.<name>
- Slot to customize a specific header columnheader.data-table-select
- Slot to replace the default va-checkbox in headeritem.<name>
- Slot to customize a specific columnitem.data-table-expand
- Slot to replace the default v-icon used when expanding rowsitem.data-table-select
- Slot to replace the default v-simple-checkbox used when selecting rowsloading
- Defines content for when loading is true and no items are providedno-data
- Defines content for when no items are providedno-results
- Defines content for when search is provided but no results are foundEmits
click:row
- Emits when a table row is clicked.item-expanded
- Event emitted when an item is expanded or closeditem-selected
- Event emitted when an item is selected or deselectedupdate:page
- Event emitted when a page is changedtoggle-select-all
- Event emitted when all items is selected or deselectedupdate:sort-by
update:sort-desc
Refs
<va-data-table>
Current state updatesOptionally accepts the
items
prop.In other words, the
items
prop must always be an array (if provided) of zero-or-more length with each item being an object withstring
keys and values of any type.The
va-data-table
automatically calculates the amount and the titles of columns (if not specified otherwise with thecolumns
prop) based on these object's keys. So e.g. if youritems
looks like:then the resulting table will have a total of 3 columns called A, B and C and will look like this:
When calculating the columns' names based on the item's objects' keys
va-data-table
uses Lodash's startCase internally, so thatmyProp
becomesMy Prop
,userFullName
becomesUser Full Name
and so on.The items' actual values are stringified before being displayed. Falthy values are asserted to the empty string.
Automatically built columns are not always the desirable behavior, so
va-data-table
also optionally accepts thecolumns
prop.In other words, the
columns
prop, if provided, must be an array of either strings or objects implementing theITableColumn
interface.The component internally builds instances similar to (but not exactly to) the
ITableColumn
interface. When a string is provided, its value is used to simultaneously build the columns title (using the same startCase method) and as an indication of which field should be used to render the value of a given row.E.g. if provided
columns
prop looks like this:And the
items
:Then the resulting table would look like follows:
Note how the
columnTrhree
is ignored. If it wasn't for thecolumns
props (if the columns were generated automatically based on theitems
' value, then it would exist).Instead of strings, you can also provide objects of the
ITableColumn
interface. Sometimes it's the only option since only that form allows to enable certain column features (such as sorting).The only mandatory field for the
ITableColumn
s is thekey
. It acts the same way as the string-value of thecolumns
array does.va-data-tables
allows to apply a single table-wise filter with thefilter
prop.When the
filter
is provided, only the rows where at least one cell contains the specified value will be rendered. To runtime-disable filtering (clear the filter), provide an empty string.To use a custom filtering function, provide the
va-data-table
with thefiltering-fn
prop.The function takes the initial value of a currently being checked cell (the
source
formal parameter) and must return a boolean, indicating, whether to include the row containing that cell or not. The following example shows the use of a custom filtering function (which looks for the exact match rather than the standard one which checks for substring-inclusion) when the respective checkbox is checked:va-data-table
emits thefilter
event each time filtering is applied (and when the filter is cleared), passing the number of filtered items as the first (and the only) argument.You can specify which columns should be sortable by providing a column definition object (see the
columns
prop above) with thesortable: true
field.Making a column sortable means allowing to click the column's header to toggle the sorting by that column's values:
You can also provide a custom sorting function for a given column using the
sortingFn
field on the column definition object.The following GIF show runtime-toggling the custom sorting function which sorts the ID column's values as number instead of as strings:
The function takes two cells' initial values (a, b) (note: initial! (i.e. in the form the user provided them, rather than stringified)) and must return a number (-1, 0, +1) indicating whether the two rows shuld be swapped the places or not. See the standard JS's Array.prototype.sort for details.
If you want to runtime-disable the custom function and start making use of the built-in one, pass the
undefined
to thesortingFn
.Each time the table's sorting changes, the
sort
event is thrown, with the following param:...indicating the column's key the table is currently sorted by and the sorting order: ascending, descending and
null
for un-sorted table.va-data-table
also optionally accepts thesortBy
and thesortingOrder
modeled props, which allow users to change sorting settings from-outside and to model the changes introduced to the table's sorting by interacting with the table itself. They also allow to provide initial sorting values. Don't use them without v-models.Selection fully respects filtering and sorting.
Use the
selectable
prop to indicate whether theva-data-table
should have selectable rows or not.We provide 2 selection modes: multiple (default) and single. Single selection, as its name implies, allows for only a single row to be selected at a time, while multiple mode allows to select multiple rows by clicking on checkboxes or using the
ctrl
/shift
keys when clicking rows.When
select-mode
is set tomultiple
, an additional header-checkbox is also rendered, allowing to select/un-select all the rows simultaneously.The highlighting color of the selected row might be changed with the
selected-color
prop, taking a string with one of the possible color options: "primary", "danger" and so on:The selection may optionally be attached to a model. The standard
v-model
(using themodelValue
prop and the@update:modelValue
event). This also allows to set the initial selection on theva-data-table
:Don't use the
:modelValue
prop withoutv-model
.Except for the
update:modelValue
event, theselection-change
event is also thrown each time the selection changes.It provides the following object as its only argument:
...with the currently and previously selected items respectively.
Use the
perPage
andcurrentPage
props to enable pagination.Those 2 indicate the amount of items which should be shown on a page and the number of the page respectively.
The
va-data-table
component is paginator-agnostic. I.e. it can work with any pagination component you'd like. In the example above it uses standard (native) inputs.The component also provide no protection whatsoever for the cases when the
currentPage
is set higher the the actual number of pages, so make sure to check it yourself.loading
(boolean) andloadingColor
props allow to set the loading state for the table (by displaying the spinning wheel loading-indicator).noDataHtml
andnoDataFilteringHtml
respectively set the html-content for the cases when:items
prop at allfilter
prop.hideDefaultHeader
(boolean) indicates whether to show the default headers for columns.footClone
(boolean) - whether to clone the headers into the footer.Has no effect if the default header is hidden with the
hideDefaultHeader
prop.allowFootSorting
- is there's a footer, allow clicking its column headers to sort (and to display the sorting status) the rows.striped
(boolean) - to apply the striped styling to the rows (highlights each 2nd row).Slots
colgroup The contents of this slot is wrapped (if provided) inside the
<colgroup>
tag, allowing to specify certain column options. Bound tocolumns
(not the prop, but their internal representation: TableColumn[]).head.prepend Allows to insert custom rows in table's header. Isn't bound to anything.
head Targets all the table's headings. Is bound to
column
.head(key) Targets a specific table header by the column's key. Is bound to
column
head.append Append custom rows inside table's header.
body.prepend Prepend
<tbody>
with custom rows.cell Targets all the cells. Is bound to the current cell (
TableCell
).cell(key) Allows to target only cells of a specified by the given key column. Is bound to the current cell.
body.append Appends rows to the table's body.
The following footer-related slots (except for foot.prepend and foot.append) will only work if there's a
footClone
prop set to some truthy value.foot.prepend Prepend rows to footer.
foot Target all the headers inside
<tfoot>
foot(key) A speecific header in footer. Is bound to the
column
.foot.append Append rows to the footer.
When binding a bindable head/foot/cell to a column/cell, here's the structure of exposed objects:
Basically, almost the same as the column definition object you pass to the
columns
prop.And here's the TableRow:
Alignment
Should be specified on columns (see the
columns
prop above).Allows to provide different values for headers and for columns' cells. Supported values for horiz. alignment are:
left, center, right
, for vert. -top, middle, bottom
. Pretty much the same as fortext-align
andvertical-align
css properties.