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

data-table-source

v0.0.5

Published

A datasource with filter, sorting and pagination for tables

Downloads

4

Readme

DataTableSource

DataTableSource is a simple JavaScript class designed to manage and filter tabular data easily. It allows you to set data, apply filters, paginate through data, sort data, and retrieve the currently rendered dataset. It contains the JS logic you need for creating dynamic HTML tables.

Installation

npm i data-table-source

Usage

import DataTableSource from 'data-table-source';

const dataArray = [
    {name: 'Dragon Rider', album: 'Archangel', year: 2010, length: 1.53, composer: 'Thomas Bergersen'},
    {name: 'Fire Nation', album: 'Invincible', year: 2010, length: 2.59, composer: 'Nick Phoenix'},
    {name: 'Blackheart', album: 'SkyWorld', year: 2012, length: 4.32, composer: 'Thomas Bergersen'}
    // Add more data as needed
];

// Create an instance with initial data and optional page size
const dataTable = new DataTableSource(dataArray, pageSize);

// Set filters and paginate through data
dataTable.filter = 'berg';
dataTable.nextPage();

// Sort data by column name in ascending order
dataTable.sortData('name', 'asc')

// Reset sorting and filtering
dataTable.resetSorting()
dataTable.resetFilter()

API

Constructor

new DataTableSource(data: Array, pageSize?: number): DataTableSource

Creates a new DataTableSource instance.

  • data: An array of objects representing the tabular data.
  • pageSize (optional): The number of items to display per page.

Throws an error if the input data is not an array of objects or if objects in the array do not have uniform keys.

Properties

data: Array

Setter for the data property.

dataTable.data = newData;

filter: string

Getter and setter for the filter property.

dataTable.filter = 'example';
console.log(dataTable.filter); // 'example'

renderedData: Array

Getter for the renderedData property, which returns the currently rendered data array based on filter, pageSize, and currentPage.

console.log(dataTable.renderedData);

pageSize: number

Getter and setter for the pageSize property. Set pageSize to 0 to deactivate it.

dataTable.pageSize = 10;
console.log(dataTable.pageSize); // 10

currentPage: number

Getter and setter for the currentPage property.

dataTable.currentPage = 2;
console.log(dataTable.currentPage); // 2

sortOrder: string

Getter for the current sort order. Can be 'asc' or 'desc'

Even if this has a value, data can be rendered unsorted. Actively call the sortData-Function to sort data.

console.log(dataTable.sortOrder); // 'asc'

sortKey: string

Getter for the current sort key / column.

console.log(dataTable.sortKey); // 'name'

Methods

getMaxPages(): number

Calculates the maximum number of pages based on the filtered data and page size.

const maxPages = dataTable.getMaxPages();
console.log(maxPages);

nextPage(): void

Moves to the next page if available.

dataTable.nextPage();

previousPage(): void

Moves to the previous page if available.

dataTable.previousPage();

resetFilter(): void

Resets the filter to an empty string.

dataTable.resetFilter();

sortData(key: string, order?: string = 'asc'): void

Sorts the data array by key (column name) in ascending or descending order.

dataTable.sortData('name', 'desc');

resetSorting(): void

Resets the sorting to display the data in its original order

dataTable.resetSorting();