Tablesorter - Documentation

Introduction

What is Tablesorter?

Tablesorter is a powerful jQuery plugin that provides sophisticated sorting functionality for HTML tables. It allows you to easily add client-side sorting capabilities to your tables, enabling users to interactively sort table rows based on the data within individual columns. This enhances the usability of your tables, making it easier for users to find and analyze data. Unlike server-side sorting, Tablesorter performs sorting directly in the user’s browser, resulting in faster response times and reduced server load.

Key Features

Browser Compatibility

Tablesorter is designed to work across a wide range of modern web browsers. While it aims for broad compatibility, optimal performance and feature support are best achieved with modern, up-to-date browsers. For optimal results, we recommend using the latest versions of:

Older browsers might exhibit limited support or require additional configuration. Thorough testing across your target browsers is always recommended.

Getting Started: Installation and Setup

  1. Include jQuery: Tablesorter relies on jQuery. Ensure you have included the jQuery library in your HTML file before including the Tablesorter plugin. You can download jQuery from https://jquery.com/ or use a CDN link:

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  2. Include Tablesorter: Download the Tablesorter plugin files (typically jquery.tablesorter.js and jquery.tablesorter.widgets.js) and include them in your HTML after the jQuery inclusion:

    html <script src="jquery.tablesorter.js"></script> <script src="jquery.tablesorter.widgets.js"></script> Alternatively, use a CDN link if available.

  3. Initialize Tablesorter: Select your table element using a jQuery selector and call the tablesorter() method.

    <table id="myTable">
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
            </tr>
        </thead>
        <tbody>
            <tr><td>Data 1</td><td>Data 2</td></tr>
            <tr><td>Data 3</td><td>Data 4</td></tr>
        </tbody>
    </table>
    
    <script>
        $(document).ready(function() {
            $("#myTable").tablesorter();
        });
    </script>

    This basic setup will enable sorting on all columns. For more advanced configurations (e.g., specifying sorting types, using widgets), refer to the detailed documentation and examples provided with the plugin.

Basic Usage

Initializing Tablesorter

The simplest way to initialize Tablesorter is by including the necessary JavaScript files (as described in the “Getting Started” section) and then calling the tablesorter() method on your table element using a jQuery selector. This will automatically enable sorting on all columns based on their data type.

$(document).ready(function() {
    $("#myTable").tablesorter(); 
});

Replace "#myTable" with the appropriate jQuery selector for your table. This code should be placed within a $(document).ready() function to ensure the DOM is fully loaded before initializing the plugin.

Sorting Options

Tablesorter provides several options to customize its behavior. These options are passed as a JavaScript object to the tablesorter() method. Some key options include:

$(document).ready(function() {
    $("#myTable").tablesorter({
        theme: "blue",
        headers: { 
            0: { sorter: false }, // Disable sorting for the first column
            1: { sorter: 'currency' } //Use currency sorter for the second column
        },
        sortList: [[1,1]] //Sort second column ascending initially
    });
});

Default Sorting

By default, Tablesorter attempts to intelligently determine the data type of each column and applies the appropriate sorting algorithm. It uses a series of built-in parsers to handle common data types like numbers, dates, and strings. For numeric and date columns, sorting is typically handled correctly without any additional configuration. String columns are sorted lexicographically (case-insensitive by default).

Customizing Sorting

You can customize the sorting behavior for specific columns using the headers option. This allows you to specify a custom sorting function or use a predefined sorter.

$(document).ready(function() {
    $("#myTable").tablesorter({
        headers: {
            2: { sorter: 'text' } //Case-sensitive text sorting for column 3
        }
    });
});

To define a completely custom sorting function, you provide a function that takes two arguments (the values to compare) and returns -1, 0, or 1 depending on the sort order.

$(document).ready(function() {
    $("#myTable").tablesorter({
        headers: {
            3: { sorter: function(a, b) {
                    // Custom sorting logic here
                    return a.localeCompare(b);
                  }
            }
        }
    });
});

Header Customization

You can control which columns are sortable and modify their header appearance using the headers option. Setting a header’s sorter property to false disables sorting for that column. You can also use CSS to style the headers. Tablesorter adds classes to the header cells to indicate the sort order (e.g., headerSortUp, headerSortDown, headerSort).

Handling Data Types

Tablesorter includes built-in parsers to automatically handle various data types. These parsers ensure correct sorting for numbers, dates, currencies, and more. However, for complex or unusual data types, you may need to create custom parsers. The plugin documentation provides details on creating and registering custom parsers. Common data types are typically handled correctly without explicit parser configuration.

Advanced Usage

Multiple Tables

Tablesorter can be used on multiple tables on a single page. Simply call the tablesorter() method on each table element using its appropriate jQuery selector. Ensure that each table has a unique ID or class for proper selection.

$(document).ready(function() {
    $("#table1").tablesorter();
    $("#table2").tablesorter();
});

Nested Tables

Tablesorter directly supports sorting only on the outermost tables. Nested tables (tables within table cells) will not be sorted by the plugin. To handle nested tables, you would need to implement a custom solution, potentially involving iterating through the nested tables and applying sorting logic individually.

Sorting by Multiple Columns

Users can sort by multiple columns simultaneously by clicking on the column headers. The order in which columns are clicked determines the sort order. To specify an initial multi-column sort order, use the sortList option:

$(document).ready(function() {
    $("#myTable").tablesorter({
        sortList: [[0, 1], [1, 0]] //Sort column 1 ascending, then column 2 descending
    });
});

This initializes the table sorted by column 1 ascending and then column 2 descending.

Filtering

The filter widget allows users to filter table rows based on the text content of each cell. Enable the widget by including it in the widgets option:

$(document).ready(function() {
    $("#myTable").tablesorter({
        widgets: ["filter"]
    });
});

This adds a filter input field above each column header, allowing users to filter rows based on the column contents. Consult the Tablesorter documentation for details on widget options and customization.

Pagination

The pager widget provides pagination functionality for large tables. Enable the widget similarly to the filter:

$(document).ready(function() {
    $("#myTable").tablesorter({
        widgets: ["pager"]
    });
});

You will also need to include appropriate HTML elements (e.g., for page navigation) as described in the plugin’s documentation.

Widgets

Tablesorter’s functionality is greatly extended by widgets. Widgets add features like filtering, pagination, and styling enhancements. Enable widgets by including their names in the widgets option array. See the plugin’s documentation for a complete list of available widgets and their configurations.

Custom Parser Functions

For data types not automatically handled by the built-in parsers, you can create custom parser functions. These functions define how data in a column should be interpreted for sorting. Refer to the Tablesorter documentation for details on creating and registering custom parsers. This involves defining a function that takes a cell’s content as input and returns a value suitable for comparison.

Using Themes

Tablesorter supports various CSS themes to customize the table’s appearance. Select a theme using the theme option:

$(document).ready(function() {
    $("#myTable").tablesorter({
        theme: "bootstrap" // or "blue", "jui", etc.
    });
});

You’ll need the corresponding theme CSS file included in your project.

Accessibility

Ensure your tables are accessible by following accessibility best practices. Use appropriate ARIA attributes (like aria-sort on headers) to improve screen reader compatibility. Tablesorter generally supports accessibility features, but proper implementation of ARIA attributes and semantic HTML is crucial.

Internationalization

For internationalization support (handling different languages and locales), you might need to use a suitable date/time library and potentially create custom parsers to handle locale-specific date and number formats. Consider using the localeCompare method for string comparisons to handle language-specific sorting. The plugin itself doesn’t include built-in internationalization features beyond the default capabilities of JavaScript’s built-in functions.

API Reference

Methods

Tablesorter exposes several methods for interacting with the plugin after initialization. These methods are called on the jQuery object representing the table. Some key methods include:

Example:

$("#myTable").tablesorter().tablesorter("addRows", [ "<tr><td>New Row 1</td><td>Data</td></tr>" ]);
$("#myTable").tablesorter("sortOn", 0, 0); //Sort the first column ascending
let sortOrder = $("#myTable").tablesorter("getSortList");
console.log(sortOrder); // Output current sort order

Consult the full Tablesorter documentation for a complete list of available methods and their parameters.

Events

Tablesorter triggers several events throughout its lifecycle. These events can be used to perform custom actions based on sorting or other plugin operations. These are jQuery events, so you can use jQuery’s on() method to listen for them. Some important events include:

Example:

$("#myTable").on("sortEnd", function(e, table) {
    console.log("Sorting complete!");
});

Options

The options passed to the tablesorter() method control various aspects of the plugin’s behavior. These options were discussed partially in the Basic Usage and Advanced Usage sections. Refer to the full Tablesorter documentation for the complete list of options and their descriptions. Options are key-value pairs within a JavaScript object. Some of the commonly used options were already illustrated previously (e.g., theme, headers, widgets, sortList).

Public Properties

Tablesorter doesn’t expose many public properties directly accessible from outside the plugin. Information like the current sort order is usually accessed using methods like getSortList(). The plugin’s internal data structure is not intended for direct manipulation. The primary way to interact with the plugin is through its methods and events. Avoid directly accessing or modifying internal plugin properties, as this might break functionality and is not supported.

Troubleshooting

Common Issues and Solutions

This section addresses frequently encountered problems when using Tablesorter.

Debugging Tips

Error Messages

Tablesorter generally doesn’t throw many custom error messages. Most errors will manifest as JavaScript errors in your browser’s console (e.g., “Uncaught TypeError,” “Uncaught ReferenceError”). These errors will usually indicate problems with the inclusion of necessary files, incorrect use of the API, or conflicts with other JavaScript libraries.

Examine the error messages in your browser’s console carefully. They usually provide clues to the source and nature of the problem, including the line number of the problematic code. If the error message is unclear, try the debugging tips described above to pinpoint the cause. If you are unable to resolve the issue, check the Tablesorter’s issue tracker or community forums for assistance.

Contributing

We welcome contributions to Tablesorter! Whether you’re fixing bugs, adding features, or improving the documentation, your help is valuable. Here’s how you can contribute:

Reporting Issues

Before submitting a new issue, please check if a similar issue already exists. Search the issue tracker to see if someone has reported the same or a related problem.

When reporting a new issue, please provide the following information:

Submitting Pull Requests

  1. Fork the repository: Create a fork of the official Tablesorter repository on GitHub.

  2. Create a new branch: Create a new branch for your changes. Use a descriptive branch name that reflects the changes you’re making (e.g., “fix-bug-123,” “add-feature-xyz”).

  3. Make your changes: Make your code changes, ensuring they adhere to the coding style guidelines (see below). Thoroughly test your changes before submitting the pull request.

  4. Commit your changes: Commit your changes with clear and concise commit messages.

  5. Push your branch: Push your branch to your forked repository.

  6. Create a pull request: Create a pull request on GitHub, linking your branch to the main repository’s branch. Provide a clear description of your changes in the pull request.

Coding Style Guidelines

To maintain consistency and readability, please adhere to the following coding style guidelines when contributing to Tablesorter:

Before submitting a pull request, ensure that your code passes all existing tests and follows the coding style guidelines. We may request changes to your code before merging it into the main repository.