Skip to content

Commit

Permalink
Merge branch 'station384-master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lightswitch05 committed Sep 24, 2014
2 parents 1163bd1 + 943a08f commit ac517e1
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 6 deletions.
28 changes: 24 additions & 4 deletions src/jquery.tabletojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
onlyColumns: null,
ignoreHiddenRows: true,
headings: null,
allowHTML: false
allowHTML: false,
includeRowId : false
};
opts = $.extend(defaults, opts);

var notNull = function(value) {
return value !== undefined && value !== null;
};
Expand Down Expand Up @@ -51,6 +52,14 @@

var rowValues = function(row) {
var result = [];
var includeRowId = opts.includeRowId;
var useRowId = (typeof includeRowId === 'boolean') ? includeRowId : (typeof includeRowId === 'string') ? true : false;
var rowIdName = (typeof includeRowId === 'string') === true ? includeRowId : 'rowId';
if (useRowId) {
if (typeof $(row).attr('id') === 'undefined') {
result.push(rowIdName);
}
}
$(row).children('td,th').each(function(cellIndex, cell) {
result.push( cellValues(cellIndex, cell) );
});
Expand All @@ -67,12 +76,23 @@
tmpArray = [], cellIndex = 0, result = [];
table.children('tbody,*').children('tr').each(function(rowIndex, row) {
if( rowIndex > 0 || notNull(opts.headings) ) {
var includeRowId = opts.includeRowId;
var useRowId = (typeof includeRowId === 'boolean') ? includeRowId : (typeof includeRowId === 'string') ? true : false;
$row = $(row);
if( $row.is(':visible') || !opts.ignoreHiddenRows ) {
cellIndex = 0;
if (!tmpArray[rowIndex]) {
tmpArray[rowIndex] = [];
}
cellIndex = 0;
if (useRowId) {
cellIndex = cellIndex + 1;
if (typeof $row.attr('id') !== 'undefined') {
tmpArray[rowIndex].push($row.attr('id'));
} else {
tmpArray[rowIndex].push('');
}
}

$row.children().each(function(){
$cell = $(this);

Expand Down Expand Up @@ -133,4 +153,4 @@
var headings = getHeadings(this);
return construct(this, headings);
};
})( jQuery );
})( jQuery );
94 changes: 92 additions & 2 deletions test/specs/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ test('allowHTML option allows HTML tags within a table to remain in the object',
$('#qunit-fixture').html(
'<table id="test-table">' +
'<tr>' +
'<th>First Name' +
'<th>First Name</th>' +
'<th>Last Name</th>' +
'<th>Points</th>' +
'</tr>' +
Expand Down Expand Up @@ -524,4 +524,94 @@ test('allowHTML option allows HTML tags within a table to remain in the object',
{'First Name':'<strong>John</strong>', 'Last Name':'<span class="lastName">Doe</span>', 'Points':'<em>80</em>'}
];
deepEqual(table, expected);
});
});

/* includeRowId option boolean type places the attribute ID from the row as a property of the row */
test('includeRowId option boolean type places the attribute ID from the row as a property of the row', function () {
$('#qunit-fixture').html(
'<table id="test-table">' +
'<tr>' +
'<th>First Name</th>' +
'<th>Last Name</th>' +
'<th>Points</th>' +
'</tr>' +
'<tr id="1">' +
'<td>Jill</td>' +
'<td>Smith</td>' +
'<td>50</td>' +
'</tr>' +
'<tr id="2">' +
'<td>Eve</td>' +
'<td>Jackson</td>' +
'<td>94</td>' +
'</tr>' +
'<tr id="3">' +
'<td>John</td>' +
'<td>Doe</td>' +
'<td>80</td>' +
'</tr>' +
'<tr>' +
'<td>No</td>' +
'<td>Row</td>' +
'<td>ID</td>' +
'</tr>' +
'</table>'
);

expect(1);
var table = $('#test-table').tableToJSON({
includeRowId: true
});
var expected = [
{'rowId':'1', 'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'},
{'rowId':'2', 'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'},
{'rowId':'3', 'First Name':'John', 'Last Name':'Doe', 'Points':'80'},
{'rowId':'', 'First Name':'No', 'Last Name':'Row', 'Points':'ID'}
];
deepEqual(table, expected);
});

/* includeRowId option custom, instead of a boolean use a string, and string value will be the property name. */
test('includeRowId option string type, instead of a boolean use a string, and string value will be the property name', function () {
$('#qunit-fixture').html(
'<table id="test-table">' +
'<tr>' +
'<th>First Name</th>' +
'<th>Last Name</th>' +
'<th>Points</th>' +
'</tr>' +
'<tr id="1">' +
'<td>Jill</td>' +
'<td>Smith</td>' +
'<td>50</td>' +
'</tr>' +
'<tr id="2">' +
'<td>Eve</td>' +
'<td>Jackson</td>' +
'<td>94</td>' +
'</tr>' +
'<tr id="3">' +
'<td>John</td>' +
'<td>Doe</td>' +
'<td>80</td>' +
'</tr>' +
'<tr>' +
'<td>No</td>' +
'<td>Row</td>' +
'<td>ID</td>' +
'</tr>' +
'</table>'
);

expect(1);
var table = $('#test-table').tableToJSON({
includeRowId: 'customIDname'
});
var expected = [
{'customIDname':'1', 'First Name':'Jill', 'Last Name':'Smith', 'Points':'50'},
{'customIDname':'2', 'First Name':'Eve', 'Last Name':'Jackson', 'Points':'94'},
{'customIDname':'3', 'First Name':'John', 'Last Name':'Doe', 'Points':'80'},
{'customIDname':'', 'First Name':'No', 'Last Name':'Row', 'Points':'ID'}
];
deepEqual(table, expected);
});

0 comments on commit ac517e1

Please sign in to comment.