Rotate table?
Rotate table?
niko
Posts: 5Questions: 0Answers: 0
I'd like to alter the table so that rows become columns and vice versa. So headers are displayed up and down the left side of the table and sorting happens from left to right.
This discussion has been closed.
Replies
Allan
A lingering question will be what the meaning of a column header is, and what you'd sort/filter by. Perhaps there should be an init-time indicator that tells the code to re-write the data (rotate it) before any initial table is made.
I was going to write a highlight plug-in but others have already taken care of that. Maybe I'll take this project.
Next blog post will be about creating first class plug-ins for DataTables, which you might be interested in (a couple of days before I can post it I think). It won't present anything hugely complex, but it might be of interest as a basic grounding for it.
Regards,
Allan
tedkalaw, I look forward to your plug-in.
note: i've left the original table visible for comparisons.
more work needs to be done, like applying strip classes, etc.
[code]
pivot a data table
<!--
var oTable;
var draw = 0;
$(document).ready(function() {
oTable = $('#properties_table').dataTable({
sDom: 't',
bUseRendered: false,
bLengthChange: true,
aaSorting: [[ 1, "asc" ]],
asStripClasses: [ "pt_odd", "pt_even" ],
aoColumns: [
{ // property column
sClass: "pt_property"
},
{ // value column
sClass: "pt_value default_value",
},
{ bVisible: true }, // category column
{ bVisible: false } // description column
],
oLanguage: {
sSearch: "Filter:"
},
"fnDrawCallback": function () {
var table_id = $(this).attr('id');
var oTable = $(this).dataTable({ bRetrieve: true });
var oSettings = oTable.fnSettings();
// get any previous pivot tables, to destroy after creating a new one
var pivot_id = table_id + "_pivot";
var old_pivot = $('#'+pivot_id);
// create new pivot table from original table
var pivot = $(this).clone(true);
var $pivot = $(pivot);
$pivot.attr('id', pivot_id);
$pivot.find('tr').remove(); // remove all rows
$tbody = $($pivot.find('tbody')[0]);
// rebuild table row by row by reading a column header and all column data from the original table
draw += 1; // using this in cell TITLE attribute to be sure I got a refresh
for (i = 0; i < oSettings.aoColumns.length; i++) {
// start a new row
var $nTr = $('');
// get header from original table, add to new row
//var nTh = $(this).find('th').eq(i).clone(true);
//$nTr.append(nTh);
// get columns from original table, add to new row
$('tr th:nth-child('+i+'), tr td:nth-child('+i+')', this).each( function() { $nTr.append($(this).clone(true).attr('title', "draw: " + draw)); });
$tbody.append($nTr[0]);
}
if (old_pivot.length) old_pivot.remove();
$pivot.append($tbody[0]);
$('#'+table_id+'_wrapper').append($pivot[0]);
}
});
});
// -->
<!--
#properties_table {
border-collapse: collapse;
border-spacing: 0;
border: solid black 1px;
}
#properties_table th, #properties_table td {
_text-wrap:supress;
padding-left: 2px;
border: solid #bbb 1px;
}
#properties_table_pivot {
border-collapse: collapse;
border-spacing: 0;
border: solid black 1px;
}
#properties_table_pivot th, #properties_table_pivot td {
_text-wrap:supress;
padding-left: 2px;
border: solid #bbb 1px;
}
.left {
text-align: left;
}
-->
A
B
C
D
a1
b4
c7
d1
a2
b1
c6
d2
a3
b5
c5
d3
a4
b2
c4
d4
a5
b6
c3
d5
a6
b3
c2
d6
a7
b7
c1
d7
[/code]
I'm not sure how to wrap this up into a more modular plug-in form factor.
Also, I'm unsure if this meets anyone's needs. If you need it tweaked, let me know.
[code]
$(document).ready(function() {
$('#example').dataTable({
"fnDrawCallback": function () {
$(this).hide(); // you could move this somewhere else because original table only needs to hide once
var table_id = $(this).attr('id');
var oTable = $(this).dataTable({ bRetrieve: true });
var oSettings = oTable.fnSettings();
// get any previous pivot tables, to destroy after creating a new one
var pivot_id = table_id + "_pivot";
var old_pivot = $('#'+pivot_id);
// create new pivot table from original table
var pivot = $(this).clone(true);
var $pivot = $(pivot);
$pivot.attr('id', pivot_id);
$pivot.find('tr').remove(); // remove all rows
$tbody = $($pivot.find('tbody')[0]);
// rebuild table row by row by reading a column header and all column data from the original table
for (i = 1; i <= oSettings.aoColumns.length; i++) { // nodes are 1-indexed in :nth-child
// start a new row
var $nTr = $('');
$nTr.addClass(oSettings.asStripClasses[i % oSettings.asStripClasses.length]);
// get columns from original table, add to new row
$('tr th:nth-child('+i+'), tr td:nth-child('+i+')', this).each( function() {
$nTr.append($(this).clone(true));
});
$tbody.append($nTr[0]);
}
if (old_pivot.length) old_pivot.remove(); // remove any old versions of pivot table, from old draws
$pivot.append($tbody[0]);
$(this).after($pivot[0]); // add the newly created table
$pivot.show(); //show, because it inherited from hidden table.
}
});
});
[/code]
[code]
$('thead tr th:nth-child('+i+'), tbody tr td:nth-child('+i+')', this)
[/code]
I've just put up the blog post I adulated to earlier: http://datatables.net/blog/Creating_feature_plug-ins . Might be of interest for wrapping it up into a feature plug-in.
Regards,
Allan
for instance, I will implement a function fnPivot(boolean) that turns on and off the pivoting, and want to set a variable bPivot somewhere to keep this state (and maybe let the value be specified at init time?).
thanks.
Not all of my "extras" follow this pattern yet, but the new ones do and I'm slowly converting the old ones over.
If it is just one parameter then you could just attach it to the settings object as something like oSettings.__var = whatever;
Regards,
Allan
It was written in August 2011. You can download it at http://www.beg.utexas.edu/_work/dt/Transpose.zip . There's a demo page in the zip file.
It adds 3 functions to the DataTable object:
[quote]
fnTranspose(bTranspose) - transpose (or untranspose) the current table
fnTransposeState([bTranspose]) - get/set the internal bTranspose value without redrawing the table
fnTransposeVersion([sVersion]) - get the version of the Transpose plugin, or compare sVersion to the current version and return true or false if version is equal to or greater than sVersion
[/quote]
example usage (from the demo page):
[code]
/* Add feature 'Z' to sDom to enable Transpose */
/* Initialise the table */
var oTable; // keep a global handle for convenience
$(document).ready(function() {
// intialize table to use Transpose (letter 'Z').
oTable = $('#example').dataTable({sDom: 'Zlfrtip'});
sTransposeVersion = oTable.fnTransposeVersion(); // get version
bVersionCheck = oTable.fnTransposeVersion('1.0.5'); // compare version
} );
/* Add a button handler to alternate between transposed and un-transposed table */
function transpose_table() {
var bTranspose = oTable.fnTransposeState();
oTable.fnTranspose(!bTranspose); // alternate the rotation of the table and redraw
}
/* add handler to button */
[/code]
Thanks!
Allan
Allan
http://www.beg.utexas.edu/_work/dt/Transpose.zip
note: I haven't tested this on anything but DT 1.8x
function transpose_table() {
$('#properties_table').dataTable({ sDom: 'Zlfrtip' });
var oTable = $('#properties_table').dataTable({ bRetrieve: true });
var bTranspose = oTable.fnTransposeState();
oTable.fnTranspose(!bTranspose); // alternate the rotation of the table and redraw
}