1 var HtmlTableExport =
function(table){
4 this.cloneTableStyle =
true;
8 HtmlTableExport.prototype.genereateTable =
function(config, style, visible, colVisProp){
9 this.cloneTableStyle = style;
10 this.config = config || {};
11 this.colVisProp = colVisProp;
13 var headers = this.generateHeaderElements();
14 var body = this.generateBodyElements(visible);
16 var table = document.createElement(
"table");
17 table.classList.add(
"tabulator-print-table");
18 table.appendChild(headers);
19 table.appendChild(body);
21 this.mapElementStyles(this.table.element, table, [
"border-top",
"border-left",
"border-right",
"border-bottom"]);
27 HtmlTableExport.prototype.generateColumnGroupHeaders =
function(){
30 var columns = this.config.columnGroups !==
false ? this.table.columnManager.columns : this.table.columnManager.columnsByIndex;
32 columns.forEach((column) => {
33 var colData = this.processColumnGroup(column);
43 HtmlTableExport.prototype.processColumnGroup =
function(column){
44 var subGroups = column.columns,
48 title:column.definition.title,
54 groupData.subGroups = [];
57 subGroups.forEach((subGroup) => {
58 var subGroupData = this.processColumnGroup(subGroup);
61 groupData.width += subGroupData.width;
62 groupData.subGroups.push(subGroupData);
64 if(subGroupData.depth > maxDepth){
65 maxDepth = subGroupData.depth;
70 groupData.depth += maxDepth;
76 if(this.columnVisCheck(column)){
87 HtmlTableExport.prototype.groupHeadersToRows =
function(columns){
89 var headers = [], headerDepth = 0;
91 function parseColumnGroup(column, level){
93 var depth = headerDepth - level;
95 if(typeof headers[level] ===
"undefined"){
99 column.height = column.subGroups ? 1 : (depth - column.depth) + 1;
101 headers[level].push(column);
103 if(column.subGroups){
104 column.subGroups.forEach(
function(subGroup){
105 parseColumnGroup(subGroup, level+1);
111 columns.forEach(
function(column){
112 if(column.depth > headerDepth){
113 headerDepth = column.depth;
117 columns.forEach(
function(column){
118 parseColumnGroup(column,0);
125 HtmlTableExport.prototype.generateHeaderElements =
function(){
127 var headerEl = document.createElement(
"thead");
129 var rows = this.groupHeadersToRows(this.generateColumnGroupHeaders());
131 rows.forEach((row) => {
132 var rowEl = document.createElement(
"tr");
134 this.mapElementStyles(this.table.columnManager.getHeadersElement(), headerEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"background-color",
"color",
"font-weight",
"font-family",
"font-size"]);
136 row.forEach((column) => {
137 var cellEl = document.createElement(
"th");
138 var classNames = column.column.definition.cssClass ? column.column.definition.cssClass.split(
" ") : [];
140 cellEl.colSpan = column.width;
141 cellEl.rowSpan = column.height;
143 cellEl.innerHTML = column.column.definition.title;
145 if(this.cloneTableStyle){
146 cellEl.style.boxSizing =
"border-box";
149 classNames.forEach(
function(className) {
150 cellEl.classList.add(className);
153 this.mapElementStyles(column.column.getElement(), cellEl, [
"text-align",
"border-top",
"border-left",
"border-right",
"border-bottom",
"background-color",
"color",
"font-weight",
"font-family",
"font-size"]);
154 this.mapElementStyles(column.column.contentElement, cellEl, [
"padding-top",
"padding-left",
"padding-right",
"padding-bottom"]);
156 if(column.column.visible){
157 this.mapElementStyles(column.column.getElement(), cellEl, [
"width"]);
159 if(column.column.definition.width){
160 cellEl.style.width = column.column.definition.width +
"px";
164 if(column.column.parent){
165 this.mapElementStyles(column.column.parent.groupElement, cellEl, [
"border-top"]);
168 rowEl.appendChild(cellEl);
171 headerEl.appendChild(rowEl);
177 HtmlTableExport.prototype.generateBodyElements =
function(visible){
178 var oddRow, evenRow, calcRow, firstRow, firstCell, firstGroup, lastCell, styleCells, styleRow;
181 if(this.cloneTableStyle && window.getComputedStyle){
182 oddRow = this.table.element.querySelector(
".tabulator-row-odd:not(.tabulator-group):not(.tabulator-calcs)");
183 evenRow = this.table.element.querySelector(
".tabulator-row-even:not(.tabulator-group):not(.tabulator-calcs)");
184 calcRow = this.table.element.querySelector(
".tabulator-row.tabulator-calcs");
185 firstRow = this.table.element.querySelector(
".tabulator-row:not(.tabulator-group):not(.tabulator-calcs)");
186 firstGroup = this.table.element.getElementsByClassName(
"tabulator-group")[0];
189 styleCells = firstRow.getElementsByClassName(
"tabulator-cell");
190 firstCell = styleCells[0];
191 lastCell = styleCells[styleCells.length - 1];
195 var bodyEl = document.createElement(
"tbody");
197 var rows = visible ? this.table.rowManager.getVisibleRows(
true) : this.table.rowManager.getDisplayRows();
200 if(this.config.columnCalcs !==
false &&
this.table.modExists(
"columnCalcs")){
201 if(this.table.modules.columnCalcs.topInitialized){
202 rows.unshift(this.table.modules.columnCalcs.topRow);
205 if(this.table.modules.columnCalcs.botInitialized){
206 rows.push(this.table.modules.columnCalcs.botRow);
210 this.table.columnManager.columnsByIndex.forEach((column) => {
211 if (this.columnVisCheck(column)) {
212 columns.push(column);
216 rows = rows.filter((row) => {
219 return this.config.rowGroups !==
false;
223 return this.config.columnCalcs !==
false;
230 if(rows.length > 1000){
231 console.warn(
"It may take a long time to render an HTML table with more than 1000 rows");
234 rows.forEach((row, i) => {
235 var rowData = row.getData();
237 var rowEl = document.createElement(
"tr");
238 rowEl.classList.add(
"tabulator-print-table-row");
242 var cellEl = document.createElement(
"td");
243 cellEl.colSpan = columns.length;
244 cellEl.innerHTML = row.key;
246 rowEl.classList.add(
"tabulator-print-table-group");
248 this.mapElementStyles(firstGroup, rowEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"color",
"font-weight",
"font-family",
"font-size",
"background-color"]);
249 this.mapElementStyles(firstGroup, cellEl, [
"padding-top",
"padding-left",
"padding-right",
"padding-bottom"]);
250 rowEl.appendChild(cellEl);
254 rowEl.classList.add(
"tabulator-print-table-calcs");
257 columns.forEach((column) =>{
258 var cellEl = document.createElement(
"td");
260 var value = column.getFieldValue(rowData);
268 return column.definition.field;
270 getElement:
function(){
273 getColumn:
function(){
274 return column.getComponent();
280 return row.getComponent();
282 getComponent:
function(){
288 var classNames = column.definition.cssClass ? column.definition.cssClass.split(
" ") : [];
290 classNames.forEach(
function(className) {
291 cellEl.classList.add(className);
294 if(this.table.modExists(
"format")){
295 value = this.table.modules.format.formatValue(cellWrapper);
297 switch(typeof value){
299 value = JSON.stringify(value);
312 if(value instanceof Node){
313 cellEl.appendChild(value);
315 cellEl.innerHTML = value;
319 this.mapElementStyles(firstCell, cellEl, [
"padding-top",
"padding-left",
"padding-right",
"padding-bottom",
"border-top",
"border-left",
"border-right",
"border-bottom",
"color",
"font-weight",
"font-family",
"font-size",
"text-align"]);
322 rowEl.appendChild(cellEl);
326 styleRow = row.type ==
"calc" ? calcRow : (((i % 2) && evenRow) ? evenRow : oddRow);
328 this.mapElementStyles(styleRow, rowEl, [
"border-top",
"border-left",
"border-right",
"border-bottom",
"color",
"font-weight",
"font-family",
"font-size",
"background-color"]);
332 bodyEl.appendChild(rowEl);
338 HtmlTableExport.prototype.columnVisCheck =
function(column){
339 return column.definition[this.colVisProp] !==
false && (column.visible || (!column.visible && column.definition[this.colVisProp]));
343 HtmlTableExport.prototype.getHtml =
function(visible, style, config){
344 var holder = document.createElement(
"div");
346 holder.appendChild(this.genereateTable(config || this.table.options.htmlOutputConfig, style, visible,
"htmlOutput"));
348 return holder.innerHTML;
352 HtmlTableExport.prototype.mapElementStyles =
function(from, to, props){
353 if(this.cloneTableStyle && from && to){
356 "background-color" :
"backgroundColor",
357 "color" :
"fontColor",
359 "font-weight" :
"fontWeight",
360 "font-family" :
"fontFamily",
361 "font-size" :
"fontSize",
362 "text-align" :
"textAlign",
363 "border-top" :
"borderTop",
364 "border-left" :
"borderLeft",
365 "border-right" :
"borderRight",
366 "border-bottom" :
"borderBottom",
367 "padding-top" :
"paddingTop",
368 "padding-left" :
"paddingLeft",
369 "padding-right" :
"paddingRight",
370 "padding-bottom" :
"paddingBottom",
373 if(window.getComputedStyle){
374 var fromStyle = window.getComputedStyle(from);
376 props.forEach(
function(prop){
377 to.style[lookup[prop]] = fromStyle.getPropertyValue(prop);
384 Tabulator.prototype.registerModule(
"htmlTableExport", HtmlTableExport);