vue-sorted-table
A plugin to turn tables into sorted tables. Supports nested object keys, custom icons and reusable components.
Installation
Install with NPM:
Import globally in app:
Examples
Basic
The basic example shows how to use the SortedTable
and SortLink
components:
The SortedTable
tag requires a values
property, which is an array of objects which contain the data:
The SortLink
tag adds a link to sort the provided data. In the case the name
property value is the current
sorting, the component adds a sort icon, depending on the actual order:
The sorted data is made accessible as a scoped slot.
Therefore the tbody
tag is served as part of the body
slot and has the slot scope sort
:
Now we can access the slot scope via sort
and iterate over the sorted values to render the data:
Advanced
The advanced example is based on the basic example.
It shows how to use the plugin configuration to set global sort icons:
Nested values
By default, the objects containing the values has to be a flat object.
To support nested objects ({ name: "Plugin Foo", user: { id: 1, name: "David Campbell" } }
) the plugin
uses lodash.
At first, install lodash:
Import lodash and register Vue prototype:
Add sort link using the nested key:
Extend v-for
loop to render nested value:
Single File Components
The SortedTable
and SortLink
components can be used without registering the plugin.
Import the components, e.g. as part of a singe file component:
Register components locally:
Add sort icons as property of the SortedTable
tag:
Configuration
The plugin configuration allows to set global sort icons, e.g. Advanced Example
Option | Description |
---|---|
ascIcon |
Ascending sort icon. |
descIcon |
Descending sort icon. |
Components
SortedTable
The SortedTable
is the main component of the plugin. It is intended to be a replacement of the <table></table>
tag.
So instead using the old table tags, use <SortedTable></SortedTable>
.
Properties
This component has the following properties:
Property | Required | Default | Description |
---|---|---|---|
values |
yes | null | Array of objects containing the values which should be sorted. |
dir |
no | asc | Sort direction. Valid values: ("asc"|"desc") |
sort |
no | id | Default sorting. Could be any valid object key. |
ascIcon |
no | Ascending icon. Overwrites default or globally set icon. | |
descIcon |
no | Descending icon. Overwrites default or globally set icon. |
SortLink
This component adds a link to sort the given values. A sort icon is attached automatically to link.
Properties
This component has the following properties:
Property | Required | Default | Description |
---|---|---|---|
name |
yes | The object key name on which the values will be sorted. |
Constraint
Currently, the SortLink
component expects to be a child of the SortedTable
component.
Adding any other component in between will break the sorting capabilities.