1 var ResponsiveLayout =
function(table){
4 this.hiddenColumns = [];
7 this.collapseFormatter = [];
8 this.collapseStartOpen =
true;
9 this.collapseHandleColumn =
false;
13 ResponsiveLayout.prototype.initialize =
function(){
17 this.mode = this.table.options.responsiveLayout;
18 this.collapseFormatter = this.table.options.responsiveLayoutCollapseFormatter || this.formatCollapsedData;
19 this.collapseStartOpen = this.table.options.responsiveLayoutCollapseStartOpen;
20 this.hiddenColumns = [];
23 this.table.columnManager.columnsByIndex.forEach(
function(column, i){
24 if(column.modules.responsive){
25 if(column.modules.responsive.order && column.modules.responsive.visible){
26 column.modules.responsive.index = i;
29 if(!column.visible &&
self.mode ===
"collapse"){
30 self.hiddenColumns.push(column);
37 columns = columns.reverse();
38 columns = columns.sort(
function(a, b){
39 var diff = b.modules.responsive.order - a.modules.responsive.order;
40 return diff || (b.modules.responsive.index - a.modules.responsive.index);
43 this.columns = columns;
45 if(this.mode ===
"collapse"){
46 this.generateCollapsedContent();
50 for (let col of this.table.columnManager.columnsByIndex){
51 if(col.definition.formatter ==
"responsiveCollapse"){
52 this.collapseHandleColumn = col;
57 if(this.collapseHandleColumn){
58 if(this.hiddenColumns.length){
59 this.collapseHandleColumn.show();
61 this.collapseHandleColumn.hide();
67 ResponsiveLayout.prototype.initializeColumn =
function(column){
68 var def = column.getDefinition();
70 column.modules.responsive = {order: typeof def.responsive ===
"undefined" ? 1 : def.responsive, visible:def.visible ===
false ?
false :
true};
73 ResponsiveLayout.prototype.initializeRow =
function(row){
76 if(row.type !==
"calc"){
77 el = document.createElement(
"div");
78 el.classList.add(
"tabulator-responsive-collapse");
80 row.modules.responsiveLayout = {
82 open:this.collapseStartOpen,
85 if(!this.collapseStartOpen){
86 el.style.display =
'none';
91 ResponsiveLayout.prototype.layoutRow =
function(row){
92 var rowEl = row.getElement();
94 if(row.modules.responsiveLayout){
95 rowEl.appendChild(row.modules.responsiveLayout.element);
96 this.generateCollapsedRowContent(row);
101 ResponsiveLayout.prototype.updateColumnVisibility =
function(column, visible){
103 if(column.modules.responsive){
104 column.modules.responsive.visible = visible;
109 ResponsiveLayout.prototype.hideColumn =
function(column){
110 var colCount = this.hiddenColumns.length;
112 column.hide(
false,
true);
114 if(this.mode ===
"collapse"){
115 this.hiddenColumns.unshift(column);
116 this.generateCollapsedContent();
118 if(this.collapseHandleColumn && !colCount){
119 this.collapseHandleColumn.show();
124 ResponsiveLayout.prototype.showColumn =
function(column){
127 column.show(
false,
true);
129 column.setWidth(column.getWidth());
131 if(this.mode ===
"collapse"){
132 index = this.hiddenColumns.indexOf(column);
135 this.hiddenColumns.splice(index, 1);
138 this.generateCollapsedContent();
140 if(this.collapseHandleColumn && !this.hiddenColumns.length){
141 this.collapseHandleColumn.hide();
147 ResponsiveLayout.prototype.update =
function(){
153 let width =
self.table.modules.layout.getMode() ==
"fitColumns" ?
self.table.columnManager.getFlexBaseWidth() :
self.table.columnManager.getWidth();
155 let diff = (
self.table.options.headerVisible ?
self.table.columnManager.element.clientWidth :
self.table.element.clientWidth) - width;
159 let column =
self.columns[
self.index];
162 self.hideColumn(column);
171 let column =
self.columns[
self.index -1];
175 if(diff >= column.getWidth()){
176 self.showColumn(column);
189 if(!
self.table.rowManager.activeRowsCount){
190 self.table.rowManager.renderEmptyScroll();
195 ResponsiveLayout.prototype.generateCollapsedContent =
function(){
197 rows = this.table.rowManager.getDisplayRows();
199 rows.forEach(
function(row){
200 self.generateCollapsedRowContent(row);
204 ResponsiveLayout.prototype.generateCollapsedRowContent =
function(row){
207 if(row.modules.responsiveLayout){
208 el = row.modules.responsiveLayout.element;
210 while(el.firstChild) el.removeChild(el.firstChild);
212 contents = this.collapseFormatter(this.generateCollapsedRowData(row));
214 el.appendChild(contents);
219 ResponsiveLayout.prototype.generateCollapsedRowData =
function(row){
221 data = row.getData(),
225 this.hiddenColumns.forEach(
function(column){
226 var value = column.getFieldValue(data);
228 if(column.definition.title && column.field){
229 if(column.modules.format &&
self.table.options.responsiveLayoutCollapseUseFormatters){
231 mockCellComponent = {
240 getElement:
function(){
241 return document.createElement(
"div");
244 return row.getComponent();
246 getColumn:
function(){
247 return column.getComponent();
252 title: column.definition.title,
253 value: column.modules.format.formatter.call(
self.table.modules.format, mockCellComponent, column.modules.format.params)
257 title: column.definition.title,
267 ResponsiveLayout.prototype.formatCollapsedData =
function(data){
268 var list = document.createElement(
"table"),
271 data.forEach(
function(item){
272 var div = document.createElement(
"div");
274 if(item.value instanceof Node){
275 div.appendChild(item.value);
276 item.value = div.innerHTML;
279 listContents +=
"<tr><td><strong>" + item.title +
"</strong></td><td>" + item.value +
"</td></tr>";
282 list.innerHTML = listContents;
284 return Object.keys(data).length ? list :
"";
287 Tabulator.prototype.registerModule(
"responsiveLayout", ResponsiveLayout);