Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,37 @@ if( Tablesaw.mustard ) {
$( thrs ).each( function(){
var coltally = 0;

$( this ).children().each( function(){
var span = parseInt( this.getAttribute( "colspan" ), 10 ),
sel = ":nth-child(" + ( coltally + 1 ) + ")";
var children = $( this ).children();
var columnlookup = [];
children.each( function(){
var span = parseInt( this.getAttribute( "colspan" ), 10 );

columnlookup[coltally] = this;
colstart = coltally + 1;

if( span ){
for( var k = 0; k < span - 1; k++ ){
coltally++;
sel += ", :nth-child(" + ( coltally + 1 ) + ")";
columnlookup[coltally] = this;
}
}

// Store "cells" data on header as a reference to all cells in the same column as this TH
this.cells = self.$table.find("tr").not( thrs[0] ).not( this ).children().filter( sel );
this.cells = [];
coltally++;
});
// Note that this assumes that children() returns its results in document order. jQuery doesn't
// promise that in the docs, but it's a pretty safe assumption.
self.$table.find("tr").not( thrs[0]).each( function() {
var cellcoltally = 0;
$(this).children().each(function () {
var span = parseInt( this.getAttribute( "colspan" ), 10 );
columnlookup[cellcoltally].cells.push(this);
if (span) {
cellcoltally += span;
} else {
cellcoltally++;
}
});
});
});

return colstart;
Expand Down
113 changes: 113 additions & 0 deletions test/tablesaw_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,117 @@
}
});

QUnit.module ('tablesaw colspans', {
beforeEach: function() {
var colspannedHTML = [
'<table %s>',
'<thead>',
'<tr>',
'<th data-tablesaw-priority="1" colspan="2">Header 1</th>',
'<th data-tablesaw-priority="2" colspan="2">Header 2</th>',
'<th data-tablesaw-priority="6">Header 3</th>',
'</tr>',
'</thead>',
'<tbody>',
'<tr>',
'<td>Body Row 1</td>',
'<td>1</td>',
'<td>Small 1</td>',
'<td>Small 2</td>',
'<td>Small 3</td>',
'</tr>',
'<tr><td>Body Row 2</td><td>2</td><td colspan="2">Fat cell</td><td>After fat</td></tr>',
'<tr><td>Body Row 10</td><td>10</td><td>A</td><td>A</td><td>A</td></tr>',
'<tr><td>body row 4</td><td>10</td><td>A</td><td>A</td><td>A</td></tr>',
'<tr><td>Body Row 1.2</td><td>1.2</td><td>A</td><td>A</td><td>A</td></tr>',
'</tbody>',
'</table>'].join('');
$fixture = $( '#qunit-fixture' );
$fixture.html( colspannedHTML.replace( /\%s/, 'data-tablesaw-mode="columntoggle" data-tablesaw-minimap data-tablesaw-no-labels' ) );
$table = $fixture.find( 'table' );
$( document ).trigger( 'enhance.tablesaw' );
}
});

QUnit.test('Hide multicolumn header and single column cells', function( assert ) {
var $firstCell = $table.find( 'tbody td' ).eq( 0 );
var $secondCell = $table.find( 'tbody td' ).eq( 1 );

assert.strictEqual( $firstCell.is( '.tablesaw-cell-hidden' ), false, 'First cell is visible before checkbox unchecked' );
assert.strictEqual( $secondCell.is( '.tablesaw-cell-hidden' ), false, 'Second cell is visible before checkbox unchecked' );

$table.prev().find( '.tablesaw-columntoggle-btn' ).trigger( 'click' )
.next().find( ':checkbox' ).first().trigger( 'click' );

// close dialog
$( '.tablesaw-columntoggle-popup .close' ).click();

assert.strictEqual( $firstCell.is( '.tablesaw-cell-hidden' ), true, 'First cell is hidden after checkbox unchecked' );
assert.strictEqual( $secondCell.is( '.tablesaw-cell-hidden' ), true, 'Second cell is hidden after checkbox unchecked' );
});

QUnit.test('Hide multicolumn header and multi column cells', function( assert ) {
var $firstRowCell1 = $table.find( 'tbody tr:eq(0) td' ).eq( 2 );
var $firstRowCell2 = $table.find( 'tbody tr:eq(0) td' ).eq( 3 );
var $firstRowCell3 = $table.find( 'tbody tr:eq(0) td' ).eq( 4 );
var $secondRowFatCell = $table.find( 'tbody tr:eq(1) td' ).eq( 2 );
var $secondRowLastCell = $table.find( 'tbody tr:eq(1) td' ).eq( 3 );

assert.strictEqual( $firstRowCell1.is( '.tablesaw-cell-hidden' ), false, 'Cell 0,2 cell is visible before checkbox unchecked' );
assert.strictEqual( $firstRowCell2.is( '.tablesaw-cell-hidden' ), false, 'Cell 0,3 is visible before checkbox unchecked' );
assert.strictEqual( $firstRowCell3.is( '.tablesaw-cell-hidden' ), false, 'Cell 0,4 is visible before checkbox unchecked' );
assert.strictEqual( $secondRowFatCell.is( '.tablesaw-cell-hidden' ), false, 'Cell 1,2-3 is visible before checkbox unchecked' );
assert.strictEqual( $secondRowLastCell.is( '.tablesaw-cell-hidden' ), false, 'Cell 1,4 is visible before checkbox unchecked' );

var middlecheck = $($table.prev().find( '.tablesaw-columntoggle-btn' ).trigger( 'click' )
.next().find( ':checkbox' )[1]);
if (!middlecheck[0].checked) {
middlecheck.trigger( 'click' );
assert.strictEqual ( middlecheck[0].checked, true, "Toggle button wasn't initially true and then should've been turned on.");
}
middlecheck.trigger( 'click' );
assert.strictEqual ( middlecheck[0].checked, false, "Toggle button wasn't unchecked.");

// close dialog
$( '.tablesaw-columntoggle-popup .close' ).click();

assert.strictEqual( $firstRowCell1.is( '.tablesaw-cell-hidden' ), true, 'Cell 0,2 cell is hidden after checkbox unchecked' );
assert.strictEqual( $firstRowCell2.is( '.tablesaw-cell-hidden' ), true, 'Cell 0,3 is hidden after checkbox unchecked' );
assert.strictEqual( $firstRowCell3.is( '.tablesaw-cell-hidden' ), false, 'Cell 0,4 is still visible after checkbox unchecked' );
assert.strictEqual( $secondRowFatCell.is( '.tablesaw-cell-hidden' ), true, 'Cell 1,2-3 is hidden after checkbox unchecked' );
assert.strictEqual( $secondRowLastCell.is( '.tablesaw-cell-hidden' ), false, 'Cell 1,4 is still visible after checkbox unchecked' );
});

QUnit.test('Hide single column header and not multi column cells', function( assert ) {
var $firstRowCell1 = $table.find( 'tbody tr:eq(0) td' ).eq( 2 );
var $firstRowCell2 = $table.find( 'tbody tr:eq(0) td' ).eq( 3 );
var $firstRowCell3 = $table.find( 'tbody tr:eq(0) td' ).eq( 4 );
var $secondRowFatCell = $table.find( 'tbody tr:eq(1) td' ).eq( 2 );
var $secondRowLastCell = $table.find( 'tbody tr:eq(1) td' ).eq( 3 );

assert.strictEqual( $firstRowCell1.is( '.tablesaw-cell-hidden' ), false, 'Cell 0,2 cell is visible before checkbox unchecked' );
assert.strictEqual( $firstRowCell2.is( '.tablesaw-cell-hidden' ), false, 'Cell 0,3 is visible before checkbox unchecked' );
assert.strictEqual( $firstRowCell3.is( '.tablesaw-cell-hidden' ), false, 'Cell 0,4 is visible before checkbox unchecked' );
assert.strictEqual( $secondRowFatCell.is( '.tablesaw-cell-hidden' ), false, 'Cell 1,2-3 is visible before checkbox unchecked' );
assert.strictEqual( $secondRowLastCell.is( '.tablesaw-cell-hidden' ), false, 'Cell 1,4 is visible before checkbox unchecked' );

var lastcheck = $table.prev().find( '.tablesaw-columntoggle-btn' ).trigger( 'click' )
.next().find( ':checkbox').last();
if (!lastcheck[0].checked) {
lastcheck.trigger( 'click' );
assert.strictEqual ( lastcheck[0].checked, true, "Toggle button wasn't initially true and then should've been turned on.");
}
lastcheck.trigger( 'click' );
assert.strictEqual ( lastcheck[0].checked, false, "Toggle button wasn't unchecked.");

// close dialog
$( '.tablesaw-columntoggle-popup .close' ).click();

assert.strictEqual( $firstRowCell1.is( '.tablesaw-cell-hidden' ), false, 'Cell 0,2 cell is visible after checkbox unchecked' );
assert.strictEqual( $firstRowCell2.is( '.tablesaw-cell-hidden' ), false, 'Cell 0,3 is visible after checkbox unchecked' );
assert.strictEqual( $firstRowCell3.is( '.tablesaw-cell-hidden' ), true, 'Cell 0,4 is hidden after checkbox unchecked' );
assert.strictEqual( $secondRowFatCell.is( '.tablesaw-cell-hidden' ), false, 'Cell 1,2-3 is visible after checkbox unchecked' );
assert.strictEqual( $secondRowLastCell.is( '.tablesaw-cell-hidden' ), true, 'Cell 1,4 is hidden after checkbox unchecked' );
});

}(jQuery));