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

html-table-generator

v1.0.2

Published

A simple module for creating HTML tables with custom styles, sorting, filtering, and exporting to CSV.

Downloads

14

Readme

HTMLTableGenerator

HTMLTableGenerator is a flexible Node.js module for creating customizable HTML tables. With its easy-to-use interface, you can generate tables from dynamic data, control styles, sort and filter rows, and even export the table data to CSV.

Features

  • Dynamic HTML table creation from JSON data
  • Customizable table styles including header background color, header text color, cell border color, cell padding, cell font weight, and cell color
  • Pre-defined themes for quick and easy styling
  • Ability to sort rows by a specified column in ascending or descending order
  • Ability to filter rows by a specified column and value
  • Export data to a CSV file

Installation

Use the package manager npm to install HTMLTableGenerator.

npm install html-table-generator

Usage

You can use this package in your JavaScript code like this:

const HTMLTableGenerator = require('html-table-generator');

// Example data
const data = {
  title: 'My Table',
  header: ['Column 1', 'Column 2'],
  content: [
    { 'Column 1': 'Row 1', 'Column 2': 'Row 1' },
    { 'Column 1': 'Row 2', 'Column 2': 'Row 2' }
  ]
};

// Use a pre-defined theme or specify custom styles
const themeOrOptions = 'dark';  // Or use custom styles like in the commented code below
// const options = {
//   titleHeaderBgColor: '#ffffff',
//   titleTextColor: '#000000',
//   headerBgColor: '#ffffff',
//   headerTextColor: '#000000',
//   tdPadding: '10px',
//   tdFontWeight: 'bold',
//   tdColor: '#000000'
// };

// Example conditional styles
const conditionalStyles = [
  {
    targetColumn: 'Column 1',
    dependsOnColumn: 'Column 2',
    rule: value => value === 'Row 2',
    style: 'background-color: red'
  }
];

// Create a new table
const table = new HTMLTableGenerator(data, options);

// Add a new column based on an existing column
table.addColumn('Column 3', row => row['Column 1'] + ' - ' + row['Column 2']);

// Add a new column that doesn't depend on existing columns
table.addColumn('Default Value', () => 'N/A');

// Get the HTML string
const html = table.createTableHTML();

// Sort rows by 'Column 1'
table.sortRows('Column 1');

// Sort rows by 'Column 1' in descing order
table.sortRows('Column 1', 'desc');

// Filter rows by 'Column 1' value 'Row 2'
table.filterRows('Column 1', 'Row 2');

// Export the filtered and sorted data to a CSV file
table.exportToCSV('myData.csv');

Please ensure that you replace the data and themeOrOptions variables with your actual data and preferred styles or themes.

Themes

HTMLTableGenerator comes with a set of pre-defined themes. You can use these themes for a quick start. Available themes are "dark", "light", "ocean", "forest", "sunset", "cloud", "vintage". To use a theme, simply provide the theme name as the second argument when creating a new HTMLTableGenerator.

Error Handling

In case the validation of the data or options fails, the constructor and the validateInput method throw an error with a message specifying the requirement that was not fulfilled.

Configuration

The constructor of the HTMLTableGenerator class accepts two arguments: a data object, an options object, and a conditionalStyles array. The data object should include a title, a header array and a content array. The options object should include style specifications for the table and its elements. The conditionalStyles array may include specific styles depending on some conditions of some columns values.

Limitations

  • The package currently only supports creating HTML tables and exporting data to CSV. Exporting to other formats (like PDF or Excel) is not supported.
  • It assumes that the provided data is well-structured and the content matches the headers.