Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new Data Table Column Sort feature
- Loading branch information
Showing
8 changed files
with
162 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<DataTable | ||
sortable | ||
bind:sort | ||
bind:sortDirection | ||
on:MDCDataTable:sorted={handleSort} | ||
table$aria-label="User list" | ||
style="width: 100%;" | ||
> | ||
<Head> | ||
<Row> | ||
<!-- | ||
Note that whatever you supply to "columnId" is | ||
appended with "-status-label" and used as an ID | ||
for the hidden label that describes the sort | ||
status to screen readers. | ||
You can localize those labels with the | ||
"sortAscendingAriaLabel" and | ||
"sortDescendingAriaLabel" props on the DataTable. | ||
--> | ||
<Cell numeric columnId="id"> | ||
<!-- For numeric columns, icon comes first. --> | ||
<IconButton class="material-icons">arrow_upward</IconButton> | ||
<Label>ID</Label> | ||
</Cell> | ||
<Cell columnId="name" style="width: 100%;"> | ||
<Label>Name</Label> | ||
<!-- For non-numeric columns, icon comes second. --> | ||
<IconButton class="material-icons">arrow_upward</IconButton> | ||
</Cell> | ||
<Cell columnId="username"> | ||
<Label>Username</Label> | ||
<IconButton class="material-icons">arrow_upward</IconButton> | ||
</Cell> | ||
<Cell columnId="email" l> | ||
<Label>Email</Label> | ||
<IconButton class="material-icons">arrow_upward</IconButton> | ||
</Cell> | ||
<!-- You can turn off sorting for a column. --> | ||
<Cell sortable={false}>Website</Cell> | ||
</Row> | ||
</Head> | ||
<Body> | ||
{#each items as item (item.id)} | ||
<Row> | ||
<Cell numeric>{item.id}</Cell> | ||
<Cell>{item.name}</Cell> | ||
<Cell>{item.username}</Cell> | ||
<Cell>{item.email}</Cell> | ||
<Cell>{item.website}</Cell> | ||
</Row> | ||
{/each} | ||
</Body> | ||
</DataTable> | ||
|
||
<script> | ||
import DataTable, { Head, Body, Row, Cell, Label } from '@smui/data-table'; | ||
import IconButton from '@smui/icon-button'; | ||
let items = []; | ||
let sort = 'id'; | ||
let sortDirection = 'ascending'; | ||
if (typeof fetch !== 'undefined') { | ||
fetch('https://jsonplaceholder.typicode.com/users') | ||
.then((response) => response.json()) | ||
.then((json) => (items = json)); | ||
} | ||
function handleSort() { | ||
items.sort((a, b) => { | ||
const [aVal, bVal] = [a[sort], b[sort]][ | ||
sortDirection === 'ascending' ? 'slice' : 'reverse' | ||
](); | ||
if (typeof aVal === 'string') { | ||
return aVal.localeCompare(bVal); | ||
} | ||
return aVal - bVal; | ||
}); | ||
items = items; | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters