GithubHelp home page GithubHelp logo

shawkenator / table-to-json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lightswitch05/table-to-json

1.0 2.0 0.0 635 KB

Serializes HTML tables into JSON objects.

Home Page: http://lightswitch05.github.io/table-to-json/

License: MIT License

JavaScript 98.68% HTML 1.32%

table-to-json's Introduction

Table To JSON

Build Status

jQuery plugin to serialize HTML tables into javascript objects.

Demo

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
    • 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({
        textExtractor : 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();
        }
      });
    • 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({
        textExtractor : {
          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.

table-to-json's People

Contributors

station384 avatar lightswitch05 avatar baw avatar mottie avatar dayalone avatar imamathwiz avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar Stephen Hawk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.