jQuery Plugin For Converting Table Into JSON Data - Table to JSON

File Size: 17.1 KB
Views Total: 25004
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
jQuery Plugin For Converting Table Into JSON Data - Table to JSON

Table to JSON is a simple jQuery plugin that allows to convert HTML tables into javascript objects for further use.

See also:

Basic Usage:

1. Create a standard html table and a button to trigger the converter

<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>
<button id="convert-table">Convert!</button>

2. Include jQuery library and jQuery Table to JSON plugin on the page

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="javascripts/jquery.tabletojson.min.js"></script>

3. The javascript

$('#convert-table').click( function() {
  var table = $('#example-table').tableToJSON();
  console.log(table);
  alert(JSON.stringify(table));  
});

4. Options.

  • ignoreColumns: []: Array of column indexes to ignore.
  • onlyColumns: null: Array of column indexes to include, all other columns are ignored. This takes presidence over ignoreColumns when provided. This takes presidence over ignoreColumns when both are provided.
  • ignoreHiddenRows: true: Boolean if hidden rows should be ignored or not.
  • headings: null: Array of column headings to use. When supplied, all table rows are treated as values.
  • allowHTML: false: Boolean if HTML tags in table cells should be preserved
  • includeRowId : false: 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.

Change log:

0.9.0 (2014-04-07)

  • Added includeRowId option.

0.8.0 (2014-04-07)

  • Added colspan & rowspan support.
  • Fixed missing license banner.
  • Updated Grunt plugins.
  • Added more JSHint requirements and cleaned code.

This awesome jQuery plugin is developed by lightswitch05. For more Advanced Usages, please check the demo page or visit the official website.