From 7829e3654ce5187362f5cec5a453ba1be7e876c0 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 28 Aug 2020 10:51:55 +1000 Subject: [PATCH] Add callback when a column is shown / hidden --- .../static/script/inventree/tables.js | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/InvenTree/InvenTree/static/script/inventree/tables.js b/InvenTree/InvenTree/static/script/inventree/tables.js index 079eee3724..34f8824412 100644 --- a/InvenTree/InvenTree/static/script/inventree/tables.js +++ b/InvenTree/InvenTree/static/script/inventree/tables.js @@ -100,6 +100,8 @@ function visibleColumnString(columns) { */ $.fn.inventreeTable = function(options) { + var table = this; + var tableName = options.name || 'table'; var varName = tableName + '-pagesize'; @@ -110,14 +112,51 @@ $.fn.inventreeTable = function(options) { options.rememberOrder = true; options.sortable = true; options.search = true; + options.showColumns = true; // Callback to save pagination data options.onPageChange = function(number, size) { inventreeSave(varName, size); }; + // Add a callback when the table is loaded + table.on('load-success.bs.table', function() { + + // Load visible column list + var visibleColumns = inventreeLoad(`table_columns_${tableName}`, null); + + // If a set of visible columns has been saved, load! + if (visibleColumns) { + var columns = visibleColumns.split(","); + + // Which columns are currently visible? + var visible = table.bootstrapTable('getVisibleColumns'); + + visible.forEach(function(column) { + // Visible field should *not* be visible! (hide it!) + if (!columns.includes(visible.field)) { + table.bootstrapTable('hideColumn', visible.field); + } + }); + } + }); + + // Callback when a column is changed + options.onColumnSwitch = function(field, checked) { + console.log(`${field} -> ${checked}`); + + var columns = table.bootstrapTable('getVisibleColumns'); + + var text = visibleColumnString(columns); + + // Save visible columns + inventreeSave(`table_columns_${tableName}`, text); + + console.log('saving: ' + text); + }; + // Standard options for all tables - this.bootstrapTable(options); + table.bootstrapTable(options); } function customGroupSorter(sortName, sortOrder, sortData) {