Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tablesort

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tablesort

A sorting component for HTML tables

  • 2.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
297K
increased by2.94%
Maintainers
1
Weekly downloads
 
Created

What is tablesort?

The tablesort npm package is a lightweight, dependency-free JavaScript library that allows you to add sorting functionality to HTML tables. It is designed to be simple to use and easily customizable.

What are tablesort's main functionalities?

Basic Table Sorting

This feature allows you to add basic sorting functionality to an HTML table. By including the Tablesort library and initializing it with the table element, users can click on the table headers to sort the rows.


<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/tablesort@5.2.1/dist/tablesort.min.js"></script>
</head>
<body>
  <table id="myTable">
    <thead>
      <tr>
        <th>Name</th>
        <th>Age</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Alice</td>
        <td>30</td>
      </tr>
      <tr>
        <td>Bob</td>
        <td>25</td>
      </tr>
    </tbody>
  </table>
  <script>
    new Tablesort(document.getElementById('myTable'));
  </script>
</body>
</html>

Custom Sort Functions

This feature allows you to define custom sort functions for specific columns. In this example, a custom sort function is used to sort dates in the format MM/DD/YYYY.


<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/tablesort@5.2.1/dist/tablesort.min.js"></script>
</head>
<body>
  <table id="myTable">
    <thead>
      <tr>
        <th data-sort-method="custom">Date</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>01/02/2020</td>
      </tr>
      <tr>
        <td>12/31/2019</td>
      </tr>
    </tbody>
  </table>
  <script>
    Tablesort.extend('custom', function(item) {
      return new Date(item.split('/').reverse().join('-')).getTime();
    });
    new Tablesort(document.getElementById('myTable'));
  </script>
</body>
</html>

Sorting with Data Attributes

This feature allows you to use data attributes to specify the sort method for a column. In this example, the 'data-sort-method' attribute is used to indicate that the 'Score' column should be sorted numerically.


<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/tablesort@5.2.1/dist/tablesort.min.js"></script>
</head>
<body>
  <table id="myTable">
    <thead>
      <tr>
        <th>Name</th>
        <th data-sort-method="number">Score</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Alice</td>
        <td data-sort="90">90</td>
      </tr>
      <tr>
        <td>Bob</td>
        <td data-sort="85">85</td>
      </tr>
    </tbody>
  </table>
  <script>
    new Tablesort(document.getElementById('myTable'));
  </script>
</body>
</html>

Other packages similar to tablesort

FAQs

Package last updated on 10 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc