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

table-to-json

v1.0.0

Published

jQuery plugin that reads an HTML table and returns a javascript object representing the values and columns of the table

Downloads

298

Readme

Table To JSON

Build Status license

jQuery plugin to serialize HTML tables into javascript objects.

Links

  • Demo: https://lightswitch05.github.io/table-to-json/
  • Plunker Template: https://plnkr.co/edit/iQFtcEEZkvsMJ2UqcrlW?p=preview

CDN

It is recommended to pull this tool into your project directly. But if you insist to use a CDN, here is one:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/jquery.tabletojson.min.js" integrity="sha256-H8xrCe0tZFi/C2CgxkmiGksqVaxhW0PFcUKZJZo1yNU=" crossorigin="anonymous"></script>

Features

  • Automatically finds column headings
    • Override found column headings by using data-override="overridden column name"
    • Always uses first row as column headings regardless of th and td tags
  • Override cell values column names by using data-override="new value"
  • Ignorable columns
  • Not confused by nested tables
  • Works with rowspan and colspan

Options

  • ignoreColumns
    • Array of column indexes to ignore.
    • Default: []
  • onlyColumns
    • Array of column indexes to include, all other columns are ignored. This takes presidence over ignoreColumns when provided.
    • Default: null - all columns
  • ignoreHiddenRows
    • Boolean if hidden rows should be ignored or not.
    • Default: true
  • ignoreEmptyRows
    • Boolean if empty rows should be ignored or not.
    • Default: false
  • headings
    • Array of table headings to use. When supplied, treats entire table as values including the first <tr>
    • Default: null
  • allowHTML
    • Boolean if HTML tags in table cells should be preserved
    • Default: false
  • includeRowId
    • Either a boolean or a string. If true, the the id attribute on the table's <tr> elements will be included in the JSON as rowId. To override the name rowId, supply a string of the name you would like to use.
    • Default: false
  • textDataOverride
    • String containing data-attribute which contains data which overrides the text contained within the table cell
    • Default: 'data-override'
  • textExtractor
    • alias of extractor
  • extractor
    • Function : function that is used on all tbody cells to extract text from the cells; a value in data-override will prevent this function from being called. Example:

      $('table').tableToJSON({
        extractor : function(cellIndex, $cell) {
          // get text from the span inside table cells;
          // if empty or non-existant, get the cell text
          return $cell.find('span').text() || $cell.text();
        }
      });
      $('table').tableToJSON({
        extractor : function(cellIndex, $cell) {
          return {
            name: $cell.find('span').text(),
            avatar: $cell.find('img').attr('src')
          };
        }
      });
    • Object : object containing a zero-based cell index (this does not take colspan cells into account!) of the table; a value in data-override will prevent this function from being called. Example:

      $('table').tableToJSON({
        extractor : {
          0 : function(cellIndex, $cell) {
            return $cell.find('em').text();
          },
          1 : function(cellIndex, $cell) {
            return $cell.find('span').text();
          }
        }
      });
    • Default: null

Example

<table id='example-table'>
  <thead>
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th data-override="Score">Points</th></tr>
  </thead>
  <tbody>
    <tr>
      <td>Jill</td>
      <td>Smith</td>
      <td data-override="disqualified">50</td></tr>
    <tr>
      <td>Eve</td>
      <td>Jackson</td>
      <td>94</td></tr>
    <tr>
      <td>John</td>
      <td>Doe</td>
      <td>80</td></tr>
    <tr>
      <td>Adam</td>
      <td>Johnson</td>
      <td>67</td></tr>
  </tbody>
</table>

<script type="text/javascript">
  // Basic Usage
  var table = $('#example-table').tableToJSON();
  // table == [{"First Name"=>"Jill", "Last Name"=>"Smith", "Score"=>"disqualified"},
  //           {"First Name"=>"Eve", "Last Name"=>"Jackson", "Score"=>"94"},
  //           {"First Name"=>"John", "Last Name"=>"Doe", "Score"=>"80"},
  //           {"First Name"=>"Adam", "Last Name"=>"Johnson", "Score"=>"67"}]

  // Ignore first column (name)
  var table = $('#example-table').tableToJSON({
        ignoreColumns: [0]
  });
  // table == [{"Last Name"=>"Smith", "Score"=>"disqualified"},
  //           {"Last Name"=>"Jackson", "Score"=>"94"},
  //           {"Last Name"=>"Doe", "Score"=>"80"},
  //           {"Last Name"=>"Johnson", "Score"=>"67"}]
</script>

Contributing

  • Install Node.js.
    • this will also the npm package manager.
  • run npm install from app root directory.
    • This installs grunt and other dependencies See package.json for a full list.
  • run npm install -g grunt-cli.
  • run grunt to run tests and create a new build in /lib.
  • Make the changes you want.
  • Make tests for the changes.
  • Submit a pull request, please submit to the develop branch.

Looking for a server-side solution?

Colin Tremblay is working on a PHP implementation at HTML-Table-To-JSON

Special Thanks

  • imamathwiz for adding allowHTML option and various other changes.
  • nenads for adding headings option.
  • Mottie for adding rowspan & colspan support. Also adding the textExtractor & dataOverride feature!
  • station384 for adding includeRowId support.
  • dayAlone for adding ignoreEmptyRows option.
  • danielapsmaior for discovering and fixing a rowspan & colspan bug.
  • koshuang for adding extractor feature!
  • noma4i added feature "Skip columns where headers are not present"
  • cn-tools for reporting AND fixing a bug when using both ignoreEmptyRows and ignoreColumns options