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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@riversun/sortable-table

v1.1.2

Published

Library template for both node and browser using ES6,Webpack,Babel,Jest,ESLint

Downloads

45

Readme

@riversun/sortable-table

npm version License: MIT CircleCI Codacy Badge Coverage Status

This library makes tables sortable.

  • Make the table sortable

  • Wroks with plain HTML, Bootstrap, Vue, React and etc.

  • No external library required, works as a standalone, no dependency.

  • A small size library , 13KB including images.

Demo

How to install

  • NPM
npm install @riversun/sortable-table

And import like,

import SortableTable from '@riversun/sortable-table';

or

  • use <script> tag from CDN
<script src="https://cdn.jsdelivr.net/npm/@riversun/sortable-table/lib/sortable-table.js"></script>

Usage

HTML and code

Here is the typical code for sortable-table.

Use thead tr th for the table header element. Specify the data-id attribute for the header element th. The data-id attribute is linked to the key in data.

<!DOCTYPE html>
<html lang="en">
<body>
<div class="sortable-table">
   <table id="my-table1">
       <thead>
       <tr>
           <th data-id="id" data-header>
               <div>#</div>
           </th>
           <th data-id="name" sortable>
               name
           </th>
           <th data-id="price" sortable>
               price($)
           </th>
           <th data-id="weight" sortable>
               weight(carat)
           </th>
       </tr>
       </thead>
   </table>
</div>

<script src="https://cdn.jsdelivr.net/npm/@riversun/sortable-table/lib/sortable-table.js"></script>
<script>

 const data = [
   {
     id: 0,
     name: 'Diamond',
     weight: 1.0,
     price: 9000,
   },
   {
     id: 1,
     name: 'Amethyst',
     weight: 3.0,
     price: 200,
   },
   {
     id: 2,
     name: 'Emerald',
     weight: 2.5,
     price: 2500,
   },
   {
     id: 3,
     name: 'Ruby',
     weight: 2.0,
     price: 2000,
   },
 ];

 const sortableTable = new SortableTable();

 // set table element
 sortableTable.setTable(document.querySelector('#my-table1'));
 // set data to be sorted
 sortableTable.setData(data);
 // handling events
 sortableTable.events()
     .on('sort', (event) => {
       console.log(`[SortableTable#onSort]
     event.colId=${event.colId}
     event.sortDir=${event.sortDir}
     event.data=\n${JSON.stringify(event.data)}`);
     });

</script>
</body>
</html>

Custom rendering cells

You can customize cell-rendering by specifying rendering function with #setCellRenderer like as follows.

Run on PEN

<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
          integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
          crossorigin="anonymous">
    <style>
        body {
            padding-top: 60px;
        }
    </style>
</head>
<body>

<header>
    <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
        <a class="navbar-brand" href="#">Sortable-table on Bootstrap</a>
    </nav>
</header>


<div class="container-fluid">


    <div class="row">

        <div class="p-1 col-sm-12 sortable-table">

            <table class="table table-hover table-bordered" id="my-table1">
                <thead>
                <tr>
                    <th scope="col" data-id="id" data-header>
                        <div>#</div>
                    </th>
                    <th scope="col" data-id="name" sortable>
                        name
                    </th>
                    <th scope="col" data-id="url" sortable>
                        url
                    </th>
                </tr>
                </thead>
            </table>
        </div>
    </div>
    <div class="row">
        <div class="p-1 col-sm-12">
            <h5>Sort by code</h5>
        </div>
    </div>
    <div class="row">
        <div class="p-1 col-sm-12 d-flex justify-content-around">
            <button id="btnSortName" class="btn btn-primary m-1 w-100">Sort by Name(toggle)</button>
            <button id="btnSortPrice" class="btn btn-primary m-1 w-100">Sort by Price(asc)</button>
            <button id="btnSortWeight" class="btn btn-primary m-1 w-100">Sort by Weight(desc)</button>
            <button id="btnReset" class="btn btn-primary m-1 w-100">Reset</button>
        </div>
    </div>

</div>


<script src="sortable-table.js"></script>
<script>

  const data = [
    {
      id: 0,
      name: 'Apple',
      url: 'https://www.apple.com'
    },
    {
      id: 1,
      name: 'Amazon',
      url: 'https://www.amazon.com'
    },
    {
      id: 2,
      name: 'Google',
      url: 'https://www.google.com'
    },
    {
      id: 3,
      name: 'Facebook',
      url: 'https://www.facebook.com'
    },
  ];

  const sortableTable = new SortableTable();
  // set table element
  sortableTable.setTable(document.querySelector('#my-table1'));

  // set callback function for table cell custom rendering
  sortableTable.setCellRenderer((col, row) => {
    const colValue = row[col.id];
    // cell-is-a-header
    if (col.isHeader) {
      if (typeof colValue !== 'undefined') {
        return `<th>${colValue}</th>`;
      }
      return '<th></th>';
    }
    // cell-is-not-a-header
    if (typeof colValue !== 'undefined') {
      if (col.id === 'url') {
        return `<td><a href="${colValue}" target="_blank">${colValue}</a></td>`;
      }
      return `<td>${colValue}</td>`;
    }
    return '<td></td>';
  });
  // set data to be sorted
  sortableTable.setData(data);


  // handling events
  sortableTable.events()
    .on('sort', (event) => {
      console.log(`[SortableTable#onSort]
      event.colId=${event.colId}
      event.sortDir=${event.sortDir}
      event.data=\n${JSON.stringify(event.data)}`);
    });

  // button handlers
  document.querySelector('#btnSortName')
    .addEventListener('click', () => {
      sortableTable.sort('name', 'toggle');//'asc','desc','toggle'
    });
  document.querySelector('#btnSortPrice')
    .addEventListener('click', () => {
      sortableTable.sort('price', 'asc');
    });
  document.querySelector('#btnSortWeight')
    .addEventListener('click', () => {
      sortableTable.sort('weight', 'desc');
    });
  document.querySelector('#btnReset')
    .addEventListener('click', () => {
      sortableTable.resetData();
    });


</script>
</body>
</html>

APIs and Methods

Open Classes and Methods