Skip to content

Commit

Permalink
According to rstudio#418, improve dataTables.buttons.min.js, mimickin…
Browse files Browse the repository at this point in the history
…g with minor adjustments changes from https://github.com/chrisvwn/DT/blob/master/inst/htmlwidgets/lib/datatables-extensions/Buttons/js/dataTables.buttons.min.js, which is inpired by  https://datatables.net/forums/discussion/comment/106434/#Comment_106434

This prepares the dataTables.buttons.min.js to handle multiple row headers. Without further adjustments this change actually create problems for all output functions, which expect a single row header instead of an array.
  • Loading branch information
mtyszler committed Jan 4, 2020
1 parent a8ba0e2 commit ed15f2f
Showing 1 changed file with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,64 @@
g(a);
return c
};

/* ----- BEGIN added Code ----- */
getHeaders = function( dt ){
var thRows = dt.nTHead.rows;
var numRows = thRows.length;
var matrix = [];

// Iterate over each row of the header and add information to matrix.
for ( var rowIdx = 0; rowIdx < numRows; rowIdx++ ) {
var $row = $(thRows[rowIdx]);

// Iterate over actual columns specified in this row.
var $ths = $row.children("th");
for ( var colIdx = 0; colIdx < $ths.length; colIdx++ )
{
var $th = $($ths.get(colIdx));
var colspan = $th.attr("colspan") || 1;
var rowspan = $th.attr("rowspan") || 1;
var colCount = 0;

// ----- add this cell's title to the matrix
if (matrix[rowIdx] === undefined) {
matrix[rowIdx] = []; // create array for this row
}
// find 1st empty cell
for ( var j = 0; j < (matrix[rowIdx]).length; j++, colCount++ ) {
if ( matrix[rowIdx][j] === "PLACEHOLDER" ) {
break;
}
}
var myColCount = colCount;
matrix[rowIdx][colCount++] = $th.text();

// ----- If title cell has colspan, add empty titles for extra cell width.
for ( var j = 1; j < colspan; j++ ) {
matrix[rowIdx][colCount++] = "";
}

// ----- If title cell has rowspan, add empty titles for extra cell height.
for ( var i = 1; i < rowspan; i++ ) {
var thisRow = rowIdx+i;
if ( matrix[thisRow] === undefined ) {
matrix[thisRow] = [];
}
// First add placeholder text for any previous columns.
for ( var j = (matrix[thisRow]).length; j < myColCount; j++ ) {
matrix[thisRow][j] = "PLACEHOLDER";
}
for ( var j = 0; j < colspan; j++ ) { // and empty for my columns
matrix[thisRow][myColCount+j] = "";
}
}
}
}
return matrix;
};
/*END added code*/

l.buttonSelector = function(a, b) {
for (var c = [], e = function(a, b, c) {
for (var d, g, f = 0, h = b.length; f < h; f++)
Expand Down Expand Up @@ -698,10 +756,14 @@
c.decodeEntities && (v.innerHTML = a, a = v.value);
return a
},
a = b.columns(c.columns).indexes().map(function(a) {
/*BEGIN ADD*/
/*a = b.columns(c.columns).indexes().map(function(a) {
var d = b.column(a).header();
return c.format.header(d.innerHTML, a, d)
}).toArray(),
*/
headerMatrix = getHeaders( b.settings()[0] ),
/*END ADD*/
g = b.table().footer() ? b.columns(c.columns).indexes().map(function(a) {
var d =
b.column(a).footer();
Expand All @@ -713,12 +775,12 @@
}, h)).any() && d.extend(h, {
selected: !0
});
for (var h = b.rows(c.rows, h).indexes().toArray(), f = b.cells(h, c.columns), h = f.render(c.orthogonal).toArray(), f = f.nodes().toArray(), k = a.length, j = [], l = 0, n = 0, o = 0 < k ? h.length / k : 0; n < o; n++) {
for (var h = b.rows(c.rows, h).indexes().toArray(), f = b.cells(h, c.columns), h = f.render(c.orthogonal).toArray(), f = f.nodes().toArray(), k = /*a.length*/headerMatrix[headerMatrix.length - 1].length, j = [], l = 0, n = 0, o = 0 < k ? h.length / k : 0; n < o; n++) {
for (var r = [k], s = 0; s < k; s++) r[s] = c.format.body(h[l], n, s, f[l]), l++;
j[n] = r
}
a = {
header: a,
header: /*a*/headerMatrix,
footer: g,
body: j
};
Expand Down

0 comments on commit ed15f2f

Please sign in to comment.