npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-tablesort

v2.0.2

Published

A Svelte 3 component that makes it possible to sort a table by clicking the headers.

Downloads

66

Readme

svelte-tablesort

A Svelte 3 component that makes it possible to sort a table by clicking the headers.

Usage

npm install svelte-tablesort

This code shows an html-table where the user can sort the rows by clicking the headers at the top of each column:

<script>
	import {TableSort} from 'svelte-tablesort'

	let items = []

	const dataPromise = fetch(`https://node-hnapi.herokuapp.com/news?page=1`)
		.then(r => r.json())
		.then(data => {
			items = data
		});
</script>

<TableSort items={items}>
	<tr slot="thead">
		<th data-sort="title">Title</th>
		<th data-sort="user">User</th>
	</tr>
	<tr slot="tbody" let:item={item}>
		<td><a href="{item.url}">{item.title}</a></td>
		<td>{item.user}</td>
	</tr>
</TableSort>

Clicking once on a heading sorts in ascending order on the value for that column. Clicking more than once toggles between ascending and descending order. To sort on "user" first and then "title" in the sample above, click the "User" header first, then hold down shift and click "Title".

The items property should be an array of objects.

The data-sort property should contain a key that has a value in all objects in the array. When the user clicks that th-element, the objects will be sorted on that key. Note that it is possible to have one key in the data-sort property and display a different expression in the table. This is useful if the value shown to the user is difficult to sort on. For example, the column can contain an age ("now", "3 minutes ago", "1 hour ago"), but the data-sort expression can reference a timestamp.

The sorting is done with a numeric comparison if the value in the object is a number and localeCompare otherwise. If one object contains a number and another contains a string in the same field, strange things will happen.

Slots

The TableSort component uses three different slots, thead, tbody and tfoot. thead and tbody are mandatory and tfoot is optional. They should all contain a single tr element. The thead and tfoot slots will be instantiated once and the tbody slot will be instantiated once per entry in the items array.

Setting initial sort order

It is possible to indicate that a table should be sorted on one or more columns by default by adding a data-sort-initial property to the th element:

<th data-sort="title" data-sort-initial>Title</th>

This will sort on that column in ascending order. To sort in descending order by default, set the property to descending:

<th data-sort="time" data-sort-initial="descending">Time ago</th>

Styling

The TableSort component provides only a single css rule: It sets the mouse to a "pointer" over a column heading that provides sorting to indicate that it can be clicked. Other than that, you have to style the table yourself.

The TableSort component adds the following css classes:

  • tablesort is added to the table-element.
  • sortable is added to all th-elements that can be sorted on.
  • descending is set on a th-element if the table is sorted on that column in descending order.
  • ascending is set on a th-element if the table is sorted on that column in ascending order.

Example

For a more complete example, see src/routes/index.svelte It can be run by cloning this repository and running npm install && npm run dev

Publishing package

npm run package
cd package
npm publish