├── .editorconfig ├── .gitignore ├── .jshintrc ├── .travis.yml ├── CHANGELOG.md ├── Gruntfile.js ├── MIT-LICENSE ├── Makefile ├── README.md ├── bower.json ├── docker-compose.yml ├── docs ├── images │ ├── bg_hr.png │ ├── blacktocat.png │ ├── forkme_right_gray_6d6d6d.png │ ├── icon_download.png │ └── sprite_download.png ├── index.html ├── jquery.tabletojson.min.js ├── main.css └── main.js ├── lib ├── jquery.tabletojson.js └── jquery.tabletojson.min.js ├── package.json ├── src ├── tabletojson-cell.js ├── tabletojson-row.js └── tabletojson.js ├── tabletojson.jquery.json └── test ├── index.html ├── specs-legacy ├── content.test.js └── core.test.js └── specs ├── content.test.js └── core.test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # https://EditorConfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | end_of_line = lf 7 | insert_final_newline = true 8 | charset = utf-8 9 | indent_style = space 10 | indent_size = 2 11 | trim_trailing_whitespace = true 12 | 13 | [Makefile] 14 | indent_style = tab 15 | indent_size = 4 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise" : true, 3 | "camelcase" : true, 4 | "curly" : true, 5 | "eqeqeq" : true, 6 | "es3" : true, 7 | "eqnull" : true, 8 | "forin" : true, 9 | "freeze" : true, 10 | "immed" : true, 11 | "indent" : 2, 12 | "jquery" : true, 13 | "latedef" : true, 14 | "newcap" : true, 15 | "noarg" : true, 16 | "noempty" : true, 17 | "nonbsp" : true, 18 | "nonew" : true, 19 | "quotmark" : "single", 20 | "sub" : true, 21 | "trailing" : true, 22 | "undef" : true, 23 | "unused" : true 24 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.12" 4 | - "0.11" 5 | - "0.10" -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 2020-01-22 - Release 1.0.0 2 | * Greatly improved `colspan` & `rowspan` support. 3 | * When using `includeRowId`, `rowid` is now set to `null` if the row does not have an ID set (previously gave an empty string). 4 | * More thorough tests. 5 | * More modular codebase. 6 | 7 | ## 2020-01-17 - Release 0.13.1 8 | * `ignoreEmptyRows` feature should consider `ignoreColumns` when deciding what is considered an empty row. 9 | 10 | ## 2017-07-14 - Release 0.13.0 11 | * Skip columns where headers are not present. 12 | 13 | ## 2017-05-10 - Release 0.12.0 14 | * Added `extractor` option. 15 | 16 | ## 2015-06-05 - Release 0.11.1 17 | * Fixed `colspan` & `rowspan` support for complicated tables. 18 | 19 | ## 2015-02-06 - Release 0.11.0 20 | * Added `textExtractor` option. 21 | 22 | ## 2014-11-21 - Release 0.10.0 23 | * Added `ignoreEmptyRows` option. 24 | 25 | ## 2014-9-24 - Release 0.9.0 26 | * Added `includeRowId` option. 27 | 28 | ## 2014-4-14 - Release 0.8.3 29 | * Updated to correct version number. 30 | 31 | ## 2014-4-14 - Release 0.8.2 32 | * Updated `onlyColumns` & `ignoreColumns` to work with colspans ([demo](http://jsfiddle.net/Mottie/4E2L6/10/)). 33 | 34 | ## 2014-4-13 - Release 0.8.1 35 | * Added basic `.gitattributes` file. 36 | * Updated `colspan` & `rowspan` support to work when applied to the same cell ([demo](http://jsfiddle.net/Mottie/4E2L6/9/)). 37 | 38 | ## 2014-4-6 - Release 0.8.0 39 | * Added `colspan` & `rowspan` support. 40 | * Fixed missing license banner. 41 | * Updated Grunt plugins. 42 | * Added more JSHint requirements and cleaned code. 43 | 44 | ## 2014-3-26 - Release 0.7.0 45 | * Added `allowHTML` option to preserve HTML tags from table cells. 46 | * Fixed Travis & Grunt tests. 47 | 48 | ## 2013-07-25 - Release 0.6.0 49 | * Added `headings` option to define the headings of a table. When supplied, treats entire table as values. 50 | 51 | ## 2013-04-23 - Release 0.5.1 52 | * Halved execution time. 53 | * Added more JSHint requirements. 54 | 55 | ## 2013-04-15 - Release 0.5.0 56 | * **The release breaks backwards compatibility for both option names and data-* attributes.** 57 | * Changed option `ignoreColNum` to `ignoreColumns`. 58 | * Merged `data-cell-value` and `data-column-name` into a single attribute: `data-override`. 59 | * Added a new option `onlyColumns` to set which columns are included and ignores all others. 60 | 61 | ## 2013-04-12 - Release 0.4.0 62 | * No longer requires the use of `th` elements - always uses the first row as column names. 63 | 64 | ## 2013-02-15 - Release 0.3.0 65 | * Added tests and fixed many bugs. 66 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | /*global module:false*/ 2 | module.exports = function(grunt) { 3 | 'use strict'; 4 | 5 | // Project configuration. 6 | grunt.initConfig({ 7 | pkg: grunt.file.readJSON( 'package.json' ), 8 | 9 | clean: { 10 | build: [ 'lib' ] 11 | }, 12 | 13 | concat: { 14 | options: { 15 | banner: '/**\n' + 16 | ' * <%= pkg.name %>\n' + 17 | ' * <%= pkg.description %>\n' + 18 | ' *\n' + 19 | ' * @author <%= pkg.author.name %>\n' + 20 | ' * @copyright <%= pkg.author.name %> <%= grunt.template.today("yyyy") %>\n' + 21 | ' * @license <%= pkg.licenses[0].type %> <<%= pkg.licenses[0].url %>>\n' + 22 | ' * @link <%= pkg.homepage %>\n' + 23 | ' * @module <%= pkg.name %>\n' + 24 | ' * @version <%= pkg.version %>\n' + 25 | ' */\n' 26 | }, 27 | dist: { 28 | files: { 29 | 'lib/jquery.tabletojson.js': 30 | ['src/tabletojson-cell.js', 31 | 'src/tabletojson-row.js', 32 | 'src/tabletojson.js'] 33 | } 34 | } 35 | }, 36 | jshint: { 37 | files: { 38 | src: [ 39 | 'Gruntfile.js', 40 | 'src/**/*.js', 41 | 'test/specs/*.js' 42 | ] 43 | }, 44 | options: { 45 | jshintrc: '.jshintrc' 46 | } 47 | }, 48 | uglify: { 49 | options: { 50 | banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' + 51 | '<%= pkg.author.name %> - <%= pkg.licenses[0].type %> - <%= pkg.homepage %> */\n', 52 | report: 'gzip' 53 | }, 54 | dist: { 55 | files: { 56 | 'lib/jquery.tabletojson.min.js': ['','lib/jquery.tabletojson.js'], 57 | 'docs/jquery.tabletojson.min.js': ['','lib/jquery.tabletojson.js'] 58 | } 59 | } 60 | }, 61 | qunit: { 62 | files: ['test/index.html'] 63 | }, 64 | watch: { 65 | scripts: { 66 | files: ['src/**/*.js'], 67 | tasks: ['build'] 68 | } 69 | } 70 | }); 71 | 72 | grunt.loadNpmTasks('grunt-contrib-clean' ); 73 | grunt.loadNpmTasks('grunt-contrib-jshint'); 74 | grunt.loadNpmTasks('grunt-contrib-qunit'); 75 | grunt.loadNpmTasks('grunt-contrib-concat'); 76 | grunt.loadNpmTasks('grunt-contrib-uglify'); 77 | grunt.loadNpmTasks('grunt-contrib-watch'); 78 | 79 | // Default task. 80 | grunt.registerTask('build', ['clean:build', 'concat', 'uglify']); 81 | grunt.registerTask('test', ['jshint', 'qunit']); 82 | grunt.registerTask('lint', ['jshint']); 83 | grunt.registerTask('default', ['test', 'build']); 84 | 85 | }; 86 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2020 Daniel White 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | 3 | setup: 4 | @docker-compose pull 5 | @docker-compose run --rm node npm install 6 | 7 | build: lint 8 | @docker-compose run --rm node ./node_modules/grunt-cli/bin/grunt build 9 | 10 | lint: 11 | @docker-compose run --rm node ./node_modules/grunt-cli/bin/grunt lint 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Table To JSON 2 | 3 | [![Build Status](https://travis-ci.org/lightswitch05/table-to-json.png?branch=master)](https://travis-ci.org/lightswitch05/table-to-json) 4 | [![license](https://img.shields.io/github/license/lightswitch05/table-to-json.svg)](https://github.com/lightswitch05/table-to-json/blob/master/MIT-LICENSE) 5 | 6 | jQuery plugin to serialize HTML tables into javascript objects. 7 | 8 | ## Links 9 | - Demo: https://lightswitch05.github.io/table-to-json/ 10 | - Plunker Template: https://plnkr.co/edit/iQFtcEEZkvsMJ2UqcrlW?p=preview 11 | 12 | ## CDN 13 | 14 | It is recommended to pull this tool into your project directly. But if you insist to use a CDN, here is one: 15 | 16 | ``` 17 | 18 | ``` 19 | 20 | ## Features 21 | - Automatically finds column headings 22 | - Override found column headings by using `data-override="overridden column name"` 23 | - Always uses first row as column headings regardless of `th` and `td` tags 24 | - Override cell values column names by using `data-override="new value"` 25 | - Ignorable columns 26 | - Not confused by nested tables 27 | - Works with `rowspan` and `colspan` 28 | 29 | ## Options 30 | - `ignoreColumns` 31 | - Array of column indexes to ignore. 32 | - Default: `[]` 33 | - `onlyColumns` 34 | - Array of column indexes to include, all other columns are ignored. This takes presidence over `ignoreColumns` when provided. 35 | - Default: `null` - all columns 36 | - `ignoreHiddenRows` 37 | - Boolean if hidden rows should be ignored or not. 38 | - Default: `true` 39 | - `ignoreEmptyRows` 40 | - Boolean if empty rows should be ignored or not. 41 | - Default: `false` 42 | - `headings` 43 | - Array of table headings to use. When supplied, treats entire table as values including the first `` 44 | - Default: `null` 45 | - `allowHTML` 46 | - Boolean if HTML tags in table cells should be preserved 47 | - Default: `false` 48 | - `includeRowId` 49 | - Either a `boolean` or a `string`. If `true`, the the `id` attribute on the table's `` 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. 50 | - Default: `false` 51 | - `textDataOverride` 52 | - String containing data-attribute which contains data which overrides the text contained within the table cell 53 | - Default: 'data-override' 54 | - `textExtractor` 55 | - alias of `extractor` 56 | - `extractor` 57 | - 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: 58 | 59 | ```js 60 | $('table').tableToJSON({ 61 | extractor : function(cellIndex, $cell) { 62 | // get text from the span inside table cells; 63 | // if empty or non-existant, get the cell text 64 | return $cell.find('span').text() || $cell.text(); 65 | } 66 | }); 67 | ``` 68 | 69 | ```js 70 | $('table').tableToJSON({ 71 | extractor : function(cellIndex, $cell) { 72 | return { 73 | name: $cell.find('span').text(), 74 | avatar: $cell.find('img').attr('src') 75 | }; 76 | } 77 | }); 78 | ``` 79 | 80 | - 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: 81 | 82 | ```js 83 | $('table').tableToJSON({ 84 | extractor : { 85 | 0 : function(cellIndex, $cell) { 86 | return $cell.find('em').text(); 87 | }, 88 | 1 : function(cellIndex, $cell) { 89 | return $cell.find('span').text(); 90 | } 91 | } 92 | }); 93 | ``` 94 | 95 | - Default: `null` 96 | 97 | ## Example 98 | 99 | ```html 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 |
First NameLast NamePoints
JillSmith50
EveJackson94
JohnDoe80
AdamJohnson67
126 | 127 | 144 | ``` 145 | 146 | ## Contributing 147 | 148 | * Install [Node.js](https://nodejs.org). 149 | * this will also the `npm` package manager. 150 | * run `npm install` from app root directory. 151 | * This installs grunt and other dependencies See `package.json` for a full list. 152 | * run `npm install -g grunt-cli`. 153 | * run `grunt` to run tests and create a new build in `/lib`. 154 | * Make the changes you want. 155 | * Make tests for the changes. 156 | * Submit a pull request, please submit to the `develop` branch. 157 | 158 | ### Looking for a server-side solution? 159 | 160 | [Colin Tremblay](https://github.com/tremblay) is working on a PHP implementation at [HTML-Table-To-JSON](https://github.com/tremblay/HTML-Table-to-JSON) 161 | 162 | ### Special Thanks 163 | * [imamathwiz](https://github.com/imamathwiz) for adding `allowHTML` option and various other changes. 164 | * [nenads](https://github.com/nenads) for adding `headings` option. 165 | * [Mottie](https://github.com/Mottie) for adding `rowspan` & `colspan` support. Also adding the `textExtractor` & `dataOverride` feature! 166 | * [station384](https://github.com/station384) for adding `includeRowId` support. 167 | * [dayAlone](https://github.com/dayAlone) for adding `ignoreEmptyRows` option. 168 | * [danielapsmaior](https://github.com/danielapsmaior) for discovering and fixing a `rowspan` & `colspan` bug. 169 | * [koshuang](https://github.com/koshuang) for adding `extractor` feature! 170 | * [noma4i](https://github.com/noma4i) added feature "Skip columns where headers are not present" 171 | * [cn-tools](https://github.com/cn-tools) for reporting AND fixing a bug when using both `ignoreEmptyRows` and `ignoreColumns` options 172 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "table-to-json", 3 | "version": "1.0.0", 4 | "main": [ 5 | "./lib/jquery.tabletojson.min.js" 6 | ], 7 | "dependencies": { 8 | "jquery": "*" 9 | }, 10 | "homepage": "https://github.com/lightswitch05/table-to-json", 11 | "authors": [ 12 | "Daniel White " 13 | ], 14 | "description": "jQuery plugin that reads an HTML table and returns a javascript object representing the values and columns of the table", 15 | "license": "MIT", 16 | "keywords": [ 17 | "table-to-json" 18 | ], 19 | "ignore": [ 20 | "**/.*", 21 | "node_modules", 22 | "bower_components", 23 | "test", 24 | "tests", 25 | "src" 26 | ] 27 | } 28 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.7' 2 | 3 | services: 4 | node: 5 | image: node:0.12.14-wheezy 6 | volumes: 7 | - "${PWD}:/opt/table-to-json" 8 | working_dir: /opt/table-to-json 9 | -------------------------------------------------------------------------------- /docs/images/bg_hr.png: -------------------------------------------------------------------------------- 1 | You are being redirected. -------------------------------------------------------------------------------- /docs/images/blacktocat.png: -------------------------------------------------------------------------------- 1 | You are being redirected. -------------------------------------------------------------------------------- /docs/images/forkme_right_gray_6d6d6d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lightswitch05/table-to-json/2663365daf11d20b2179c05cb25c7b88d3218bb2/docs/images/forkme_right_gray_6d6d6d.png -------------------------------------------------------------------------------- /docs/images/icon_download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lightswitch05/table-to-json/2663365daf11d20b2179c05cb25c7b88d3218bb2/docs/images/icon_download.png -------------------------------------------------------------------------------- /docs/images/sprite_download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lightswitch05/table-to-json/2663365daf11d20b2179c05cb25c7b88d3218bb2/docs/images/sprite_download.png -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Table-to-JSON 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 34 |
35 | 36 |
37 |
38 |

Table-to-JSON

39 |

40 | A jQuery plugin that converts an HTML Table into a javascript object. Great for working with user-editable tables or accessing output from 3rd party tools. 41 |

42 | 43 |
44 |

45 | Download .zip 46 | Download tar.gz 47 |

48 |
49 |
50 |
51 | 52 | 53 |
54 |
55 |

Demo

56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 |
First NameLast NamePoints
JillSmith50
EveJackson94
JohnDoe80
AdamJohnson67
82 | 83 |

84 | This examples uses the data-override option: 85 |

<table id='example-table'>
 86 |   <thead>
 87 |     <tr>
 88 |       <th>First Name</th>
 89 |       <th>Last Name</th>
 90 |       <th data-override="Score">Points</th></tr>
 91 |   </thead>
 92 |   <tbody>
 93 |     <tr>
 94 |       <td>Jill</td>
 95 |       <td>Smith</td>
 96 |       <td data-override="disqualified">50</td></tr>
 97 |     <tr>
 98 |       <td>Eve</td>
 99 |       <td>Jackson</td>
100 |       <td>94</td></tr>
101 |     <tr>
102 |       <td>John</td>
103 |       <td>Doe</td>
104 |       <td>80</td></tr>
105 |     <tr>
106 |       <td>Adam</td>
107 |       <td>Johnson</td>
108 |       <td>67</td></tr>
109 |   </tbody>
110 | </table>
111 | 
112 | <script>
113 | $('#convert-table').click( function() {
114 |   var table = $('#example-table').tableToJSON(); // Convert the table into a javascript object
115 |   console.log(table);
116 |   alert(JSON.stringify(table));
117 | });
118 | </script>
119 |           
120 |

121 |
122 | 123 |
124 |

Options

125 |
    126 |
  • ignoreColumns 127 |
      128 |
    • Array of column indexes to ignore.
    • 129 |
    • Type: Array 130 |
    • Default: []
    • 131 |
    132 |
  • 133 |
  • onlyColumns 134 |
      135 |
    • Array of column indexes to include, all other columns are ignored. This takes presidence over ignoreColumns when provided.
    • 136 |
    • This takes precedence over ignoreColumns when both are provided.
    • 137 |
    • Type: Array 138 |
    • Default: null - all columns
    • 139 |
    140 |
  • 141 |
  • ignoreHiddenRows 142 |
      143 |
    • Boolean if hidden rows should be ignored or not.
    • 144 |
    • Type: Boolean 145 |
    • Default: true
    • 146 |
    147 |
  • 148 |
  • headings 149 |
      150 |
    • Array of column headings to use. When supplied, all table rows are treated as values.
    • 151 |
    • Type: Array 152 |
    • Default: null
    • 153 |
    154 |
  • 155 |
  • allowHTML 156 |
      157 |
    • Boolean if HTML tags in table cells should be preserved.
    • 158 |
    • Type: Boolean
    • 159 |
    • Default: false
    • 160 |
    161 |
  • 162 |
  • includeRowId 163 |
      164 |
    • Determines if the id attribute of each <tr> element is included in the JSON.
    • 165 |
    • Type: Boolean or String 166 |
        If true, the ids are included under the header rowId.
      167 |
        If String, it is used as the header for the id instead of the default rowId.
      168 |
    • 169 |
    • Default: false
    • 170 |
    171 |
  • 172 |
173 |
174 | 175 |
176 |

Features

177 |
    178 |
  • Automatically finds column headings. 179 |
      180 |
    • Override found column headings by using data-override="overridden column name".
    • 181 |
    • Always uses first row as column headings regardless of th and td tags.
    • 182 |
    • Define headings using the headings option when a table doesn't have any specified headings.
    • 183 |
    184 |
  • 185 |
  • Override cell values column names by using data-override="new value".
  • 186 |
  • Ignorable columns.
  • 187 |
  • Not confused by nested tables.
  • 188 |
189 |
190 |
191 | 192 | 193 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | -------------------------------------------------------------------------------- /docs/jquery.tabletojson.min.js: -------------------------------------------------------------------------------- 1 | /*! table-to-json - v1.0.0 - Daniel White - MIT - https://github.com/lightswitch05/table-to-json */ 2 | !function(a){"use strict";var b=function(b,c,d){this.$element=a(b),this.index=c,this.cachedRowSpan=null,this.options=a.extend({},a.fn.tableToJSONCell.defaults,d),this.init()};b.prototype={constructor:b,value:function(b){var c=a.extend({},this.options,b),d=a.trim(this.$element.text()),e=this.$element.attr(this.options.textDataOverride);if(e)d=e;else{if(c.extractor||c.textExtractor)return this.extractedValue();c.allowHTML&&(d=a.trim(this.$element.html()))}return this.withColSpan(d)},extractedValue:function(){var b=this.options.extractor||this.options.textExtractor,c=null;return a.isFunction(b)?c=b(this.index,this.$element):"object"==typeof b&&a.isFunction(b[this.index])&&(c=b[this.index](this.index,this.$element)),"string"==typeof c?a.trim(c):c},colSpan:function(){var a=1;return this.$element.attr("colSpan")&&(a=parseInt(this.$element.attr("colSpan"),10)),a},rowSpan:function(a){return 1===arguments.length?this.cachedRowSpan=a:this.cachedRowSpan||(this.cachedRowSpan=1,this.$element.attr("rowSpan")&&(this.cachedRowSpan=parseInt(this.$element.attr("rowSpan"),10))),this.cachedRowSpan},withColSpan:function(a){var b=a;if(this.$element.attr("colSpan")){var c=this.colSpan();if(c>1){b=[];for(var d=0;d1;)d.push(c),b--;c.rowSpan(1)}d.length>0&&(a[e]=d)}return a},insertRowSpans:function(a){for(var b=0;b0){var c=a[b].splice(0,1)[0];this.insert(b,c)}return a},rowSpans:function(){for(var a,b,c=[],d=[],e=0;e1;)d.push(b),a--;b.rowSpan(1),d.length>0&&(c[e]=d)}return c},values:function(b){for(var c=a.extend({},this.options,b),d=[],e=null,f=0,g=0;g-1},init:function(){var b=this;this.$element.children(this.options.cellSelector).each(function(c,d){b.cells.push(a(d).tableToJSONCell(c,b.options))}),a.proxy(function(){this.$element.triggerHandler("init",this)},this)}},a.fn.tableToJSONRow=function(a){return new b(this,a)},a.fn.tableToJSONRow.defaults={onlyColumns:null,ignoreColumns:[],cellSelector:"td,th"}}(jQuery),function(a){"use strict";var b=function(b,c){this.$element=a(b),this.rows=[],this.options=a.extend({},a.fn.tableToJSON.defaults,c),this.init()};b.prototype={constructor:b,headings:function(){return this.rows.length>0&&!this.options.headings?this.rows[0].values({extractor:null,textExtractor:null}):this.options.headings?this.options.headings:[]},values:function(){var a=[],b=this.headings(),c=this.options.headings?0:1;for(c;c-1||a.$element.data("ignore")&&"false"!==a.$element.data("ignore")||this.options.ignoreHiddenRows&&!a.$element.is(":visible")||this.options.ignoreEmptyRows&&a.isEmpty()},addRow:function(a,b){return a.insertRowSpans(b),this.rows.push(a),a.getRowSpans(b)},init:function(){var b=this,c=[],d=null;this.$element.children(this.options.rowParentSelector).children(this.options.rowSelector).each(function(e,f){d=a(f).tableToJSONRow(b.options),c=b.addRow(d,c)}),a.proxy(function(){this.$element.triggerHandler("init",this)},this)}},a.fn.tableToJSON=function(a){return new b(this,a).values()},a.fn.tableToJSON.defaults={ignoreRows:[],ignoreHiddenRows:!0,ignoreEmptyRows:!1,headings:null,includeRowId:!1,rowParentSelector:"tbody,*",rowSelector:"tr"}}(jQuery); -------------------------------------------------------------------------------- /docs/main.css: -------------------------------------------------------------------------------- 1 | .footer { 2 | text-align: center; 3 | padding: 30px 0; 4 | margin-top: 70px; 5 | border-top: 1px solid #e5e5e5; 6 | background-color: #f5f5f5; 7 | } 8 | 9 | .center { 10 | float: none; 11 | margin-left: auto; 12 | margin-right: auto; 13 | } 14 | 15 | xmp { 16 | margin: 0; 17 | } 18 | #convert-table { 19 | margin-bottom: 1.5em; 20 | } -------------------------------------------------------------------------------- /docs/main.js: -------------------------------------------------------------------------------- 1 | $('#convert-table').click( function() { 2 | var table = $('#example-table').tableToJSON(); 3 | console.log(table); 4 | alert(JSON.stringify(table)); 5 | }); -------------------------------------------------------------------------------- /lib/jquery.tabletojson.js: -------------------------------------------------------------------------------- 1 | /** 2 | * table-to-json 3 | * jQuery plugin that reads an HTML table and returns a javascript object representing the values and columns of the table 4 | * 5 | * @author Daniel White 6 | * @copyright Daniel White 2020 7 | * @license MIT 8 | * @link https://github.com/lightswitch05/table-to-json 9 | * @module table-to-json 10 | * @version 1.0.0 11 | */ 12 | (function($) { 13 | 'use strict'; 14 | 15 | var TableToJSONCell = function (cell, index, options) { 16 | this.$element = $(cell); 17 | this.index = index; 18 | this.cachedRowSpan = null; 19 | this.options = $.extend({}, $.fn.tableToJSONCell.defaults, options); 20 | this.init(); 21 | }; 22 | 23 | TableToJSONCell.prototype = { 24 | constructor: TableToJSONCell, 25 | 26 | value: function(options) { 27 | var runOptions = $.extend({}, this.options, options); 28 | var value = $.trim(this.$element.text()); 29 | var override = this.$element.attr(this.options.textDataOverride); 30 | if (override) { 31 | value = override; 32 | } else if (runOptions.extractor || runOptions.textExtractor) { 33 | return this.extractedValue(); 34 | } else if (runOptions.allowHTML) { 35 | value = $.trim(this.$element.html()); 36 | } 37 | return this.withColSpan(value); 38 | }, 39 | 40 | extractedValue: function() { 41 | var extractor = this.options.extractor || this.options.textExtractor; 42 | var value = null; 43 | if ( $.isFunction(extractor) ) { 44 | value = extractor(this.index, this.$element); 45 | } else if (typeof extractor === 'object' && $.isFunction(extractor[this.index])) { 46 | value = extractor[this.index](this.index, this.$element); 47 | } 48 | return typeof value === 'string' ? $.trim(value) : value; 49 | }, 50 | 51 | /** 52 | * Gets the cell's colSpan attribute. 53 | * @returns {number} 54 | */ 55 | colSpan: function() { 56 | var span = 1; 57 | if (this.$element.attr('colSpan')) { 58 | span = parseInt( this.$element.attr('colSpan'), 10 ); 59 | } 60 | return span; 61 | }, 62 | 63 | rowSpan: function(rowSpan) { 64 | if(arguments.length === 1){ 65 | this.cachedRowSpan = rowSpan; 66 | } else if (!this.cachedRowSpan) { 67 | this.cachedRowSpan = 1; 68 | if (this.$element.attr('rowSpan')) { 69 | this.cachedRowSpan = parseInt( this.$element.attr('rowSpan'), 10 ); 70 | } 71 | } 72 | return this.cachedRowSpan; 73 | }, 74 | 75 | /** 76 | * Returns either the given value, or an array of the value for each header that owns this cell. 77 | * @param value 78 | * @returns [] 79 | */ 80 | withColSpan: function(value) { 81 | var response = value; 82 | if (this.$element.attr('colSpan')) { 83 | var span = this.colSpan(); 84 | if(span > 1){ 85 | response = []; 86 | for(var index=0; index < span; index++){ 87 | // Dup the value for each header this cell belongs to 88 | response.push(value); 89 | } 90 | } 91 | } 92 | return response; 93 | }, 94 | 95 | init: function () { 96 | $.proxy(function() { 97 | /** 98 | Fired when element was initialized by `$().tableToJSON()` method. 99 | Please note that you should setup `init` handler **before** applying `tableToJSON`. 100 | 101 | @event init 102 | @param {Object} event event object 103 | @param {Object} editable TableToJSONCell instance 104 | **/ 105 | this.$element.triggerHandler('init', this); 106 | }, this); 107 | } 108 | }; 109 | 110 | // Initialize cell 111 | $.fn.tableToJSONCell = function (index, options) { 112 | return new TableToJSONCell(this, index, options); 113 | }; 114 | 115 | $.fn.tableToJSONCell.defaults = { 116 | /** 117 | * Boolean if HTML tags in table cells should be preserved. 118 | * @type boolean 119 | * @default false 120 | */ 121 | allowHTML: false, 122 | 123 | /** 124 | * String of the data-* attribute name to use for the override value. 125 | * @type String 126 | * @default 'override' 127 | */ 128 | textDataOverride: 'data-override', 129 | 130 | /** 131 | * Function that is used on all tbody cells to extract text from the cells. 132 | * Note: A value in `data-override` will prevent this function from being called. 133 | * @type Function|Object 134 | * @default null 135 | */ 136 | extractor: null, 137 | 138 | /** 139 | * alias for extractor option. 140 | */ 141 | textExtractor: null 142 | }; 143 | })(jQuery); 144 | 145 | (function($) { 146 | 'use strict'; 147 | 148 | var TableToJSONRow = function (row, options) { 149 | this.$element = $(row); 150 | this.cells = []; 151 | this.options = $.extend({}, $.fn.tableToJSONRow.defaults, options); 152 | this.init(); 153 | }; 154 | 155 | TableToJSONRow.prototype = { 156 | constructor: TableToJSONRow, 157 | 158 | id: function(){ 159 | return this.$element.attr('id') ? this.$element.attr('id') : null; 160 | }, 161 | 162 | valuesWithHeadings: function(headers){ 163 | var valuesWithHeadings = {}; 164 | var values = this.values(); 165 | for(var index = 0; index < values.length; index++){ 166 | valuesWithHeadings[headers[index]] = values[index]; 167 | } 168 | return valuesWithHeadings; 169 | }, 170 | 171 | isEmpty: function(){ 172 | var empty = true; 173 | var values = this.values(); 174 | for(var index = 0; empty && index < values.length; index++){ 175 | if(values[index] !== ''){ 176 | empty = false; 177 | } 178 | } 179 | return empty; 180 | }, 181 | 182 | cell: function(index){ 183 | if(index < this.cells.length){ 184 | return this.cells[index]; 185 | } else { 186 | return null; 187 | } 188 | }, 189 | 190 | insert: function(index, cell){ 191 | this.cells.splice(index, 0, cell); 192 | }, 193 | 194 | getRowSpans: function(spans){ 195 | var span, rows = [], cell; 196 | for(var cellIndex = 0; cellIndex < this.cells.length; cellIndex++){ 197 | rows = []; 198 | cell = this.cells[cellIndex]; 199 | if(cell){ 200 | span = cell.rowSpan(); 201 | while(span > 1){ 202 | rows.push(cell); 203 | span--; 204 | } 205 | cell.rowSpan(1); 206 | } 207 | if(rows.length > 0){ 208 | spans[cellIndex] = rows; 209 | } 210 | } 211 | return spans; 212 | }, 213 | 214 | insertRowSpans: function(spans){ 215 | for(var cellIndex = 0; cellIndex < spans.length; cellIndex++) { 216 | if (spans[cellIndex] && spans[cellIndex].length > 0) { 217 | var spannedCell = spans[cellIndex].splice(0, 1)[0]; 218 | this.insert(cellIndex, spannedCell); 219 | } 220 | } 221 | return spans; 222 | }, 223 | 224 | rowSpans: function(){ 225 | var spans = [], span, rows = [], cell; 226 | for(var cellIndex = 0; cellIndex < this.cells.length; cellIndex++){ 227 | rows = []; 228 | cell = this.cells[cellIndex]; 229 | span = cell.rowSpan(); 230 | while(span > 1){ 231 | rows.push(cell); 232 | span--; 233 | } 234 | cell.rowSpan(1); 235 | if(rows.length > 0){ 236 | spans[cellIndex] = rows; 237 | } 238 | } 239 | return spans; 240 | }, 241 | 242 | values: function(options){ 243 | var runOptions = $.extend({}, this.options, options); 244 | var cellValues = [], value = null, colSpanOffset = 0; 245 | for(var index = 0; index < this.cells.length; index++){ 246 | value = this.cells[index].value(runOptions); 247 | 248 | if (this.cells[index].colSpan() === 1) { 249 | // simple case, either ignore it or not 250 | if(!this.ignoreColumn(colSpanOffset)){ 251 | cellValues = cellValues.concat(value); 252 | } 253 | colSpanOffset++; 254 | } else { 255 | // this cell has a colSpan, ensure each 256 | // value match ignored columns 257 | for (var valuesIndex = 0; valuesIndex < value.length; valuesIndex++) { 258 | if (!this.ignoreColumn(colSpanOffset)) { 259 | cellValues = cellValues.concat(value[valuesIndex]); 260 | } 261 | colSpanOffset++; 262 | } 263 | } 264 | } 265 | return cellValues; 266 | }, 267 | 268 | ignoreColumn: function(index){ 269 | if( this.options.onlyColumns ) { 270 | return this.options.onlyColumns.indexOf(index) < 0; 271 | } 272 | return this.options.ignoreColumns.indexOf(index) > -1; 273 | }, 274 | 275 | init: function () { 276 | // Init Cells 277 | var self = this; 278 | this.$element.children(this.options.cellSelector).each(function(cellIndex, cell) { 279 | self.cells.push( $(cell).tableToJSONCell(cellIndex, self.options) ); 280 | }); 281 | 282 | // Finalize init 283 | $.proxy(function() { 284 | /** 285 | Fired when row was initialized by `$().tableToJSON()` method. 286 | Please note that you should setup `init` handler **before** applying `tableToJSON`. 287 | 288 | @event init 289 | @param {Object} event event object 290 | @param {Object} editable TableToJSONRow instance 291 | **/ 292 | this.$element.triggerHandler('init', this); 293 | }, this); 294 | } 295 | }; 296 | 297 | // Initialize row 298 | $.fn.tableToJSONRow = function (options) { 299 | return new TableToJSONRow(this, options); 300 | }; 301 | 302 | $.fn.tableToJSONRow.defaults = { 303 | /** 304 | Array of column indexes to include, all other columns are ignored. This takes precedence over ignoreColumns when provided. 305 | 306 | @type Array 307 | @default null 308 | **/ 309 | onlyColumns: null, 310 | 311 | /** 312 | Array of column indexes to ignore. 313 | 314 | @type Array 315 | @default [] 316 | **/ 317 | ignoreColumns: [], 318 | cellSelector: 'td,th' 319 | }; 320 | })(jQuery); 321 | 322 | (function( $ ) { 323 | 'use strict'; 324 | 325 | var TableToJSON = function (table, options) { 326 | this.$element = $(table); 327 | this.rows = []; 328 | this.options = $.extend({}, $.fn.tableToJSON.defaults, options); 329 | this.init(); 330 | }; 331 | 332 | TableToJSON.prototype = { 333 | constructor: TableToJSON, 334 | 335 | headings: function() { 336 | if(this.rows.length > 0 && !this.options.headings){ 337 | // have to disable the extractor for header rows 338 | return this.rows[0].values({extractor: null, textExtractor: null}); 339 | } else if (this.options.headings){ 340 | return this.options.headings; 341 | } else { 342 | return []; 343 | } 344 | }, 345 | 346 | values: function() { 347 | var rowValues = []; 348 | var headings = this.headings(); 349 | var index = (this.options.headings) ? 0 : 1; // Skip first row if heading are not supplied 350 | for(index; index < this.rows.length; index++){ 351 | if(!this.ignoreRow(this.rows[index], index)){ 352 | if(this.options.includeRowId){ 353 | var rowIdHeader = (typeof this.options.includeRowId === 'string') ? this.options.includeRowId : 'rowId'; 354 | var cellValues = this.rows[index].valuesWithHeadings(headings); 355 | cellValues[rowIdHeader] = this.rows[index].id(); 356 | rowValues.push( cellValues ); 357 | } else { 358 | rowValues.push( this.rows[index].valuesWithHeadings(headings) ); 359 | } 360 | } 361 | } 362 | return rowValues; 363 | }, 364 | 365 | ignoreRow: function($row, index) { 366 | return ( 367 | this.options.ignoreRows && this.options.ignoreRows.indexOf(index) > -1) || 368 | ($row.$element.data('ignore') && $row.$element.data('ignore') !== 'false') || 369 | (this.options.ignoreHiddenRows && !$row.$element.is(':visible')) || 370 | (this.options.ignoreEmptyRows && $row.isEmpty()); 371 | }, 372 | 373 | addRow: function($row, rowSpans) { 374 | $row.insertRowSpans(rowSpans); 375 | this.rows.push($row); 376 | return $row.getRowSpans(rowSpans); 377 | }, 378 | 379 | init: function () { 380 | // Init Rows 381 | var self = this, rowSpans = [], newRow = null; 382 | this.$element.children(this.options.rowParentSelector).children(this.options.rowSelector).each(function(rowIndex, row) { 383 | newRow = $(row).tableToJSONRow(self.options); 384 | rowSpans = self.addRow( newRow, rowSpans ); 385 | }); 386 | 387 | $.proxy(function() { 388 | /** 389 | Fired when element was initialized by `$().tableToJSON()` method. 390 | Please note that you should setup `init` handler **before** applying `tableToJSON`. 391 | 392 | @event init 393 | @param {Object} event event object 394 | @param {Object} editable TableToJSON instance 395 | **/ 396 | this.$element.triggerHandler('init', this); 397 | }, this); 398 | } 399 | }; 400 | 401 | // Initialize 402 | $.fn.tableToJSON = function (options) { 403 | var table = new TableToJSON(this, options); 404 | return table.values(); 405 | }; 406 | 407 | $.fn.tableToJSON.defaults = { 408 | /** 409 | * Array of row indexes to ignore. 410 | * @type Array 411 | * @default [] 412 | */ 413 | ignoreRows: [], 414 | 415 | /** 416 | * Boolean if hidden rows should be ignored or not. 417 | * @type Boolean 418 | * @default true 419 | */ 420 | ignoreHiddenRows: true, 421 | 422 | /** 423 | * Boolean if hidden rows should be ignored or not. 424 | * @type Boolean 425 | * @default false 426 | */ 427 | ignoreEmptyRows: false, 428 | 429 | /** 430 | * Array of column headings to use. When supplied, all table rows are treated as values (no headings row). 431 | * @type Array 432 | * @default null 433 | */ 434 | headings: null, 435 | 436 | /** 437 | * Determines if the `id` attribute of each `` element is included in the JSON. 438 | * @type Boolean 439 | * @default false 440 | */ 441 | includeRowId: false, 442 | 443 | rowParentSelector: 'tbody,*', 444 | rowSelector: 'tr' 445 | }; 446 | })( jQuery ); 447 | -------------------------------------------------------------------------------- /lib/jquery.tabletojson.min.js: -------------------------------------------------------------------------------- 1 | /*! table-to-json - v1.0.0 - Daniel White - MIT - https://github.com/lightswitch05/table-to-json */ 2 | !function(a){"use strict";var b=function(b,c,d){this.$element=a(b),this.index=c,this.cachedRowSpan=null,this.options=a.extend({},a.fn.tableToJSONCell.defaults,d),this.init()};b.prototype={constructor:b,value:function(b){var c=a.extend({},this.options,b),d=a.trim(this.$element.text()),e=this.$element.attr(this.options.textDataOverride);if(e)d=e;else{if(c.extractor||c.textExtractor)return this.extractedValue();c.allowHTML&&(d=a.trim(this.$element.html()))}return this.withColSpan(d)},extractedValue:function(){var b=this.options.extractor||this.options.textExtractor,c=null;return a.isFunction(b)?c=b(this.index,this.$element):"object"==typeof b&&a.isFunction(b[this.index])&&(c=b[this.index](this.index,this.$element)),"string"==typeof c?a.trim(c):c},colSpan:function(){var a=1;return this.$element.attr("colSpan")&&(a=parseInt(this.$element.attr("colSpan"),10)),a},rowSpan:function(a){return 1===arguments.length?this.cachedRowSpan=a:this.cachedRowSpan||(this.cachedRowSpan=1,this.$element.attr("rowSpan")&&(this.cachedRowSpan=parseInt(this.$element.attr("rowSpan"),10))),this.cachedRowSpan},withColSpan:function(a){var b=a;if(this.$element.attr("colSpan")){var c=this.colSpan();if(c>1){b=[];for(var d=0;d1;)d.push(c),b--;c.rowSpan(1)}d.length>0&&(a[e]=d)}return a},insertRowSpans:function(a){for(var b=0;b0){var c=a[b].splice(0,1)[0];this.insert(b,c)}return a},rowSpans:function(){for(var a,b,c=[],d=[],e=0;e1;)d.push(b),a--;b.rowSpan(1),d.length>0&&(c[e]=d)}return c},values:function(b){for(var c=a.extend({},this.options,b),d=[],e=null,f=0,g=0;g-1},init:function(){var b=this;this.$element.children(this.options.cellSelector).each(function(c,d){b.cells.push(a(d).tableToJSONCell(c,b.options))}),a.proxy(function(){this.$element.triggerHandler("init",this)},this)}},a.fn.tableToJSONRow=function(a){return new b(this,a)},a.fn.tableToJSONRow.defaults={onlyColumns:null,ignoreColumns:[],cellSelector:"td,th"}}(jQuery),function(a){"use strict";var b=function(b,c){this.$element=a(b),this.rows=[],this.options=a.extend({},a.fn.tableToJSON.defaults,c),this.init()};b.prototype={constructor:b,headings:function(){return this.rows.length>0&&!this.options.headings?this.rows[0].values({extractor:null,textExtractor:null}):this.options.headings?this.options.headings:[]},values:function(){var a=[],b=this.headings(),c=this.options.headings?0:1;for(c;c-1||a.$element.data("ignore")&&"false"!==a.$element.data("ignore")||this.options.ignoreHiddenRows&&!a.$element.is(":visible")||this.options.ignoreEmptyRows&&a.isEmpty()},addRow:function(a,b){return a.insertRowSpans(b),this.rows.push(a),a.getRowSpans(b)},init:function(){var b=this,c=[],d=null;this.$element.children(this.options.rowParentSelector).children(this.options.rowSelector).each(function(e,f){d=a(f).tableToJSONRow(b.options),c=b.addRow(d,c)}),a.proxy(function(){this.$element.triggerHandler("init",this)},this)}},a.fn.tableToJSON=function(a){return new b(this,a).values()},a.fn.tableToJSON.defaults={ignoreRows:[],ignoreHiddenRows:!0,ignoreEmptyRows:!1,headings:null,includeRowId:!1,rowParentSelector:"tbody,*",rowSelector:"tr"}}(jQuery); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "table-to-json", 3 | "description": "jQuery plugin that reads an HTML table and returns a javascript object representing the values and columns of the table", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/lightswitch05/table-to-json", 6 | "main": "lib/jquery.tabletojson.js", 7 | "demo": "https://lightswitch05.github.io/table-to-json/", 8 | "license": "MIT", 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/lightswitch05/table-to-json.git" 12 | }, 13 | "author": { 14 | "name": "Daniel White" 15 | }, 16 | "scripts": { 17 | "test": "grunt test" 18 | }, 19 | "devDependencies": { 20 | "grunt": "~0.4.5", 21 | "grunt-cli": "~0.1.13", 22 | "grunt-contrib-jshint": "~0.11.2", 23 | "grunt-contrib-qunit": "~0.7.0", 24 | "grunt-contrib-concat": "~0.5.1", 25 | "grunt-contrib-uglify": "~0.9.1", 26 | "grunt-contrib-clean": "~0.6.0", 27 | "grunt-contrib-watch": "~0.6.1" 28 | }, 29 | "licenses": [ 30 | { 31 | "type": "MIT", 32 | "url": "https://github.com/lightswitch05/table-to-json/blob/master/MIT-LICENSE" 33 | } 34 | ] 35 | } 36 | -------------------------------------------------------------------------------- /src/tabletojson-cell.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | 'use strict'; 3 | 4 | var TableToJSONCell = function (cell, index, options) { 5 | this.$element = $(cell); 6 | this.index = index; 7 | this.cachedRowSpan = null; 8 | this.options = $.extend({}, $.fn.tableToJSONCell.defaults, options); 9 | this.init(); 10 | }; 11 | 12 | TableToJSONCell.prototype = { 13 | constructor: TableToJSONCell, 14 | 15 | value: function(options) { 16 | var runOptions = $.extend({}, this.options, options); 17 | var value = $.trim(this.$element.text()); 18 | var override = this.$element.attr(this.options.textDataOverride); 19 | if (override) { 20 | value = override; 21 | } else if (runOptions.extractor || runOptions.textExtractor) { 22 | return this.extractedValue(); 23 | } else if (runOptions.allowHTML) { 24 | value = $.trim(this.$element.html()); 25 | } 26 | return this.withColSpan(value); 27 | }, 28 | 29 | extractedValue: function() { 30 | var extractor = this.options.extractor || this.options.textExtractor; 31 | var value = null; 32 | if ( $.isFunction(extractor) ) { 33 | value = extractor(this.index, this.$element); 34 | } else if (typeof extractor === 'object' && $.isFunction(extractor[this.index])) { 35 | value = extractor[this.index](this.index, this.$element); 36 | } 37 | return typeof value === 'string' ? $.trim(value) : value; 38 | }, 39 | 40 | /** 41 | * Gets the cell's colSpan attribute. 42 | * @returns {number} 43 | */ 44 | colSpan: function() { 45 | var span = 1; 46 | if (this.$element.attr('colSpan')) { 47 | span = parseInt( this.$element.attr('colSpan'), 10 ); 48 | } 49 | return span; 50 | }, 51 | 52 | rowSpan: function(rowSpan) { 53 | if(arguments.length === 1){ 54 | this.cachedRowSpan = rowSpan; 55 | } else if (!this.cachedRowSpan) { 56 | this.cachedRowSpan = 1; 57 | if (this.$element.attr('rowSpan')) { 58 | this.cachedRowSpan = parseInt( this.$element.attr('rowSpan'), 10 ); 59 | } 60 | } 61 | return this.cachedRowSpan; 62 | }, 63 | 64 | /** 65 | * Returns either the given value, or an array of the value for each header that owns this cell. 66 | * @param value 67 | * @returns [] 68 | */ 69 | withColSpan: function(value) { 70 | var response = value; 71 | if (this.$element.attr('colSpan')) { 72 | var span = this.colSpan(); 73 | if(span > 1){ 74 | response = []; 75 | for(var index=0; index < span; index++){ 76 | // Dup the value for each header this cell belongs to 77 | response.push(value); 78 | } 79 | } 80 | } 81 | return response; 82 | }, 83 | 84 | init: function () { 85 | $.proxy(function() { 86 | /** 87 | Fired when element was initialized by `$().tableToJSON()` method. 88 | Please note that you should setup `init` handler **before** applying `tableToJSON`. 89 | 90 | @event init 91 | @param {Object} event event object 92 | @param {Object} editable TableToJSONCell instance 93 | **/ 94 | this.$element.triggerHandler('init', this); 95 | }, this); 96 | } 97 | }; 98 | 99 | // Initialize cell 100 | $.fn.tableToJSONCell = function (index, options) { 101 | return new TableToJSONCell(this, index, options); 102 | }; 103 | 104 | $.fn.tableToJSONCell.defaults = { 105 | /** 106 | * Boolean if HTML tags in table cells should be preserved. 107 | * @type boolean 108 | * @default false 109 | */ 110 | allowHTML: false, 111 | 112 | /** 113 | * String of the data-* attribute name to use for the override value. 114 | * @type String 115 | * @default 'override' 116 | */ 117 | textDataOverride: 'data-override', 118 | 119 | /** 120 | * Function that is used on all tbody cells to extract text from the cells. 121 | * Note: A value in `data-override` will prevent this function from being called. 122 | * @type Function|Object 123 | * @default null 124 | */ 125 | extractor: null, 126 | 127 | /** 128 | * alias for extractor option. 129 | */ 130 | textExtractor: null 131 | }; 132 | })(jQuery); 133 | -------------------------------------------------------------------------------- /src/tabletojson-row.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | 'use strict'; 3 | 4 | var TableToJSONRow = function (row, options) { 5 | this.$element = $(row); 6 | this.cells = []; 7 | this.options = $.extend({}, $.fn.tableToJSONRow.defaults, options); 8 | this.init(); 9 | }; 10 | 11 | TableToJSONRow.prototype = { 12 | constructor: TableToJSONRow, 13 | 14 | id: function(){ 15 | return this.$element.attr('id') ? this.$element.attr('id') : null; 16 | }, 17 | 18 | valuesWithHeadings: function(headers){ 19 | var valuesWithHeadings = {}; 20 | var values = this.values(); 21 | for(var index = 0; index < values.length; index++){ 22 | valuesWithHeadings[headers[index]] = values[index]; 23 | } 24 | return valuesWithHeadings; 25 | }, 26 | 27 | isEmpty: function(){ 28 | var empty = true; 29 | var values = this.values(); 30 | for(var index = 0; empty && index < values.length; index++){ 31 | if(values[index] !== ''){ 32 | empty = false; 33 | } 34 | } 35 | return empty; 36 | }, 37 | 38 | cell: function(index){ 39 | if(index < this.cells.length){ 40 | return this.cells[index]; 41 | } else { 42 | return null; 43 | } 44 | }, 45 | 46 | insert: function(index, cell){ 47 | this.cells.splice(index, 0, cell); 48 | }, 49 | 50 | getRowSpans: function(spans){ 51 | var span, rows = [], cell; 52 | for(var cellIndex = 0; cellIndex < this.cells.length; cellIndex++){ 53 | rows = []; 54 | cell = this.cells[cellIndex]; 55 | if(cell){ 56 | span = cell.rowSpan(); 57 | while(span > 1){ 58 | rows.push(cell); 59 | span--; 60 | } 61 | cell.rowSpan(1); 62 | } 63 | if(rows.length > 0){ 64 | spans[cellIndex] = rows; 65 | } 66 | } 67 | return spans; 68 | }, 69 | 70 | insertRowSpans: function(spans){ 71 | for(var cellIndex = 0; cellIndex < spans.length; cellIndex++) { 72 | if (spans[cellIndex] && spans[cellIndex].length > 0) { 73 | var spannedCell = spans[cellIndex].splice(0, 1)[0]; 74 | this.insert(cellIndex, spannedCell); 75 | } 76 | } 77 | return spans; 78 | }, 79 | 80 | rowSpans: function(){ 81 | var spans = [], span, rows = [], cell; 82 | for(var cellIndex = 0; cellIndex < this.cells.length; cellIndex++){ 83 | rows = []; 84 | cell = this.cells[cellIndex]; 85 | span = cell.rowSpan(); 86 | while(span > 1){ 87 | rows.push(cell); 88 | span--; 89 | } 90 | cell.rowSpan(1); 91 | if(rows.length > 0){ 92 | spans[cellIndex] = rows; 93 | } 94 | } 95 | return spans; 96 | }, 97 | 98 | values: function(options){ 99 | var runOptions = $.extend({}, this.options, options); 100 | var cellValues = [], value = null, colSpanOffset = 0; 101 | for(var index = 0; index < this.cells.length; index++){ 102 | value = this.cells[index].value(runOptions); 103 | 104 | if (this.cells[index].colSpan() === 1) { 105 | // simple case, either ignore it or not 106 | if(!this.ignoreColumn(colSpanOffset)){ 107 | cellValues = cellValues.concat(value); 108 | } 109 | colSpanOffset++; 110 | } else { 111 | // this cell has a colSpan, ensure each 112 | // value match ignored columns 113 | for (var valuesIndex = 0; valuesIndex < value.length; valuesIndex++) { 114 | if (!this.ignoreColumn(colSpanOffset)) { 115 | cellValues = cellValues.concat(value[valuesIndex]); 116 | } 117 | colSpanOffset++; 118 | } 119 | } 120 | } 121 | return cellValues; 122 | }, 123 | 124 | ignoreColumn: function(index){ 125 | if( this.options.onlyColumns ) { 126 | return this.options.onlyColumns.indexOf(index) < 0; 127 | } 128 | return this.options.ignoreColumns.indexOf(index) > -1; 129 | }, 130 | 131 | init: function () { 132 | // Init Cells 133 | var self = this; 134 | this.$element.children(this.options.cellSelector).each(function(cellIndex, cell) { 135 | self.cells.push( $(cell).tableToJSONCell(cellIndex, self.options) ); 136 | }); 137 | 138 | // Finalize init 139 | $.proxy(function() { 140 | /** 141 | Fired when row was initialized by `$().tableToJSON()` method. 142 | Please note that you should setup `init` handler **before** applying `tableToJSON`. 143 | 144 | @event init 145 | @param {Object} event event object 146 | @param {Object} editable TableToJSONRow instance 147 | **/ 148 | this.$element.triggerHandler('init', this); 149 | }, this); 150 | } 151 | }; 152 | 153 | // Initialize row 154 | $.fn.tableToJSONRow = function (options) { 155 | return new TableToJSONRow(this, options); 156 | }; 157 | 158 | $.fn.tableToJSONRow.defaults = { 159 | /** 160 | Array of column indexes to include, all other columns are ignored. This takes precedence over ignoreColumns when provided. 161 | 162 | @type Array 163 | @default null 164 | **/ 165 | onlyColumns: null, 166 | 167 | /** 168 | Array of column indexes to ignore. 169 | 170 | @type Array 171 | @default [] 172 | **/ 173 | ignoreColumns: [], 174 | cellSelector: 'td,th' 175 | }; 176 | })(jQuery); 177 | -------------------------------------------------------------------------------- /src/tabletojson.js: -------------------------------------------------------------------------------- 1 | (function( $ ) { 2 | 'use strict'; 3 | 4 | var TableToJSON = function (table, options) { 5 | this.$element = $(table); 6 | this.rows = []; 7 | this.options = $.extend({}, $.fn.tableToJSON.defaults, options); 8 | this.init(); 9 | }; 10 | 11 | TableToJSON.prototype = { 12 | constructor: TableToJSON, 13 | 14 | headings: function() { 15 | if(this.rows.length > 0 && !this.options.headings){ 16 | // have to disable the extractor for header rows 17 | return this.rows[0].values({extractor: null, textExtractor: null}); 18 | } else if (this.options.headings){ 19 | return this.options.headings; 20 | } else { 21 | return []; 22 | } 23 | }, 24 | 25 | values: function() { 26 | var rowValues = []; 27 | var headings = this.headings(); 28 | var index = (this.options.headings) ? 0 : 1; // Skip first row if heading are not supplied 29 | for(index; index < this.rows.length; index++){ 30 | if(!this.ignoreRow(this.rows[index], index)){ 31 | if(this.options.includeRowId){ 32 | var rowIdHeader = (typeof this.options.includeRowId === 'string') ? this.options.includeRowId : 'rowId'; 33 | var cellValues = this.rows[index].valuesWithHeadings(headings); 34 | cellValues[rowIdHeader] = this.rows[index].id(); 35 | rowValues.push( cellValues ); 36 | } else { 37 | rowValues.push( this.rows[index].valuesWithHeadings(headings) ); 38 | } 39 | } 40 | } 41 | return rowValues; 42 | }, 43 | 44 | ignoreRow: function($row, index) { 45 | return ( 46 | this.options.ignoreRows && this.options.ignoreRows.indexOf(index) > -1) || 47 | ($row.$element.data('ignore') && $row.$element.data('ignore') !== 'false') || 48 | (this.options.ignoreHiddenRows && !$row.$element.is(':visible')) || 49 | (this.options.ignoreEmptyRows && $row.isEmpty()); 50 | }, 51 | 52 | addRow: function($row, rowSpans) { 53 | $row.insertRowSpans(rowSpans); 54 | this.rows.push($row); 55 | return $row.getRowSpans(rowSpans); 56 | }, 57 | 58 | init: function () { 59 | // Init Rows 60 | var self = this, rowSpans = [], newRow = null; 61 | this.$element.children(this.options.rowParentSelector).children(this.options.rowSelector).each(function(rowIndex, row) { 62 | newRow = $(row).tableToJSONRow(self.options); 63 | rowSpans = self.addRow( newRow, rowSpans ); 64 | }); 65 | 66 | $.proxy(function() { 67 | /** 68 | Fired when element was initialized by `$().tableToJSON()` method. 69 | Please note that you should setup `init` handler **before** applying `tableToJSON`. 70 | 71 | @event init 72 | @param {Object} event event object 73 | @param {Object} editable TableToJSON instance 74 | **/ 75 | this.$element.triggerHandler('init', this); 76 | }, this); 77 | } 78 | }; 79 | 80 | // Initialize 81 | $.fn.tableToJSON = function (options) { 82 | var table = new TableToJSON(this, options); 83 | return table.values(); 84 | }; 85 | 86 | $.fn.tableToJSON.defaults = { 87 | /** 88 | * Array of row indexes to ignore. 89 | * @type Array 90 | * @default [] 91 | */ 92 | ignoreRows: [], 93 | 94 | /** 95 | * Boolean if hidden rows should be ignored or not. 96 | * @type Boolean 97 | * @default true 98 | */ 99 | ignoreHiddenRows: true, 100 | 101 | /** 102 | * Boolean if hidden rows should be ignored or not. 103 | * @type Boolean 104 | * @default false 105 | */ 106 | ignoreEmptyRows: false, 107 | 108 | /** 109 | * Array of column headings to use. When supplied, all table rows are treated as values (no headings row). 110 | * @type Array 111 | * @default null 112 | */ 113 | headings: null, 114 | 115 | /** 116 | * Determines if the `id` attribute of each `` element is included in the JSON. 117 | * @type Boolean 118 | * @default false 119 | */ 120 | includeRowId: false, 121 | 122 | rowParentSelector: 'tbody,*', 123 | rowSelector: 'tr' 124 | }; 125 | })( jQuery ); 126 | -------------------------------------------------------------------------------- /tabletojson.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tabletojson", 3 | "title": "Table To JSON", 4 | "description": "jQuery plugin that reads an HTML table and returns a javascript object representing the values and columns of the table", 5 | "keywords": [ 6 | "table", 7 | "serialize", 8 | "tabletojson", 9 | "json" 10 | ], 11 | "version": "1.0.0", 12 | "author": { 13 | "name": "Daniel White", 14 | "url": "https://www.github.developerdan.com/" 15 | }, 16 | "licenses": [ 17 | { 18 | "type": "MIT", 19 | "url": "https://github.com/lightswitch05/table-to-json/blob/master/MIT-LICENSE" 20 | } 21 | ], 22 | "bugs": "https://github.com/lightswitch05/table-to-json/issues", 23 | "homepage": "https://github.com/lightswitch05/table-to-json", 24 | "docs": "https://github.com/lightswitch05/table-to-json", 25 | "download": "https://github.com/lightswitch05/table-to-json", 26 | "demo": "https://lightswitch05.github.io/table-to-json/", 27 | "dependencies": { 28 | "jquery": ">=1.5" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Table To JSON Test 6 | 7 | 8 | 9 | 10 | 11 |

Table To Json Test Suite

12 |

13 |
14 |

15 |
    16 |
    17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /test/specs-legacy/content.test.js: -------------------------------------------------------------------------------- 1 | /*global module, test, expect, deepEqual */ 2 | 3 | module('legacy-content'); 4 | 5 | /* Ignore nested tables in cells with just td's*/ 6 | test('ignore nested in cells', function() { 7 | $('#qunit-fixture').html( 8 | '' + 9 | '' + 10 | '' + 11 | '' + 12 | '' + 13 | '' + 14 | '' + 15 | '' + 16 | '' + 17 | '' + 18 | '' + 19 | '' + 20 | '' + 21 | '' + 22 | '' + 23 | '' + 24 | '' + 25 | '' + 26 | '' + 27 | '' + 28 | '' + 29 | '
    First NameLast NamePoints
    JillSmith50
    Eve
    1
    2
    Jackson94
    JohnDoe80
    ' 30 | ); 31 | 32 | 33 | expect(1); 34 | var table = $('#test-table').tableToJSON(); 35 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 36 | {'First Name':'Eve 12', 'Last Name':'Jackson', 'Points':'94'}, 37 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 38 | deepEqual(table, expected); 39 | }); 40 | 41 | /* Ignore nested tables in cells with th's and td's*/ 42 | test('ignore nested and in cells', function() { 43 | $('#qunit-fixture').html( 44 | '' + 45 | '' + 46 | '' + 47 | '' + 48 | '' + 49 | '' + 50 | '' + 51 | '' + 52 | '' + 53 | '' + 54 | '' + 55 | '' + 56 | '' + 57 | '' + 58 | '' + 59 | '' + 60 | '' + 61 | '' + 62 | '' + 63 | '' + 64 | '' + 65 | '
    First NameLast NamePoints
    JillSmith50
    Eve
    number
    1
    2
    Jackson94
    JohnDoe80
    ' 66 | ); 67 | 68 | 69 | expect(1); 70 | var table = $('#test-table').tableToJSON(); 71 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 72 | {'First Name':'Eve number12', 'Last Name':'Jackson', 'Points':'94'}, 73 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 74 | deepEqual(table, expected); 75 | }); 76 | 77 | /* Ignore nested tables in headings with just td's*/ 78 | test('ignore nested in headings', function() { 79 | $('#qunit-fixture').html( 80 | '' + 81 | '' + 82 | '' + 83 | '' + 84 | '' + 85 | '' + 86 | '' + 87 | '' + 88 | '' + 89 | '' + 90 | '' + 91 | '' + 92 | '' + 93 | '' + 94 | '' + 95 | '' + 96 | '' + 97 | '' + 98 | '' + 99 | '' + 100 | '' + 101 | '
    First NameLast Name
    1
    2
    Points
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 102 | ); 103 | 104 | 105 | expect(1); 106 | var table = $('#test-table').tableToJSON(); 107 | var expected = [{'First Name':'Jill', 'Last Name 12':'Smith', 'Points':'50'}, 108 | {'First Name':'Eve', 'Last Name 12':'Jackson', 'Points':'94'}, 109 | {'First Name':'John', 'Last Name 12':'Doe', 'Points':'80'}]; 110 | deepEqual(table, expected); 111 | }); 112 | 113 | /* Ignore nested tables in headings with th's and td's*/ 114 | test('ignore nested and in headings', function() { 115 | $('#qunit-fixture').html( 116 | '' + 117 | '' + 118 | '' + 119 | '' + 120 | '' + 121 | '' + 122 | '' + 123 | '' + 124 | '' + 125 | '' + 126 | '' + 127 | '' + 128 | '' + 129 | '' + 130 | '' + 131 | '' + 132 | '' + 133 | '' + 134 | '' + 135 | '' + 136 | '' + 137 | '
    First NameLast Name
    number
    1
    2
    Points
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 138 | ); 139 | 140 | 141 | expect(1); 142 | var table = $('#test-table').tableToJSON(); 143 | var expected = [{'First Name':'Jill', 'Last Name number12':'Smith', 'Points':'50'}, 144 | {'First Name':'Eve', 'Last Name number12':'Jackson', 'Points':'94'}, 145 | {'First Name':'John', 'Last Name number12':'Doe', 'Points':'80'}]; 146 | deepEqual(table, expected); 147 | }); 148 | 149 | /* Links are retrieved as values */ 150 | test('links are just values', function() { 151 | $('#qunit-fixture').html( 152 | '' + 153 | '' + 154 | '' + 155 | '' + 156 | '' + 157 | '' + 158 | '' + 159 | '' + 160 | '' + 161 | '' + 162 | '' + 163 | '' + 164 | '' + 165 | '' + 166 | '' + 167 | '' + 168 | '' + 169 | '' + 170 | '' + 171 | '' + 172 | '' + 173 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 174 | ); 175 | 176 | 177 | expect(1); 178 | var table = $('#test-table').tableToJSON(); 179 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 180 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 181 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 182 | deepEqual(table, expected); 183 | }); 184 | 185 | /* A complex table */ 186 | test('complex table', function() { 187 | $('#qunit-fixture').html( 188 | '
    Status Location Zone Tier Install Calc Install Rate
    INCOMPLETE location 1 QTY B 9.5
    INCOMPLETE Empty Empty Empty Empty Empty
    ' 189 | ); 190 | 191 | 192 | expect(1); 193 | var table = $('#test-table').tableToJSON(); 194 | var expected = [{'id':'oaWd4cs2JHY660pG', 'State':'INCOMPLETE', 'BCS':'location', 'ZONE':'1', 'TIER':'QTY', 'INSTALL_CALC':'B', 'INSTALL_R1':'9.5'}]; 195 | deepEqual(table, expected); 196 | }); 197 | 198 | /* A table with rowspan & colspan */ 199 | test('rowspan & colspan in tbody', function() { 200 | $('#qunit-fixture').html( 201 | '' + 202 | '' + 203 | '' + 204 | '' + 205 | '' + 206 | '' + 207 | '' + 208 | '' + 209 | '
    linevalue1value2value3value4
    11.11.21.31.4
    1.51.61.7
    22.12.22.32.4
    2.52.62.7
    33.13.2
    3.43.5
    ' 210 | ); 211 | 212 | expect(1); 213 | var table = $('#test-table').tableToJSON(); 214 | var expected = [ 215 | {'line':'1','value1':'1.1','value2':'1.2','value3':'1.3','value4':'1.4'}, 216 | {'line':'1','value1':'1.5','value2':'1.6','value3':'1.7','value4':'1.4'}, 217 | {'line':'2','value1':'2.1','value2':'2.2','value3':'2.3','value4':'2.4'}, 218 | {'line':'2','value1':'2.5','value2':'2.5','value3':'2.6','value4':'2.7'}, 219 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.2','value4':'3.2'}, 220 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.4','value4':'3.5'} 221 | ]; 222 | deepEqual(table, expected); 223 | }); 224 | 225 | /* A table with rowspan & colspan & ignoreColumns */ 226 | test('ignoreColumns with rowspan & colspan in tbody', function() { 227 | $('#qunit-fixture').html( 228 | '' + 229 | '' + 230 | '' + 231 | '' + 232 | '' + 233 | '' + 234 | '' + 235 | '' + 236 | '
    linevalue1value2value3value4
    11.11.21.31.4
    1.51.61.7
    22.12.22.32.4
    2.52.62.7
    33.13.2
    3.43.5
    ' 237 | ); 238 | 239 | expect(5); 240 | var $table = $('#test-table'); 241 | $table.find('tr:eq(0)').hide(); 242 | var table = $table.tableToJSON({ headings: [ 'v1','v2','v3','v4' ], ignoreColumns : [0] }); 243 | $table.find('tr:eq(0)').show(); 244 | var expected = [ 245 | {'v1':'1.1','v2':'1.2','v3':'1.3','v4':'1.4'}, 246 | {'v1':'1.5','v2':'1.6','v3':'1.7','v4':'1.4'}, 247 | {'v1':'2.1','v2':'2.2','v3':'2.3','v4':'2.4'}, 248 | {'v1':'2.5','v2':'2.5','v3':'2.6','v4':'2.7'}, 249 | {'v1':'3.1','v2':'3.1','v3':'3.2','v4':'3.2'}, 250 | {'v1':'3.1','v2':'3.1','v3':'3.4','v4':'3.5'} 251 | ]; 252 | deepEqual(table, expected); 253 | 254 | table = $table.tableToJSON({ ignoreColumns : [1] }); 255 | expected = [ 256 | {'line':'1','value2':'1.2','value3':'1.3','value4':'1.4'}, 257 | {'line':'1','value2':'1.6','value3':'1.7','value4':'1.4'}, 258 | {'line':'2','value2':'2.2','value3':'2.3','value4':'2.4'}, 259 | {'line':'2','value2':'2.5','value3':'2.6','value4':'2.7'}, 260 | {'line':'3','value2':'3.1','value3':'3.2','value4':'3.2'}, 261 | {'line':'3','value2':'3.1','value3':'3.4','value4':'3.5'} 262 | ]; 263 | deepEqual(table, expected); 264 | 265 | table = $table.tableToJSON({ ignoreColumns : [2] }); 266 | expected = [ 267 | {'line':'1','value1':'1.1','value3':'1.3','value4':'1.4'}, 268 | {'line':'1','value1':'1.5','value3':'1.7','value4':'1.4'}, 269 | {'line':'2','value1':'2.1','value3':'2.3','value4':'2.4'}, 270 | {'line':'2','value1':'2.5','value3':'2.6','value4':'2.7'}, 271 | {'line':'3','value1':'3.1','value3':'3.2','value4':'3.2'}, 272 | {'line':'3','value1':'3.1','value3':'3.4','value4':'3.5'} 273 | ]; 274 | deepEqual(table, expected); 275 | 276 | table = $table.tableToJSON({ ignoreColumns : [3] }); 277 | expected = [ 278 | {'line':'1','value1':'1.1','value2':'1.2','value4':'1.4'}, 279 | {'line':'1','value1':'1.5','value2':'1.6','value4':'1.4'}, 280 | {'line':'2','value1':'2.1','value2':'2.2','value4':'2.4'}, 281 | {'line':'2','value1':'2.5','value2':'2.5','value4':'2.7'}, 282 | {'line':'3','value1':'3.1','value2':'3.1','value4':'3.2'}, 283 | {'line':'3','value1':'3.1','value2':'3.1','value4':'3.5'} 284 | ]; 285 | deepEqual(table, expected); 286 | 287 | table = $table.tableToJSON({ ignoreColumns : [4] }); 288 | expected = [ 289 | {'line':'1','value1':'1.1','value2':'1.2','value3':'1.3'}, 290 | {'line':'1','value1':'1.5','value2':'1.6','value3':'1.7'}, 291 | {'line':'2','value1':'2.1','value2':'2.2','value3':'2.3'}, 292 | {'line':'2','value1':'2.5','value2':'2.5','value3':'2.6'}, 293 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.2'}, 294 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.4'} 295 | ]; 296 | deepEqual(table, expected); 297 | 298 | }); 299 | 300 | /* A complex table with row & col spans */ 301 | test('complex table with row & col spans', function() { 302 | $('#qunit-fixture').html( 303 | '
    Rota Descrição Descritor Ano Mês (completo) Dia Intervalo 30min Totais
    nullAutoAtendimento201501-Jan0611:00:001
    12:00:003
    Fila201501-Jan0611:00:001
    12:00:002
    2711:30:001
    12:00:001
    15:00:004
    16:00:002
    Inicio201501-Jan0611:00:002
    12:00:005
    2711:30:001
    12:00:001
    15:00:004
    16:00:002
    Rota externaAutoAtendimento201501-Jan0612:00:001
    12:30:002
    Fila201501-Jan0612:00:001
    12:30:002
    2710:30:001
    11:00:002
    11:30:001
    12:00:002
    16:00:002
    18:30:001
    19:00:001
    Inicio201501-Jan0612:00:002
    12:30:004
    2710:30:002
    11:00:002
    11:30:001
    12:00:002
    16:00:002
    18:30:001
    19:00:001
    3020:00:001
    02-Feb0314:30:001
    Totais 34
    ' 304 | ); 305 | 306 | 307 | expect(1); 308 | var table = $('#test-table').tableToJSON(); 309 | var expected = [ 310 | { 311 | 'Ano': '2015', 312 | 'Descrição Descritor': 'AutoAtendimento', 313 | 'Dia': '06', 314 | 'Intervalo 30min': '11:00:00', 315 | 'Mês (completo)': '01-Jan', 316 | 'Rota': 'null', 317 | 'Totais': '1' 318 | }, 319 | { 320 | 'Ano': '2015', 321 | 'Descrição Descritor': 'AutoAtendimento', 322 | 'Dia': '06', 323 | 'Intervalo 30min': '12:00:00', 324 | 'Mês (completo)': '01-Jan', 325 | 'Rota': 'null', 326 | 'Totais': '3' 327 | }, 328 | { 329 | 'Ano': '2015', 330 | 'Descrição Descritor': 'Fila', 331 | 'Dia': '06', 332 | 'Intervalo 30min': '11:00:00', 333 | 'Mês (completo)': '01-Jan', 334 | 'Rota': 'null', 335 | 'Totais': '1' 336 | }, 337 | { 338 | 'Ano': '2015', 339 | 'Descrição Descritor': 'Fila', 340 | 'Dia': '06', 341 | 'Intervalo 30min': '12:00:00', 342 | 'Mês (completo)': '01-Jan', 343 | 'Rota': 'null', 344 | 'Totais': '2' 345 | }, 346 | { 347 | 'Ano': '2015', 348 | 'Descrição Descritor': 'Fila', 349 | 'Dia': '27', 350 | 'Intervalo 30min': '11:30:00', 351 | 'Mês (completo)': '01-Jan', 352 | 'Rota': 'null', 353 | 'Totais': '1' 354 | }, 355 | { 356 | 'Ano': '2015', 357 | 'Descrição Descritor': 'Fila', 358 | 'Dia': '27', 359 | 'Intervalo 30min': '12:00:00', 360 | 'Mês (completo)': '01-Jan', 361 | 'Rota': 'null', 362 | 'Totais': '1' 363 | }, 364 | { 365 | 'Ano': '2015', 366 | 'Descrição Descritor': 'Fila', 367 | 'Dia': '27', 368 | 'Intervalo 30min': '15:00:00', 369 | 'Mês (completo)': '01-Jan', 370 | 'Rota': 'null', 371 | 'Totais': '4' 372 | }, 373 | { 374 | 'Ano': '2015', 375 | 'Descrição Descritor': 'Fila', 376 | 'Dia': '27', 377 | 'Intervalo 30min': '16:00:00', 378 | 'Mês (completo)': '01-Jan', 379 | 'Rota': 'null', 380 | 'Totais': '2' 381 | }, 382 | { 383 | 'Ano': '2015', 384 | 'Descrição Descritor': 'Inicio', 385 | 'Dia': '06', 386 | 'Intervalo 30min': '11:00:00', 387 | 'Mês (completo)': '01-Jan', 388 | 'Rota': 'null', 389 | 'Totais': '2' 390 | }, 391 | { 392 | 'Ano': '2015', 393 | 'Descrição Descritor': 'Inicio', 394 | 'Dia': '06', 395 | 'Intervalo 30min': '12:00:00', 396 | 'Mês (completo)': '01-Jan', 397 | 'Rota': 'null', 398 | 'Totais': '5' 399 | }, 400 | { 401 | 'Ano': '2015', 402 | 'Descrição Descritor': 'Inicio', 403 | 'Dia': '27', 404 | 'Intervalo 30min': '11:30:00', 405 | 'Mês (completo)': '01-Jan', 406 | 'Rota': 'null', 407 | 'Totais': '1' 408 | }, 409 | { 410 | 'Ano': '2015', 411 | 'Descrição Descritor': 'Inicio', 412 | 'Dia': '27', 413 | 'Intervalo 30min': '12:00:00', 414 | 'Mês (completo)': '01-Jan', 415 | 'Rota': 'null', 416 | 'Totais': '1' 417 | }, 418 | { 419 | 'Ano': '2015', 420 | 'Descrição Descritor': 'Inicio', 421 | 'Dia': '27', 422 | 'Intervalo 30min': '15:00:00', 423 | 'Mês (completo)': '01-Jan', 424 | 'Rota': 'null', 425 | 'Totais': '4' 426 | }, 427 | { 428 | 'Ano': '2015', 429 | 'Descrição Descritor': 'Inicio', 430 | 'Dia': '27', 431 | 'Intervalo 30min': '16:00:00', 432 | 'Mês (completo)': '01-Jan', 433 | 'Rota': 'null', 434 | 'Totais': '2' 435 | }, 436 | { 437 | 'Ano': '2015', 438 | 'Descrição Descritor': 'AutoAtendimento', 439 | 'Dia': '06', 440 | 'Intervalo 30min': '12:00:00', 441 | 'Mês (completo)': '01-Jan', 442 | 'Rota': 'Rota externa', 443 | 'Totais': '1' 444 | }, 445 | { 446 | 'Ano': '2015', 447 | 'Descrição Descritor': 'AutoAtendimento', 448 | 'Dia': '06', 449 | 'Intervalo 30min': '12:30:00', 450 | 'Mês (completo)': '01-Jan', 451 | 'Rota': 'Rota externa', 452 | 'Totais': '2' 453 | }, 454 | { 455 | 'Ano': '2015', 456 | 'Descrição Descritor': 'Fila', 457 | 'Dia': '06', 458 | 'Intervalo 30min': '12:00:00', 459 | 'Mês (completo)': '01-Jan', 460 | 'Rota': 'Rota externa', 461 | 'Totais': '1' 462 | }, 463 | { 464 | 'Ano': '2015', 465 | 'Descrição Descritor': 'Fila', 466 | 'Dia': '06', 467 | 'Intervalo 30min': '12:30:00', 468 | 'Mês (completo)': '01-Jan', 469 | 'Rota': 'Rota externa', 470 | 'Totais': '2' 471 | }, 472 | { 473 | 'Ano': '2015', 474 | 'Descrição Descritor': 'Fila', 475 | 'Dia': '27', 476 | 'Intervalo 30min': '10:30:00', 477 | 'Mês (completo)': '01-Jan', 478 | 'Rota': 'Rota externa', 479 | 'Totais': '1' 480 | }, 481 | { 482 | 'Ano': '2015', 483 | 'Descrição Descritor': 'Fila', 484 | 'Dia': '27', 485 | 'Intervalo 30min': '11:00:00', 486 | 'Mês (completo)': '01-Jan', 487 | 'Rota': 'Rota externa', 488 | 'Totais': '2' 489 | }, 490 | { 491 | 'Ano': '2015', 492 | 'Descrição Descritor': 'Fila', 493 | 'Dia': '27', 494 | 'Intervalo 30min': '11:30:00', 495 | 'Mês (completo)': '01-Jan', 496 | 'Rota': 'Rota externa', 497 | 'Totais': '1' 498 | }, 499 | { 500 | 'Ano': '2015', 501 | 'Descrição Descritor': 'Fila', 502 | 'Dia': '27', 503 | 'Intervalo 30min': '12:00:00', 504 | 'Mês (completo)': '01-Jan', 505 | 'Rota': 'Rota externa', 506 | 'Totais': '2' 507 | }, 508 | { 509 | 'Ano': '2015', 510 | 'Descrição Descritor': 'Fila', 511 | 'Dia': '27', 512 | 'Intervalo 30min': '16:00:00', 513 | 'Mês (completo)': '01-Jan', 514 | 'Rota': 'Rota externa', 515 | 'Totais': '2' 516 | }, 517 | { 518 | 'Ano': '2015', 519 | 'Descrição Descritor': 'Fila', 520 | 'Dia': '27', 521 | 'Intervalo 30min': '18:30:00', 522 | 'Mês (completo)': '01-Jan', 523 | 'Rota': 'Rota externa', 524 | 'Totais': '1' 525 | }, 526 | { 527 | 'Ano': '2015', 528 | 'Descrição Descritor': 'Fila', 529 | 'Dia': '27', 530 | 'Intervalo 30min': '19:00:00', 531 | 'Mês (completo)': '01-Jan', 532 | 'Rota': 'Rota externa', 533 | 'Totais': '1' 534 | }, 535 | { 536 | 'Ano': '2015', 537 | 'Descrição Descritor': 'Inicio', 538 | 'Dia': '06', 539 | 'Intervalo 30min': '12:00:00', 540 | 'Mês (completo)': '01-Jan', 541 | 'Rota': 'Rota externa', 542 | 'Totais': '2' 543 | }, 544 | { 545 | 'Ano': '2015', 546 | 'Descrição Descritor': 'Inicio', 547 | 'Dia': '06', 548 | 'Intervalo 30min': '12:30:00', 549 | 'Mês (completo)': '01-Jan', 550 | 'Rota': 'Rota externa', 551 | 'Totais': '4' 552 | }, 553 | { 554 | 'Ano': '2015', 555 | 'Descrição Descritor': 'Inicio', 556 | 'Dia': '27', 557 | 'Intervalo 30min': '10:30:00', 558 | 'Mês (completo)': '01-Jan', 559 | 'Rota': 'Rota externa', 560 | 'Totais': '2' 561 | }, 562 | { 563 | 'Ano': '2015', 564 | 'Descrição Descritor': 'Inicio', 565 | 'Dia': '27', 566 | 'Intervalo 30min': '11:00:00', 567 | 'Mês (completo)': '01-Jan', 568 | 'Rota': 'Rota externa', 569 | 'Totais': '2' 570 | }, 571 | { 572 | 'Ano': '2015', 573 | 'Descrição Descritor': 'Inicio', 574 | 'Dia': '27', 575 | 'Intervalo 30min': '11:30:00', 576 | 'Mês (completo)': '01-Jan', 577 | 'Rota': 'Rota externa', 578 | 'Totais': '1' 579 | }, 580 | { 581 | 'Ano': '2015', 582 | 'Descrição Descritor': 'Inicio', 583 | 'Dia': '27', 584 | 'Intervalo 30min': '12:00:00', 585 | 'Mês (completo)': '01-Jan', 586 | 'Rota': 'Rota externa', 587 | 'Totais': '2' 588 | }, 589 | { 590 | 'Ano': '2015', 591 | 'Descrição Descritor': 'Inicio', 592 | 'Dia': '27', 593 | 'Intervalo 30min': '16:00:00', 594 | 'Mês (completo)': '01-Jan', 595 | 'Rota': 'Rota externa', 596 | 'Totais': '2' 597 | }, 598 | { 599 | 'Ano': '2015', 600 | 'Descrição Descritor': 'Inicio', 601 | 'Dia': '27', 602 | 'Intervalo 30min': '18:30:00', 603 | 'Mês (completo)': '01-Jan', 604 | 'Rota': 'Rota externa', 605 | 'Totais': '1' 606 | }, 607 | { 608 | 'Ano': '2015', 609 | 'Descrição Descritor': 'Inicio', 610 | 'Dia': '27', 611 | 'Intervalo 30min': '19:00:00', 612 | 'Mês (completo)': '01-Jan', 613 | 'Rota': 'Rota externa', 614 | 'Totais': '1' 615 | }, 616 | { 617 | 'Ano': '2015', 618 | 'Descrição Descritor': 'Inicio', 619 | 'Dia': '30', 620 | 'Intervalo 30min': '20:00:00', 621 | 'Mês (completo)': '01-Jan', 622 | 'Rota': 'Rota externa', 623 | 'Totais': '1' 624 | }, 625 | { 626 | 'Ano': '2015', 627 | 'Descrição Descritor': 'Inicio', 628 | 'Dia': '03', 629 | 'Intervalo 30min': '14:30:00', 630 | 'Mês (completo)': '02-Feb', 631 | 'Rota': 'Rota externa', 632 | 'Totais': '1' 633 | }, 634 | { 635 | 'Ano': 'Totais', 636 | 'Descrição Descritor': 'Totais', 637 | 'Dia': 'Totais', 638 | 'Intervalo 30min': 'Totais', 639 | 'Mês (completo)': 'Totais', 640 | 'Rota': 'Totais', 641 | 'Totais': '34' 642 | } 643 | ]; 644 | deepEqual(table, expected); 645 | }); 646 | 647 | test('ignore empty rows should take into account ignore columns feature', function() { 648 | $('#qunit-fixture').html( 649 | '' + 650 | '' + 651 | '' + 652 | '' + 653 | '' + 654 | '' + 655 | '' + 656 | '' + 657 | '' + 658 | '' + 659 | '' + 660 | '' + 661 | '' + 662 | '' + 663 | '' + 664 | '' + 665 | '' + 666 | '' + 667 | '' + 668 | '' + 669 | '' + 670 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    this not empty - but ignored
    ' 671 | ); 672 | 673 | 674 | expect(1); 675 | var table = $('#test-table').tableToJSON({ignoreEmptyRows:true, ignoreColumns:[2]}); 676 | var expected = [ 677 | {'First Name':'Jill', 'Last Name':'Smith'}, 678 | {'First Name':'Eve', 'Last Name':'Jackson'} 679 | ]; 680 | deepEqual(table, expected); 681 | }); 682 | -------------------------------------------------------------------------------- /test/specs-legacy/core.test.js: -------------------------------------------------------------------------------- 1 | /*global module, test, expect, deepEqual */ 2 | 3 | module('legacy-core'); 4 | 5 | /* Basic Usage */ 6 | test('basic usage', function() { 7 | $('#qunit-fixture').html( 8 | '' + 9 | '' + 10 | '' + 11 | '' + 12 | '' + 13 | '' + 14 | '' + 15 | '' + 16 | '' + 17 | '' + 18 | '' + 19 | '' + 20 | '' + 21 | '' + 22 | '' + 23 | '' + 24 | '' + 25 | '' + 26 | '' + 27 | '' + 28 | '' + 29 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 30 | ); 31 | 32 | 33 | expect(1); 34 | var table = $('#test-table').tableToJSON(); 35 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 36 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 37 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 38 | deepEqual(table, expected); 39 | }); 40 | 41 | /* Basic Usage with thead and tbody*/ 42 | test('basic usage with thead and tbody', function() { 43 | $('#qunit-fixture').html( 44 | '' + 45 | '' + 46 | '' + 47 | '' + 48 | '' + 49 | '' + 50 | '' + 51 | '' + 52 | '' + 53 | '' + 54 | '' + 55 | '' + 56 | '' + 57 | '' + 58 | '' + 59 | '' + 60 | '' + 61 | '' + 62 | '' + 63 | '' + 64 | '' + 65 | '' + 66 | '' + 67 | '' + 68 | '' + 69 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 70 | ); 71 | 72 | 73 | expect(1); 74 | var table = $('#test-table').tableToJSON(); 75 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 76 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 77 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 78 | deepEqual(table, expected); 79 | }); 80 | 81 | /* Override Column Names */ 82 | test('override column names', function() { 83 | $('#qunit-fixture').html( 84 | '' + 85 | '' + 86 | '' + 87 | '' + 88 | '' + 89 | '' + 90 | '' + 91 | '' + 92 | '' + 93 | '' + 94 | '' + 95 | '' + 96 | '' + 97 | '' + 98 | '' + 99 | '' + 100 | '' + 101 | '' + 102 | '' + 103 | '' + 104 | '' + 105 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 106 | ); 107 | 108 | 109 | expect(1); 110 | var table = $('#test-table').tableToJSON(); 111 | var expected = [{'Nickname':'Jill', 'Last Name':'Smith', 'Points':'50'}, 112 | {'Nickname':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 113 | {'Nickname':'John', 'Last Name':'Doe', 'Points':'80'}]; 114 | deepEqual(table, expected); 115 | }); 116 | 117 | /* Override Cell Values */ 118 | test('override column names', function() { 119 | $('#qunit-fixture').html( 120 | '' + 121 | '' + 122 | '' + 123 | '' + 124 | '' + 125 | '' + 126 | '' + 127 | '' + 128 | '' + 129 | '' + 130 | '' + 131 | '' + 132 | '' + 133 | '' + 134 | '' + 135 | '' + 136 | '' + 137 | '' + 138 | '' + 139 | '' + 140 | '' + 141 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 142 | ); 143 | 144 | 145 | expect(1); 146 | var table = $('#test-table').tableToJSON(); 147 | var expected = [{'Nickname':'Jill', 'Last Name':'Smith', 'Points':'disqualified'}, 148 | {'Nickname':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 149 | {'Nickname':'John', 'Last Name':'Doe', 'Points':'80'}]; 150 | deepEqual(table, expected); 151 | }); 152 | 153 | /* Ignore Column */ 154 | test('ignore columns', function() { 155 | $('#qunit-fixture').html( 156 | '' + 157 | '' + 158 | '' + 159 | '' + 160 | '' + 161 | '' + 162 | '' + 163 | '' + 164 | '' + 165 | '' + 166 | '' + 167 | '' + 168 | '' + 169 | '' + 170 | '' + 171 | '' + 172 | '' + 173 | '' + 174 | '' + 175 | '' + 176 | '' + 177 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 178 | ); 179 | 180 | 181 | expect(1); 182 | var table = $('#test-table').tableToJSON({ 183 | ignoreColumns: [0] 184 | }); 185 | var expected = [ 186 | {'Last Name':'Smith', 'Points':'50'}, 187 | {'Last Name':'Jackson', 'Points':'94'}, 188 | {'Last Name':'Doe', 'Points':'80'} 189 | ]; 190 | deepEqual(table, expected); 191 | }); 192 | 193 | /* Ignore Hidden Row */ 194 | test('ignore hidden rows', function() { 195 | $('#qunit-fixture').html( 196 | '' + 197 | '' + 198 | '' + 199 | '' + 200 | '' + 201 | '' + 202 | '' + 203 | '' + 204 | '' + 205 | '' + 206 | '' + 207 | '' + 208 | '' + 209 | '' + 210 | '' + 211 | '' + 212 | '' + 213 | '' + 214 | '' + 215 | '' + 216 | '' + 217 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 218 | ); 219 | 220 | 221 | expect(1); 222 | var table = $('#test-table').tableToJSON(); 223 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 224 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}]; 225 | deepEqual(table, expected); 226 | }); 227 | 228 | /* Include Hidden Rows */ 229 | test('Include Hidden Rows', function() { 230 | $('#qunit-fixture').html( 231 | '' + 232 | '' + 233 | '' + 234 | '' + 235 | '' + 236 | '' + 237 | '' + 238 | '' + 239 | '' + 240 | '' + 241 | '' + 242 | '' + 243 | '' + 244 | '' + 245 | '' + 246 | '' + 247 | '' + 248 | '' + 249 | '' + 250 | '' + 251 | '' + 252 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 253 | ); 254 | 255 | 256 | expect(1); 257 | var table = $('#test-table').tableToJSON({ 258 | ignoreHiddenRows: false 259 | }); 260 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 261 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 262 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 263 | deepEqual(table, expected); 264 | }); 265 | 266 | /* Ignore Empty Row */ 267 | test('ignore empty rows', function() { 268 | $('#qunit-fixture').html( 269 | '' + 270 | '' + 271 | '' + 272 | '' + 273 | '' + 274 | '' + 275 | '' + 276 | '' + 277 | '' + 278 | '' + 279 | '' + 280 | '' + 281 | '' + 282 | '' + 283 | '' + 284 | '' + 285 | '' + 286 | '' + 287 | '' + 288 | '' + 289 | '' + 290 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    ' 291 | ); 292 | 293 | 294 | expect(1); 295 | var table = $('#test-table').tableToJSON({ignoreEmptyRows:true}); 296 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 297 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}]; 298 | deepEqual(table, expected); 299 | }); 300 | 301 | /* Include Empty Row */ 302 | test('ignore empty rows', function() { 303 | $('#qunit-fixture').html( 304 | '' + 305 | '' + 306 | '' + 307 | '' + 308 | '' + 309 | '' + 310 | '' + 311 | '' + 312 | '' + 313 | '' + 314 | '' + 315 | '' + 316 | '' + 317 | '' + 318 | '' + 319 | '' + 320 | '' + 321 | '' + 322 | '' + 323 | '' + 324 | '' + 325 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    ' 326 | ); 327 | 328 | 329 | expect(1); 330 | var table = $('#test-table').tableToJSON(); 331 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 332 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 333 | {'First Name':'', 'Last Name':'', 'Points':''}]; 334 | deepEqual(table, expected); 335 | }); 336 | 337 | /* Ignore Row Option */ 338 | test('ignore empty rows', function() { 339 | $('#qunit-fixture').html( 340 | '' + 341 | '' + 342 | '' + 343 | '' + 344 | '' + 345 | '' + 346 | '' + 347 | '' + 348 | '' + 349 | '' + 350 | '' + 351 | '' + 352 | '' + 353 | '' + 354 | '' + 355 | '' + 356 | '' + 357 | '' + 358 | '' + 359 | '' + 360 | '' + 361 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 362 | ); 363 | 364 | 365 | expect(1); 366 | var table = $('#test-table').tableToJSON({ignoreEmptyRows:true}); 367 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 368 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 369 | deepEqual(table, expected); 370 | }); 371 | 372 | /* Read first row of td's as column when there are no th's*/ 373 | test('without any ', function() { 374 | $('#qunit-fixture').html( 375 | '' + 376 | '' + 377 | '' + 378 | '' + 379 | '' + 380 | '' + 381 | '' + 382 | '' + 383 | '' + 384 | '' + 385 | '' + 386 | '' + 387 | '' + 388 | '' + 389 | '' + 390 | '' + 391 | '' + 392 | '' + 393 | '' + 394 | '' + 395 | '' + 396 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 397 | ); 398 | 399 | 400 | expect(1); 401 | var table = $('#test-table').tableToJSON(); 402 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 403 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 404 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 405 | deepEqual(table, expected); 406 | }); 407 | 408 | /* Uses the onlyColumns option to only include the first column*/ 409 | test('Ony include 1 column', function() { 410 | $('#qunit-fixture').html( 411 | '' + 412 | '' + 413 | '' + 414 | '' + 415 | '' + 416 | '' + 417 | '' + 418 | '' + 419 | '' + 420 | '' + 421 | '' + 422 | '' + 423 | '' + 424 | '' + 425 | '' + 426 | '' + 427 | '' + 428 | '' + 429 | '' + 430 | '' + 431 | '' + 432 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 433 | ); 434 | 435 | 436 | expect(1); 437 | var table = $('#test-table').tableToJSON({ 438 | onlyColumns: [0] 439 | }); 440 | var expected = [ 441 | {'First Name':'Jill'}, 442 | {'First Name':'Eve'}, 443 | {'First Name':'John'} 444 | ]; 445 | deepEqual(table, expected); 446 | }); 447 | 448 | /* onlyColumns option overrides ignoreColumns option */ 449 | test('onlyColumns option overrides ignoreColumns option', function() { 450 | $('#qunit-fixture').html( 451 | '' + 452 | '' + 453 | '' + 454 | '' + 455 | '' + 456 | '' + 457 | '' + 458 | '' + 459 | '' + 460 | '' + 461 | '' + 462 | '' + 463 | '' + 464 | '' + 465 | '' + 466 | '' + 467 | '' + 468 | '' + 469 | '' + 470 | '' + 471 | '' + 472 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 473 | ); 474 | 475 | 476 | expect(1); 477 | var table = $('#test-table').tableToJSON({ 478 | onlyColumns: [0, 1], 479 | ignoreColumns: [0, 1] 480 | }); 481 | var expected = [ 482 | {'First Name':'Jill', 'Last Name':'Smith'}, 483 | {'First Name':'Eve', 'Last Name':'Jackson'}, 484 | {'First Name':'John', 'Last Name':'Doe'} 485 | ]; 486 | deepEqual(table, expected); 487 | }); 488 | 489 | /* headings option uses all rows as data */ 490 | test('headings option uses all rows as data', function() { 491 | $('#qunit-fixture').html( 492 | '' + 493 | '' + 494 | '' + 495 | '' + 496 | '' + 497 | '' + 498 | '' + 499 | '' + 500 | '' + 501 | '' + 502 | '' + 503 | '' + 504 | '' + 505 | '' + 506 | '' + 507 | '' + 508 | '
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 509 | ); 510 | 511 | 512 | expect(1); 513 | var table = $('#test-table').tableToJSON({ 514 | headings: ['First Name', 'Last Name', 'Points'] 515 | }); 516 | var expected = [ 517 | {'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 518 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 519 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 520 | ]; 521 | deepEqual(table, expected); 522 | }); 523 | 524 | /* headings option works with ignoreColumns option */ 525 | test('headings option works with ignoreColumns option', function() { 526 | $('#qunit-fixture').html( 527 | '' + 528 | '' + 529 | '' + 530 | '' + 531 | '' + 532 | '' + 533 | '' + 534 | '' + 535 | '' + 536 | '' + 537 | '' + 538 | '' + 539 | '' + 540 | '' + 541 | '' + 542 | '' + 543 | '
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 544 | ); 545 | 546 | 547 | expect(1); 548 | var table = $('#test-table').tableToJSON({ 549 | ignoreColumns: [0], 550 | headings: ['Last Name', 'Points'] 551 | }); 552 | var expected = [ 553 | {'Last Name':'Smith', 'Points':'50'}, 554 | {'Last Name':'Jackson', 'Points':'94'}, 555 | {'Last Name':'Doe', 'Points':'80'} 556 | ]; 557 | deepEqual(table, expected); 558 | }); 559 | 560 | /* headings option works with onlyColumns option */ 561 | test('headings option works with onlyColumns option', function() { 562 | $('#qunit-fixture').html( 563 | '' + 564 | '' + 565 | '' + 566 | '' + 567 | '' + 568 | '' + 569 | '' + 570 | '' + 571 | '' + 572 | '' + 573 | '' + 574 | '' + 575 | '' + 576 | '' + 577 | '' + 578 | '' + 579 | '
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 580 | ); 581 | 582 | 583 | expect(1); 584 | var table = $('#test-table').tableToJSON({ 585 | onlyColumns: [1, 2], 586 | headings: ['Last Name', 'Points'] 587 | }); 588 | var expected = [ 589 | {'Last Name':'Smith', 'Points':'50'}, 590 | {'Last Name':'Jackson', 'Points':'94'}, 591 | {'Last Name':'Doe', 'Points':'80'} 592 | ]; 593 | deepEqual(table, expected); 594 | }); 595 | 596 | /* allowHTML option allows HTML tags within a table to remain in the object */ 597 | test('allowHTML option allows HTML tags within a table to remain in the object', function () { 598 | $('#qunit-fixture').html( 599 | '' + 600 | '' + 601 | '' + 602 | '' + 603 | '' + 604 | '' + 605 | '' + 606 | '' + 607 | '' + 608 | '' + 609 | '' + 610 | '' + 611 | '' + 612 | '' + 613 | '' + 614 | '' + 615 | '' + 616 | '' + 617 | '' + 618 | '' + 619 | '' + 620 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 621 | ); 622 | 623 | expect(1); 624 | var table = $('#test-table').tableToJSON({ 625 | allowHTML: true 626 | }); 627 | var expected = [ 628 | {'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 629 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 630 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 631 | ]; 632 | deepEqual(table, expected); 633 | }); 634 | 635 | /** 636 | * WARNING! The test results here are different then in 0.13.1! 637 | * In this version, an empty rowid is `null` - but in the old release its an empty string. 638 | * I think this is the preferred way - but its still different behavior! 639 | */ 640 | test('includeRowId option boolean type places the attribute ID from the row as a property of the row', function () { 641 | $('#qunit-fixture').html( 642 | '' + 643 | '' + 644 | '' + 645 | '' + 646 | '' + 647 | '' + 648 | '' + 649 | '' + 650 | '' + 651 | '' + 652 | '' + 653 | '' + 654 | '' + 655 | '' + 656 | '' + 657 | '' + 658 | '' + 659 | '' + 660 | '' + 661 | '' + 662 | '' + 663 | '' + 664 | '' + 665 | '' + 666 | '' + 667 | '' + 668 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    NoRowID
    ' 669 | ); 670 | 671 | expect(1); 672 | var table = $('#test-table').tableToJSON({ 673 | includeRowId: true 674 | }); 675 | var expected = [ 676 | {'rowId':'1', 'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 677 | {'rowId':'2', 'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 678 | {'rowId':'3', 'First Name':'John', 'Last Name':'Doe', 'Points':'80'}, 679 | {'rowId': null, 'First Name':'No', 'Last Name':'Row', 'Points':'ID'} // in the legacy this `null` is empty string 680 | ]; 681 | deepEqual(table, expected); 682 | }); 683 | 684 | /** 685 | * WARNING! The test results here are different then in 0.13.1! 686 | * In this version, an empty rowid is `null` - but in the old release its an empty string. 687 | * I think this is the preferred way - but its still different behavior! 688 | */ 689 | /* includeRowId option custom, instead of a boolean use a string, and string value will be the property name. */ 690 | test('includeRowId option string type, instead of a boolean use a string, and string value will be the property name', function () { 691 | $('#qunit-fixture').html( 692 | '' + 693 | '' + 694 | '' + 695 | '' + 696 | '' + 697 | '' + 698 | '' + 699 | '' + 700 | '' + 701 | '' + 702 | '' + 703 | '' + 704 | '' + 705 | '' + 706 | '' + 707 | '' + 708 | '' + 709 | '' + 710 | '' + 711 | '' + 712 | '' + 713 | '' + 714 | '' + 715 | '' + 716 | '' + 717 | '' + 718 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    NoRowID
    ' 719 | ); 720 | 721 | expect(1); 722 | var table = $('#test-table').tableToJSON({ 723 | includeRowId: 'customIDname' 724 | }); 725 | var expected = [ 726 | {'customIDname':'1', 'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 727 | {'customIDname':'2', 'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 728 | {'customIDname':'3', 'First Name':'John', 'Last Name':'Doe', 'Points':'80'}, 729 | {'customIDname':null, 'First Name':'No', 'Last Name':'Row', 'Points':'ID'} // in the legacy this `null` is empty string 730 | ]; 731 | deepEqual(table, expected); 732 | }); 733 | 734 | test('Basic Usage extractor option', function() { 735 | $('#qunit-fixture').html( 736 | '' + 737 | '' + 738 | '' + 739 | '' + 740 | '' + 741 | '' + 742 | '' + 743 | '' + 744 | '' + 745 | '' + 746 | '' + 747 | '' + 748 | '' + 749 | '' + 750 | '' + 751 | '' + 752 | '' + 753 | '' + 754 | '' + 755 | '' + 756 | '' + 757 | '
    TitleStreet
    Doctor Jill Smith123 Main Street
    President Eve Jackson999 National Blvd.
    Mr. John Doe555 Letter Ave.
    ' 758 | ); 759 | 760 | 761 | expect(1); 762 | var table = $('#test-table').tableToJSON({ 763 | extractor : function(cellIndex, $cell) { 764 | return $cell.find('span').text(); 765 | } 766 | }); 767 | var expected = [{'Title':'Doctor', 'Street':'Main'}, 768 | {'Title':'President', 'Street':'National'}, 769 | {'Title':'Mr.', 'Street':'Letter'}]; 770 | deepEqual(table, expected); 771 | }); 772 | 773 | test('extractor option per column', function() { 774 | $('#qunit-fixture').html( 775 | '' + 776 | '' + 777 | '' + 778 | '' + 779 | '' + 780 | '' + 781 | '' + 782 | '' + 783 | '' + 784 | '' + 785 | '' + 786 | '' + 787 | '' + 788 | '' + 789 | '' + 790 | '' + 791 | '' + 792 | '' + 793 | '' + 794 | '' + 795 | '' + 796 | '' + 797 | '' + 798 | '' + 799 | '' + 800 | '
    TitleNumberDate
    Doctor Jill Smith123 Main Street1/31/2015
    President Eve Jackson999 National Blvd.2/1/2014
    Mr. John Doe555 Letter Ave.12/2/2014
    ' 801 | ); 802 | 803 | 804 | expect(1); 805 | var table = $('#test-table').tableToJSON({ 806 | extractor : { 807 | 0 : function(cellIndex, $cell) { 808 | return $cell.find('span').text(); 809 | }, 810 | 1 : function(cellIndex, $cell) { 811 | return $cell.find('em').text(); 812 | }, 813 | 2 : function(cellIndex, $cell) { 814 | return new Date( $cell.text() ).toString(); 815 | } 816 | } 817 | }); 818 | var dates = [ '1/31/2015', '2/1/2014', '12/2/2014' ]; 819 | $.each(dates, function(i,v){ 820 | dates[i] = new Date(v).toString(); 821 | }); 822 | var expected = [{'Title':'Doctor', 'Number':'345', 'Date': dates[0]}, 823 | {'Title':'President', 'Number':'999', 'Date': dates[1]}, 824 | {'Title':'Mr.', 'Number':'555', 'Date': dates[2] }]; 825 | deepEqual(table, expected); 826 | }); 827 | -------------------------------------------------------------------------------- /test/specs/content.test.js: -------------------------------------------------------------------------------- 1 | /*global module, test, expect, deepEqual */ 2 | 3 | module('content'); 4 | 5 | /* Ignore nested tables in cells with just td's*/ 6 | test('ignore nested in cells', function() { 7 | $('#qunit-fixture').html( 8 | '' + 9 | '' + 10 | '' + 11 | '' + 12 | '' + 13 | '' + 14 | '' + 15 | '' + 16 | '' + 17 | '' + 18 | '' + 19 | '' + 20 | '' + 21 | '' + 22 | '' + 23 | '' + 24 | '' + 25 | '' + 26 | '' + 27 | '' + 28 | '' + 29 | '
    First NameLast NamePoints
    JillSmith50
    Eve
    1
    2
    Jackson94
    JohnDoe80
    ' 30 | ); 31 | 32 | 33 | expect(1); 34 | var table = $('#test-table').tableToJSON(); 35 | var expected = [ 36 | {'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 37 | {'First Name':'Eve 12', 'Last Name':'Jackson', 'Points':'94'}, 38 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 39 | ]; 40 | deepEqual(table, expected); 41 | }); 42 | 43 | /* Ignore nested tables in cells with th's and td's*/ 44 | test('ignore nested and in cells', function() { 45 | $('#qunit-fixture').html( 46 | '' + 47 | '' + 48 | '' + 49 | '' + 50 | '' + 51 | '' + 52 | '' + 53 | '' + 54 | '' + 55 | '' + 56 | '' + 57 | '' + 58 | '' + 59 | '' + 60 | '' + 61 | '' + 62 | '' + 63 | '' + 64 | '' + 65 | '' + 66 | '' + 67 | '
    First NameLast NamePoints
    JillSmith50
    Eve
    number
    1
    2
    Jackson94
    JohnDoe80
    ' 68 | ); 69 | 70 | 71 | expect(1); 72 | var table = $('#test-table').tableToJSON(); 73 | var expected = [ 74 | {'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 75 | {'First Name':'Eve number12', 'Last Name':'Jackson', 'Points':'94'}, 76 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 77 | ]; 78 | deepEqual(table, expected); 79 | }); 80 | 81 | /* Ignore nested tables in headings with just td's*/ 82 | test('ignore nested in headings', function() { 83 | $('#qunit-fixture').html( 84 | '' + 85 | '' + 86 | '' + 87 | '' + 88 | '' + 89 | '' + 90 | '' + 91 | '' + 92 | '' + 93 | '' + 94 | '' + 95 | '' + 96 | '' + 97 | '' + 98 | '' + 99 | '' + 100 | '' + 101 | '' + 102 | '' + 103 | '' + 104 | '' + 105 | '
    First NameLast Name
    1
    2
    Points
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 106 | ); 107 | 108 | 109 | expect(1); 110 | var table = $('#test-table').tableToJSON(); 111 | var expected = [ 112 | {'First Name':'Jill', 'Last Name 12':'Smith', 'Points':'50'}, 113 | {'First Name':'Eve', 'Last Name 12':'Jackson', 'Points':'94'}, 114 | {'First Name':'John', 'Last Name 12':'Doe', 'Points':'80'} 115 | ]; 116 | deepEqual(table, expected); 117 | }); 118 | 119 | /* Ignore nested tables in headings with th's and td's*/ 120 | test('ignore nested and in headings', function() { 121 | $('#qunit-fixture').html( 122 | '' + 123 | '' + 124 | '' + 125 | '' + 126 | '' + 127 | '' + 128 | '' + 129 | '' + 130 | '' + 131 | '' + 132 | '' + 133 | '' + 134 | '' + 135 | '' + 136 | '' + 137 | '' + 138 | '' + 139 | '' + 140 | '' + 141 | '' + 142 | '' + 143 | '
    First NameLast Name
    number
    1
    2
    Points
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 144 | ); 145 | 146 | 147 | expect(1); 148 | var table = $('#test-table').tableToJSON(); 149 | var expected = [ 150 | {'First Name':'Jill', 'Last Name number12':'Smith', 'Points':'50'}, 151 | {'First Name':'Eve', 'Last Name number12':'Jackson', 'Points':'94'}, 152 | {'First Name':'John', 'Last Name number12':'Doe', 'Points':'80'} 153 | ]; 154 | deepEqual(table, expected); 155 | }); 156 | 157 | /* Links are retrieved as values */ 158 | test('links are just values', function() { 159 | $('#qunit-fixture').html( 160 | '' + 161 | '' + 162 | '' + 163 | '' + 164 | '' + 165 | '' + 166 | '' + 167 | '' + 168 | '' + 169 | '' + 170 | '' + 171 | '' + 172 | '' + 173 | '' + 174 | '' + 175 | '' + 176 | '' + 177 | '' + 178 | '' + 179 | '' + 180 | '' + 181 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 182 | ); 183 | 184 | 185 | expect(1); 186 | var table = $('#test-table').tableToJSON(); 187 | var expected = [ 188 | {'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 189 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 190 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 191 | ]; 192 | deepEqual(table, expected); 193 | }); 194 | 195 | /* A complex table */ 196 | test('complex table', function() { 197 | $('#qunit-fixture').html( 198 | '
    Status Location Zone Tier Install Calc Install Rate
    INCOMPLETE location 1 QTY B 9.5
    INCOMPLETE Empty Empty Empty Empty Empty
    ' 199 | ); 200 | 201 | 202 | expect(1); 203 | var table = $('#test-table').tableToJSON(); 204 | var expected = [{'id':'oaWd4cs2JHY660pG', 'State':'INCOMPLETE', 'BCS':'location', 'ZONE':'1', 'TIER':'QTY', 'INSTALL_CALC':'B', 'INSTALL_R1':'9.5'}]; 205 | deepEqual(table, expected); 206 | }); 207 | 208 | /* A table with colspan */ 209 | test('A table with colspan', function() { 210 | $('#qunit-fixture').html( 211 | '' + 212 | '' + 213 | '' + 214 | '' + 215 | '' + 216 | '' + 217 | '' + 218 | '' + 219 | '' + 220 | '' + 221 | '' + 222 | '' + 223 | '' + 224 | '' + 225 | '' + 226 | '' + 227 | '' + 228 | '' + 229 | '
    First NameLast NamePoints
    Jill
    Eve94
    JohnDoe80
    ' 230 | ); 231 | 232 | 233 | expect(1); 234 | var table = $('#test-table').tableToJSON(); 235 | var expected = [ 236 | {'First Name':'Jill', 'Last Name':'Jill', 'Points':'Jill'}, 237 | {'First Name':'Eve', 'Last Name':'Eve', 'Points':'94'}, 238 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 239 | ]; 240 | deepEqual(table, expected); 241 | }); 242 | 243 | /* ignoreColumns with colspan */ 244 | test('ignoreColumns with colspan', function() { 245 | $('#qunit-fixture').html( 246 | '' + 247 | '' + 248 | '' + 249 | '' + 250 | '' + 251 | '' + 252 | '' + 253 | '' + 254 | '' + 255 | '' + 256 | '' + 257 | '' + 258 | '' + 259 | '' + 260 | '' + 261 | '' + 262 | '' + 263 | '' + 264 | '
    First NameLast NamePoints
    Jill
    Eve94
    JohnDoe80
    ' 265 | ); 266 | 267 | 268 | expect(1); 269 | var table = $('#test-table').tableToJSON({ ignoreColumns : [1] }); 270 | var expected = [ 271 | {'First Name':'Jill', 'Points':'Jill'}, 272 | {'First Name':'Eve', 'Points':'94'}, 273 | {'First Name':'John', 'Points':'80'} 274 | ]; 275 | deepEqual(table, expected); 276 | }); 277 | 278 | /* A table with rowspan */ 279 | test('A table with rowspan', function() { 280 | $('#qunit-fixture').html( 281 | '' + 282 | '' + 283 | '' + 284 | '' + 285 | '' + 286 | '' + 287 | '' + 288 | '' + 289 | '' + 290 | '' + 291 | '' + 292 | '' + 293 | '' + 294 | '' + 295 | '' + 296 | '' + 297 | '' + 298 | '' + 299 | '
    First NameLast NamePoints
    JillSmith50
    Jackson94
    80
    ' 300 | ); 301 | 302 | 303 | expect(1); 304 | var table = $('#test-table').tableToJSON(); 305 | var expected = [ 306 | {'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 307 | {'First Name':'Jill', 'Last Name':'Jackson', 'Points':'94'}, 308 | {'First Name':'Jill', 'Last Name':'Jackson', 'Points':'80'} 309 | ]; 310 | deepEqual(table, expected); 311 | }); 312 | 313 | /* ignoreColumns with rowspan */ 314 | test('ignoreColumns with rowspan', function() { 315 | $('#qunit-fixture').html( 316 | '' + 317 | '' + 318 | '' + 319 | '' + 320 | '' + 321 | '' + 322 | '' + 323 | '' + 324 | '' + 325 | '' + 326 | '' + 327 | '' + 328 | '' + 329 | '' + 330 | '' + 331 | '' + 332 | '' + 333 | '' + 334 | '
    First NameLast NamePoints
    JillSmith50
    Jackson94
    80
    ' 335 | ); 336 | 337 | 338 | expect(1); 339 | var table = $('#test-table').tableToJSON({ ignoreColumns : [1] }); 340 | var expected = [ 341 | {'First Name':'Jill', 'Points':'50'}, 342 | {'First Name':'Jill', 'Points':'94'}, 343 | {'First Name':'Jill', 'Points':'80'} 344 | ]; 345 | deepEqual(table, expected); 346 | }); 347 | 348 | /* ignoreColumns with rowspan */ 349 | test('ignoreRows with rowspan', function() { 350 | $('#qunit-fixture').html( 351 | '' + 352 | '' + 353 | '' + 354 | '' + 355 | '' + 356 | '' + 357 | '' + 358 | '' + 359 | '' + 360 | '' + 361 | '' + 362 | '' + 363 | '' + 364 | '' + 365 | '' + 366 | '' + 367 | '' + 368 | '' + 369 | '
    First NameLast NamePoints
    JillSmith50
    Jackson94
    80
    ' 370 | ); 371 | 372 | 373 | expect(1); 374 | var table = $('#test-table').tableToJSON({ ignoreRows : [2] }); 375 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 376 | {'First Name':'Jill', 'Last Name':'Jackson', 'Points':'80'}]; 377 | deepEqual(table, expected); 378 | }); 379 | 380 | /* A table with rowspan & colspan */ 381 | test('rowspan & colspan in tbody', function() { 382 | $('#qunit-fixture').html( 383 | '' + 384 | '' + 385 | '' + 386 | '' + 387 | '' + 388 | '' + 389 | '' + 390 | '' + 391 | '
    linevalue1value2value3value4
    11.11.21.31.4
    1.51.61.7
    22.12.22.32.4
    2.52.62.7
    33.13.2
    3.43.5
    ' 392 | ); 393 | 394 | expect(1); 395 | var table = $('#test-table').tableToJSON(); 396 | var expected = [ 397 | {'line':'1','value1':'1.1','value2':'1.2','value3':'1.3','value4':'1.4'}, 398 | {'line':'1','value1':'1.5','value2':'1.6','value3':'1.7','value4':'1.4'}, 399 | {'line':'2','value1':'2.1','value2':'2.2','value3':'2.3','value4':'2.4'}, 400 | {'line':'2','value1':'2.5','value2':'2.5','value3':'2.6','value4':'2.7'}, 401 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.2','value4':'3.2'}, 402 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.4','value4':'3.5'} 403 | ]; 404 | deepEqual(table, expected); 405 | }); 406 | 407 | /* A table with rowspan & colspan & ignoreColumns */ 408 | test('ignoreColumns with rowspan & colspan in tbody', function() { 409 | $('#qunit-fixture').html( 410 | '' + 411 | '' + 412 | '' + 413 | '' + 414 | '' + 415 | '' + 416 | '' + 417 | '' + 418 | '
    linevalue1value2value3value4
    11.11.21.31.4
    1.51.61.7
    22.12.22.32.4
    2.52.62.7
    33.13.2
    3.43.5
    ' 419 | ); 420 | 421 | expect(5); 422 | var $table = $('#test-table'); 423 | var table = $table.tableToJSON({ ignoreColumns : [0] }); 424 | var expected = [ 425 | {'value1':'1.1','value2':'1.2','value3':'1.3','value4':'1.4'}, 426 | {'value1':'1.5','value2':'1.6','value3':'1.7','value4':'1.4'}, 427 | {'value1':'2.1','value2':'2.2','value3':'2.3','value4':'2.4'}, 428 | {'value1':'2.5','value2':'2.5','value3':'2.6','value4':'2.7'}, 429 | {'value1':'3.1','value2':'3.1','value3':'3.2','value4':'3.2'}, 430 | {'value1':'3.1','value2':'3.1','value3':'3.4','value4':'3.5'} 431 | ]; 432 | deepEqual(table, expected, 'Ignore Column "line"'); 433 | 434 | table = $table.tableToJSON({ ignoreColumns : [1] }); 435 | expected = [ 436 | {'line':'1','value2':'1.2','value3':'1.3','value4':'1.4'}, 437 | {'line':'1','value2':'1.6','value3':'1.7','value4':'1.4'}, 438 | {'line':'2','value2':'2.2','value3':'2.3','value4':'2.4'}, 439 | {'line':'2','value2':'2.5','value3':'2.6','value4':'2.7'}, 440 | {'line':'3','value2':'3.1','value3':'3.2','value4':'3.2'}, 441 | {'line':'3','value2':'3.1','value3':'3.4','value4':'3.5'} 442 | ]; 443 | deepEqual(table, expected, 'Ignore Column "value1"'); 444 | 445 | table = $table.tableToJSON({ ignoreColumns : [2] }); 446 | expected = [ 447 | {'line':'1','value1':'1.1','value3':'1.3','value4':'1.4'}, 448 | {'line':'1','value1':'1.5','value3':'1.7','value4':'1.4'}, 449 | {'line':'2','value1':'2.1','value3':'2.3','value4':'2.4'}, 450 | {'line':'2','value1':'2.5','value3':'2.6','value4':'2.7'}, 451 | {'line':'3','value1':'3.1','value3':'3.2','value4':'3.2'}, 452 | {'line':'3','value1':'3.1','value3':'3.4','value4':'3.5'} 453 | ]; 454 | deepEqual(table, expected, 'Ignore Column "value2"'); 455 | 456 | table = $table.tableToJSON({ ignoreColumns : [3] }); 457 | expected = [ 458 | {'line':'1','value1':'1.1','value2':'1.2','value4':'1.4'}, 459 | {'line':'1','value1':'1.5','value2':'1.6','value4':'1.4'}, 460 | {'line':'2','value1':'2.1','value2':'2.2','value4':'2.4'}, 461 | {'line':'2','value1':'2.5','value2':'2.5','value4':'2.7'}, 462 | {'line':'3','value1':'3.1','value2':'3.1','value4':'3.2'}, 463 | {'line':'3','value1':'3.1','value2':'3.1','value4':'3.5'} 464 | ]; 465 | deepEqual(table, expected, 'Ignore Column "value3"'); 466 | 467 | table = $table.tableToJSON({ ignoreColumns : [4] }); 468 | expected = [ 469 | {'line':'1','value1':'1.1','value2':'1.2','value3':'1.3'}, 470 | {'line':'1','value1':'1.5','value2':'1.6','value3':'1.7'}, 471 | {'line':'2','value1':'2.1','value2':'2.2','value3':'2.3'}, 472 | {'line':'2','value1':'2.5','value2':'2.5','value3':'2.6'}, 473 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.2'}, 474 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.4'} 475 | ]; 476 | deepEqual(table, expected, 'Ignore Column "value4"'); 477 | 478 | }); 479 | 480 | /* A table with rowspan & colspan & ignoreRows */ 481 | test('ignoreRows with rowspan & colspan in tbody', function() { 482 | $('#qunit-fixture').html( 483 | '' + 484 | '' + 485 | '' + 486 | '' + 487 | '' + 488 | '' + 489 | '' + 490 | '' + 491 | '
    linevalue1value2value3value4
    11.11.21.31.4
    1.51.61.7
    22.12.22.32.4
    2.52.62.7
    33.13.2
    3.43.5
    ' 492 | ); 493 | 494 | expect(6); 495 | var $table = $('#test-table'); 496 | var table = $table.tableToJSON({ ignoreRows : [1] }); 497 | var expected = [ 498 | {'line':'1','value1':'1.5','value2':'1.6','value3':'1.7','value4':'1.4'}, 499 | {'line':'2','value1':'2.1','value2':'2.2','value3':'2.3','value4':'2.4'}, 500 | {'line':'2','value1':'2.5','value2':'2.5','value3':'2.6','value4':'2.7'}, 501 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.2','value4':'3.2'}, 502 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.4','value4':'3.5'} 503 | ]; 504 | deepEqual(table, expected, 'Ignore row 1'); 505 | 506 | table = $table.tableToJSON({ ignoreRows : [2] }); 507 | expected = [ 508 | {'line':'1','value1':'1.1','value2':'1.2','value3':'1.3','value4':'1.4'}, 509 | {'line':'2','value1':'2.1','value2':'2.2','value3':'2.3','value4':'2.4'}, 510 | {'line':'2','value1':'2.5','value2':'2.5','value3':'2.6','value4':'2.7'}, 511 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.2','value4':'3.2'}, 512 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.4','value4':'3.5'} 513 | ]; 514 | deepEqual(table, expected, 'Ignore row 2'); 515 | 516 | table = $table.tableToJSON({ ignoreRows : [3] }); 517 | expected = [ 518 | {'line':'1','value1':'1.1','value2':'1.2','value3':'1.3','value4':'1.4'}, 519 | {'line':'1','value1':'1.5','value2':'1.6','value3':'1.7','value4':'1.4'}, 520 | {'line':'2','value1':'2.5','value2':'2.5','value3':'2.6','value4':'2.7'}, 521 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.2','value4':'3.2'}, 522 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.4','value4':'3.5'} 523 | ]; 524 | deepEqual(table, expected, 'Ignore row 3'); 525 | 526 | table = $table.tableToJSON({ ignoreRows : [4] }); 527 | expected = [ 528 | {'line':'1','value1':'1.1','value2':'1.2','value3':'1.3','value4':'1.4'}, 529 | {'line':'1','value1':'1.5','value2':'1.6','value3':'1.7','value4':'1.4'}, 530 | {'line':'2','value1':'2.1','value2':'2.2','value3':'2.3','value4':'2.4'}, 531 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.2','value4':'3.2'}, 532 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.4','value4':'3.5'} 533 | ]; 534 | deepEqual(table, expected, 'Ignore row 4'); 535 | 536 | table = $table.tableToJSON({ ignoreRows : [5] }); 537 | expected = [ 538 | {'line':'1','value1':'1.1','value2':'1.2','value3':'1.3','value4':'1.4'}, 539 | {'line':'1','value1':'1.5','value2':'1.6','value3':'1.7','value4':'1.4'}, 540 | {'line':'2','value1':'2.1','value2':'2.2','value3':'2.3','value4':'2.4'}, 541 | {'line':'2','value1':'2.5','value2':'2.5','value3':'2.6','value4':'2.7'}, 542 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.4','value4':'3.5'} 543 | ]; 544 | deepEqual(table, expected, 'Ignore row 5'); 545 | 546 | table = $table.tableToJSON({ ignoreRows : [6] }); 547 | expected = [ 548 | {'line':'1','value1':'1.1','value2':'1.2','value3':'1.3','value4':'1.4'}, 549 | {'line':'1','value1':'1.5','value2':'1.6','value3':'1.7','value4':'1.4'}, 550 | {'line':'2','value1':'2.1','value2':'2.2','value3':'2.3','value4':'2.4'}, 551 | {'line':'2','value1':'2.5','value2':'2.5','value3':'2.6','value4':'2.7'}, 552 | {'line':'3','value1':'3.1','value2':'3.1','value3':'3.2','value4':'3.2'} 553 | ]; 554 | deepEqual(table, expected, 'Ignore row 6'); 555 | }); 556 | 557 | test('textDataOverride overrides', function() { 558 | $('#qunit-fixture').html( 559 | '' + 560 | '' + 561 | '' + 562 | '' + 563 | '' + 564 | '' + 565 | '' + 566 | '' + 567 | '' + 568 | '' + 569 | '' + 570 | '' + 571 | '' + 572 | '' + 573 | '' + 574 | '' + 575 | '' + 576 | '' + 577 | '' + 578 | '' + 579 | '' + 580 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 581 | ); 582 | 583 | expect(2); 584 | 585 | var table = $('#test-table').tableToJSON(); 586 | var expected = [ 587 | {'First Name':'Bobby', 'Last Name':'Bill', 'Points':'50'}, 588 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 589 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 590 | ]; 591 | deepEqual(table, expected, 'Default override'); 592 | 593 | table = $('#test-table').tableToJSON({ textDataOverride: 'data-custom'}); 594 | expected = [ 595 | {'First Name':'Jack', 'Last Name':'Frost', 'Points':'50'}, 596 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 597 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 598 | ]; 599 | deepEqual(table, expected, 'Custom override'); 600 | }); 601 | -------------------------------------------------------------------------------- /test/specs/core.test.js: -------------------------------------------------------------------------------- 1 | /*global module, test, expect, deepEqual */ 2 | 3 | module('core'); 4 | 5 | /* Basic Usage */ 6 | test('basic usage', function() { 7 | $('#qunit-fixture').html( 8 | '' + 9 | '' + 10 | '' + 11 | '' + 12 | '' + 13 | '' + 14 | '' + 15 | '' + 16 | '' + 17 | '' + 18 | '' + 19 | '' + 20 | '' + 21 | '' + 22 | '' + 23 | '' + 24 | '' + 25 | '' + 26 | '' + 27 | '' + 28 | '' + 29 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 30 | ); 31 | 32 | 33 | expect(1); 34 | var table = $('#test-table').tableToJSON(); 35 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 36 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 37 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 38 | deepEqual(table, expected); 39 | }); 40 | 41 | /* Basic Usage with thead and tbody*/ 42 | test('basic usage with thead and tbody', function() { 43 | $('#qunit-fixture').html( 44 | '' + 45 | '' + 46 | '' + 47 | '' + 48 | '' + 49 | '' + 50 | '' + 51 | '' + 52 | '' + 53 | '' + 54 | '' + 55 | '' + 56 | '' + 57 | '' + 58 | '' + 59 | '' + 60 | '' + 61 | '' + 62 | '' + 63 | '' + 64 | '' + 65 | '' + 66 | '' + 67 | '' + 68 | '' + 69 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 70 | ); 71 | 72 | 73 | expect(1); 74 | var table = $('#test-table').tableToJSON(); 75 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 76 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 77 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 78 | deepEqual(table, expected); 79 | }); 80 | 81 | /* Override Column Names */ 82 | test('override column names', function() { 83 | $('#qunit-fixture').html( 84 | '' + 85 | '' + 86 | '' + 87 | '' + 88 | '' + 89 | '' + 90 | '' + 91 | '' + 92 | '' + 93 | '' + 94 | '' + 95 | '' + 96 | '' + 97 | '' + 98 | '' + 99 | '' + 100 | '' + 101 | '' + 102 | '' + 103 | '' + 104 | '' + 105 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 106 | ); 107 | 108 | 109 | expect(1); 110 | var table = $('#test-table').tableToJSON(); 111 | var expected = [{'Nickname':'Jill', 'Last Name':'Smith', 'Points':'50'}, 112 | {'Nickname':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 113 | {'Nickname':'John', 'Last Name':'Doe', 'Points':'80'}]; 114 | deepEqual(table, expected); 115 | }); 116 | 117 | /* Override Cell Values */ 118 | test('override column names', function() { 119 | $('#qunit-fixture').html( 120 | '' + 121 | '' + 122 | '' + 123 | '' + 124 | '' + 125 | '' + 126 | '' + 127 | '' + 128 | '' + 129 | '' + 130 | '' + 131 | '' + 132 | '' + 133 | '' + 134 | '' + 135 | '' + 136 | '' + 137 | '' + 138 | '' + 139 | '' + 140 | '' + 141 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 142 | ); 143 | 144 | 145 | expect(1); 146 | var table = $('#test-table').tableToJSON(); 147 | var expected = [{'Nickname':'Jill', 'Last Name':'Smith', 'Points':'disqualified'}, 148 | {'Nickname':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 149 | {'Nickname':'John', 'Last Name':'Doe', 'Points':'80'}]; 150 | deepEqual(table, expected); 151 | }); 152 | 153 | /* Ignore Column */ 154 | test('ignore columns', function() { 155 | $('#qunit-fixture').html( 156 | '' + 157 | '' + 158 | '' + 159 | '' + 160 | '' + 161 | '' + 162 | '' + 163 | '' + 164 | '' + 165 | '' + 166 | '' + 167 | '' + 168 | '' + 169 | '' + 170 | '' + 171 | '' + 172 | '' + 173 | '' + 174 | '' + 175 | '' + 176 | '' + 177 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 178 | ); 179 | 180 | 181 | expect(1); 182 | var table = $('#test-table').tableToJSON({ 183 | ignoreColumns: [0] 184 | }); 185 | var expected = [ 186 | {'Last Name':'Smith', 'Points':'50'}, 187 | {'Last Name':'Jackson', 'Points':'94'}, 188 | {'Last Name':'Doe', 'Points':'80'} 189 | ]; 190 | deepEqual(table, expected); 191 | }); 192 | 193 | /* Ignore Hidden Row */ 194 | test('ignore hidden rows', function() { 195 | $('#qunit-fixture').html( 196 | '' + 197 | '' + 198 | '' + 199 | '' + 200 | '' + 201 | '' + 202 | '' + 203 | '' + 204 | '' + 205 | '' + 206 | '' + 207 | '' + 208 | '' + 209 | '' + 210 | '' + 211 | '' + 212 | '' + 213 | '' + 214 | '' + 215 | '' + 216 | '' + 217 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 218 | ); 219 | 220 | 221 | expect(1); 222 | var table = $('#test-table').tableToJSON(); 223 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 224 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}]; 225 | deepEqual(table, expected); 226 | }); 227 | 228 | /* Include Hidden Rows */ 229 | test('Include Hidden Rows', function() { 230 | $('#qunit-fixture').html( 231 | '' + 232 | '' + 233 | '' + 234 | '' + 235 | '' + 236 | '' + 237 | '' + 238 | '' + 239 | '' + 240 | '' + 241 | '' + 242 | '' + 243 | '' + 244 | '' + 245 | '' + 246 | '' + 247 | '' + 248 | '' + 249 | '' + 250 | '' + 251 | '' + 252 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 253 | ); 254 | 255 | 256 | expect(1); 257 | var table = $('#test-table').tableToJSON({ 258 | ignoreHiddenRows: false 259 | }); 260 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 261 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 262 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 263 | deepEqual(table, expected); 264 | }); 265 | 266 | /* Ignore Empty Row */ 267 | test('ignore empty rows', function() { 268 | $('#qunit-fixture').html( 269 | '' + 270 | '' + 271 | '' + 272 | '' + 273 | '' + 274 | '' + 275 | '' + 276 | '' + 277 | '' + 278 | '' + 279 | '' + 280 | '' + 281 | '' + 282 | '' + 283 | '' + 284 | '' + 285 | '' + 286 | '' + 287 | '' + 288 | '' + 289 | '' + 290 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    ' 291 | ); 292 | 293 | 294 | expect(1); 295 | var table = $('#test-table').tableToJSON({ignoreEmptyRows:true}); 296 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 297 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}]; 298 | deepEqual(table, expected); 299 | }); 300 | 301 | /* Ignore empty rows defaults to false */ 302 | test('ignore empty rows defaults to false', function() { 303 | $('#qunit-fixture').html( 304 | '' + 305 | '' + 306 | '' + 307 | '' + 308 | '' + 309 | '' + 310 | '' + 311 | '' + 312 | '' + 313 | '' + 314 | '' + 315 | '' + 316 | '' + 317 | '' + 318 | '' + 319 | '' + 320 | '' + 321 | '' + 322 | '' + 323 | '' + 324 | '' + 325 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    ' 326 | ); 327 | 328 | 329 | expect(1); 330 | var table = $('#test-table').tableToJSON(); 331 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 332 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 333 | {'First Name':'', 'Last Name':'', 'Points':''}]; 334 | deepEqual(table, expected); 335 | }); 336 | 337 | /* Ignore empty rows via data attribute */ 338 | test('ignore empty rows via data attribute', function() { 339 | $('#qunit-fixture').html( 340 | '' + 341 | '' + 342 | '' + 343 | '' + 344 | '' + 345 | '' + 346 | '' + 347 | '' + 348 | '' + 349 | '' + 350 | '' + 351 | '' + 352 | '' + 353 | '' + 354 | '' + 355 | '' + 356 | '' + 357 | '' + 358 | '' + 359 | '' + 360 | '' + 361 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 362 | ); 363 | 364 | 365 | expect(1); 366 | var table = $('#test-table').tableToJSON({ignoreEmptyRows:true}); 367 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 368 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 369 | deepEqual(table, expected); 370 | }); 371 | 372 | /* Read first row of td's as column when there are no th's*/ 373 | test('without any ', function() { 374 | $('#qunit-fixture').html( 375 | '' + 376 | '' + 377 | '' + 378 | '' + 379 | '' + 380 | '' + 381 | '' + 382 | '' + 383 | '' + 384 | '' + 385 | '' + 386 | '' + 387 | '' + 388 | '' + 389 | '' + 390 | '' + 391 | '' + 392 | '' + 393 | '' + 394 | '' + 395 | '' + 396 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 397 | ); 398 | 399 | 400 | expect(1); 401 | var table = $('#test-table').tableToJSON(); 402 | var expected = [{'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 403 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 404 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'}]; 405 | deepEqual(table, expected); 406 | }); 407 | 408 | /* Uses the onlyColumns option to only include the first column*/ 409 | test('Ony include 1 column', function() { 410 | $('#qunit-fixture').html( 411 | '' + 412 | '' + 413 | '' + 414 | '' + 415 | '' + 416 | '' + 417 | '' + 418 | '' + 419 | '' + 420 | '' + 421 | '' + 422 | '' + 423 | '' + 424 | '' + 425 | '' + 426 | '' + 427 | '' + 428 | '' + 429 | '' + 430 | '' + 431 | '' + 432 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 433 | ); 434 | 435 | 436 | expect(1); 437 | var table = $('#test-table').tableToJSON({ 438 | onlyColumns: [0] 439 | }); 440 | var expected = [ 441 | {'First Name':'Jill'}, 442 | {'First Name':'Eve'}, 443 | {'First Name':'John'} 444 | ]; 445 | deepEqual(table, expected); 446 | }); 447 | 448 | /* onlyColumns option overrides ignoreColumns option */ 449 | test('onlyColumns option overrides ignoreColumns option', function() { 450 | $('#qunit-fixture').html( 451 | '' + 452 | '' + 453 | '' + 454 | '' + 455 | '' + 456 | '' + 457 | '' + 458 | '' + 459 | '' + 460 | '' + 461 | '' + 462 | '' + 463 | '' + 464 | '' + 465 | '' + 466 | '' + 467 | '' + 468 | '' + 469 | '' + 470 | '' + 471 | '' + 472 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 473 | ); 474 | 475 | 476 | expect(1); 477 | var table = $('#test-table').tableToJSON({ 478 | onlyColumns: [0, 1], 479 | ignoreColumns: [0, 1] 480 | }); 481 | var expected = [ 482 | {'First Name':'Jill', 'Last Name':'Smith'}, 483 | {'First Name':'Eve', 'Last Name':'Jackson'}, 484 | {'First Name':'John', 'Last Name':'Doe'} 485 | ]; 486 | deepEqual(table, expected); 487 | }); 488 | 489 | /* headings option uses all rows as data */ 490 | test('headings option uses all rows as data', function() { 491 | $('#qunit-fixture').html( 492 | '' + 493 | '' + 494 | '' + 495 | '' + 496 | '' + 497 | '' + 498 | '' + 499 | '' + 500 | '' + 501 | '' + 502 | '' + 503 | '' + 504 | '' + 505 | '' + 506 | '' + 507 | '' + 508 | '
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 509 | ); 510 | 511 | 512 | expect(1); 513 | var table = $('#test-table').tableToJSON({ 514 | headings: ['First Name', 'Last Name', 'Points'] 515 | }); 516 | var expected = [ 517 | {'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 518 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 519 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 520 | ]; 521 | deepEqual(table, expected); 522 | }); 523 | 524 | /* headings option works with ignoreColumns option */ 525 | test('headings option works with ignoreColumns option', function() { 526 | $('#qunit-fixture').html( 527 | '' + 528 | '' + 529 | '' + 530 | '' + 531 | '' + 532 | '' + 533 | '' + 534 | '' + 535 | '' + 536 | '' + 537 | '' + 538 | '' + 539 | '' + 540 | '' + 541 | '' + 542 | '' + 543 | '
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 544 | ); 545 | 546 | 547 | expect(1); 548 | var table = $('#test-table').tableToJSON({ 549 | ignoreColumns: [0], 550 | headings: ['Last Name', 'Points'] 551 | }); 552 | var expected = [ 553 | {'Last Name':'Smith', 'Points':'50'}, 554 | {'Last Name':'Jackson', 'Points':'94'}, 555 | {'Last Name':'Doe', 'Points':'80'} 556 | ]; 557 | deepEqual(table, expected); 558 | }); 559 | 560 | /* headings option works with onlyColumns option */ 561 | test('headings option works with onlyColumns option', function() { 562 | $('#qunit-fixture').html( 563 | '' + 564 | '' + 565 | '' + 566 | '' + 567 | '' + 568 | '' + 569 | '' + 570 | '' + 571 | '' + 572 | '' + 573 | '' + 574 | '' + 575 | '' + 576 | '' + 577 | '' + 578 | '' + 579 | '
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 580 | ); 581 | 582 | 583 | expect(1); 584 | var table = $('#test-table').tableToJSON({ 585 | onlyColumns: [1, 2], 586 | headings: ['Last Name', 'Points'] 587 | }); 588 | var expected = [ 589 | {'Last Name':'Smith', 'Points':'50'}, 590 | {'Last Name':'Jackson', 'Points':'94'}, 591 | {'Last Name':'Doe', 'Points':'80'} 592 | ]; 593 | deepEqual(table, expected); 594 | }); 595 | 596 | /* allowHTML option allows HTML tags within a table to remain in the object */ 597 | test('allowHTML option allows HTML tags within a table to remain in the object', function () { 598 | $('#qunit-fixture').html( 599 | '' + 600 | '' + 601 | '' + 602 | '' + 603 | '' + 604 | '' + 605 | '' + 606 | '' + 607 | '' + 608 | '' + 609 | '' + 610 | '' + 611 | '' + 612 | '' + 613 | '' + 614 | '' + 615 | '' + 616 | '' + 617 | '' + 618 | '' + 619 | '' + 620 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    ' 621 | ); 622 | 623 | expect(1); 624 | var table = $('#test-table').tableToJSON({ 625 | allowHTML: true 626 | }); 627 | var expected = [ 628 | {'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 629 | {'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 630 | {'First Name':'John', 'Last Name':'Doe', 'Points':'80'} 631 | ]; 632 | deepEqual(table, expected); 633 | }); 634 | 635 | /* includeRowId option boolean type places the attribute ID from the row as a property of the row */ 636 | test('includeRowId option boolean type places the attribute ID from the row as a property of the row', function () { 637 | $('#qunit-fixture').html( 638 | '' + 639 | '' + 640 | '' + 641 | '' + 642 | '' + 643 | '' + 644 | '' + 645 | '' + 646 | '' + 647 | '' + 648 | '' + 649 | '' + 650 | '' + 651 | '' + 652 | '' + 653 | '' + 654 | '' + 655 | '' + 656 | '' + 657 | '' + 658 | '' + 659 | '' + 660 | '' + 661 | '' + 662 | '' + 663 | '' + 664 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    NoRowID
    ' 665 | ); 666 | 667 | expect(1); 668 | var table = $('#test-table').tableToJSON({ 669 | includeRowId: true 670 | }); 671 | var expected = [ 672 | {'rowId':'1', 'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 673 | {'rowId':'2', 'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 674 | {'rowId':'3', 'First Name':'John', 'Last Name':'Doe', 'Points':'80'}, 675 | {'rowId':null, 'First Name':'No', 'Last Name':'Row', 'Points':'ID'} 676 | ]; 677 | deepEqual(table, expected); 678 | }); 679 | 680 | /* includeRowId option custom, instead of a boolean use a string, and string value will be the property name. */ 681 | test('includeRowId option string type, instead of a boolean use a string, and string value will be the property name', function () { 682 | $('#qunit-fixture').html( 683 | '' + 684 | '' + 685 | '' + 686 | '' + 687 | '' + 688 | '' + 689 | '' + 690 | '' + 691 | '' + 692 | '' + 693 | '' + 694 | '' + 695 | '' + 696 | '' + 697 | '' + 698 | '' + 699 | '' + 700 | '' + 701 | '' + 702 | '' + 703 | '' + 704 | '' + 705 | '' + 706 | '' + 707 | '' + 708 | '' + 709 | '
    First NameLast NamePoints
    JillSmith50
    EveJackson94
    JohnDoe80
    NoRowID
    ' 710 | ); 711 | 712 | expect(1); 713 | var table = $('#test-table').tableToJSON({ 714 | includeRowId: 'customIDname' 715 | }); 716 | var expected = [ 717 | {'customIDname':'1', 'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'}, 718 | {'customIDname':'2', 'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'}, 719 | {'customIDname':'3', 'First Name':'John', 'Last Name':'Doe', 'Points':'80'}, 720 | {'customIDname':null, 'First Name':'No', 'Last Name':'Row', 'Points':'ID'} 721 | ]; 722 | deepEqual(table, expected); 723 | }); 724 | --------------------------------------------------------------------------------