Popup Column Picker

You can easily implement a column-picker UI using the grid's columns property, a ListBox control, and Wijmo's showPopup and hidePopup methods. For example, the grid below starts with an auto-generated set of columns. Click the gear icon at the top-left cell to show a ListBox where you can select the columns you want to display.

Learn about FlexGrid | FlexGrid API Reference

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import { showPopup, hidePopup, hasClass, contains, closest, createElement, getElement, removeChild } from '@grapecity/wijmo'; import { ListBox } from '@grapecity/wijmo.input'; import { FlexGrid } from '@grapecity/wijmo.grid'; document.readyState === 'complete' ? init() : window.onload = init; function init() { // generate some random data var data = [], countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece'], products = ['Widget', 'Sprocket', 'Gadget', 'Doohickey'], colors = ['Black', 'White', 'Red', 'Green', 'Blue'], dt = new Date(); for (var i = 0; i < 100; i++) { var date = new Date(dt.getFullYear(), i % 12, 25, i % 24, i % 60, i % 60), countryId = Math.floor(Math.random() * countries.length), productId = Math.floor(Math.random() * products.length), colorId = Math.floor(Math.random() * colors.length); var item = { id: i, start: date, end: date, country: countries[countryId], product: products[productId], color: colors[colorId], countryId: countryId, productId: productId, colorId: colorId, amount1: Math.random() * 10000 - 5000, amount2: Math.random() * 10000 - 5000, amount3: Math.random() * 10000 - 5000, amount4: Math.random() * 10000 - 5000, amount5: Math.random() * 10000 - 5000, discount: Math.random() / 4, active: i % 4 == 0 }; data.push(item); } // create the grid var theGrid = new FlexGrid('#theGrid', { itemsSource: data, formatItem: function (s, e) { if (e.panel == s.topLeftCells) { e.cell.innerHTML = '<span class="column-picker-icon glyphicon glyphicon-cog"></span>'; } } }); // create the column picker var theColumnPicker = new ListBox('#theColumnPicker', { itemsSource: theGrid.columns, checkedMemberPath: 'visible', displayMemberPath: 'header', lostFocus: function () { hidePopup(theColumnPicker.hostElement); } }); // show the column picker when the user clicks the top-left cell var ref = theGrid.hostElement.querySelector('.wj-topleft'); ref.addEventListener('mousedown', function (e) { if (hasClass(e.target, 'column-picker-icon')) { let host = theColumnPicker.hostElement; if (!host.offsetHeight) { showPopup(host, ref, false, true, false); theColumnPicker.focus(); } else { hidePopup(host, true, true); theGrid.focus(); } e.preventDefault(); } }); // work around Safari/IOS bug (TFS 321525, 361500, 402670) // https://developer.mozilla.org/en-US/docs/Web/Events/click#Safari_Mobile window.addEventListener('touchstart', (e) => { let host = theColumnPicker.hostElement; if (!contains(host, e.target) && !closest(e.target, '.wj-flexgrid')) { hidePopup(host, true, true); } }); // save/restore layout buttons document.getElementById('btnSave').addEventListener('click', function () { localStorage.setItem('myLayout', theGrid.columnLayout); }); document.getElementById('btnRestore').addEventListener('click', function () { var layout = localStorage.getItem('myLayout'); if (layout) { theGrid.columnLayout = layout; } }); // column picker's drag-and-drop var isDragEnabled = false; var dragSrc = null; var dragDst = null; var dragCheckbox = document.getElementById('enableDrag'); dragCheckbox.addEventListener('click', function (e) { var element = e.target; isDragEnabled = element.checked; var host = theColumnPicker.hostElement; var items = host.getElementsByClassName('wj-listbox-item'); for (var i = 0; i < items.length; i++) { enableDragItem(items[i], isDragEnabled); } }); theColumnPicker.formatItem.addHandler(function (s, e) { enableDragItem(e.item, isDragEnabled); }); var theColumnPickerHost = theColumnPicker.hostElement; theColumnPickerHost.addEventListener('dragstart', handleDragStart); theColumnPickerHost.addEventListener('dragover', handleDragOver); theColumnPickerHost.addEventListener('drop', handleDrop); theColumnPickerHost.addEventListener('dragend', handleDragEnd); function enableDragItem(item, enabled) { item.setAttribute('draggable', enabled); } function handleDragStart(e) { dragSrc = closest(e.target, '.wj-listbox-item'); if (dragSrc) { e.dataTransfer.setData('text', dragSrc.innerHTML); e.dataTransfer.effectAllowed = 'move'; } } function handleDragOver(e) { var dragOver = closest(e.target, '.wj-listbox-item'); if (dragDst && dragDst !== dragOver) { removeDropMarker(); } if (dragOver && dragOver !== dragSrc) { e.preventDefault(); e.dataTransfer.dropEffect = 'move'; dragDst = dragOver; var src = getElementIndex(dragSrc); var dst = getElementIndex(dragDst); removeDropMarker(); addDropMarker(dst > src); } else { dragDst = null; } } function handleDrop(e) { if (dragSrc && dragDst) { e.preventDefault(); var src = getElementIndex(dragSrc); var dst = getElementIndex(dragDst); theGrid.columns.moveElement(src, dst); } } function handleDragEnd() { dragSrc = null; dragDst = null; removeDropMarker(); } function getElementIndex(element) { var parent = element.parentElement; var siblings = Array.prototype.slice.call(parent.children); return siblings.indexOf(element); } function removeDropMarker() { removeChild(getElement('.drop-marker')); } function addDropMarker(isAfterPos) { const itemsGap = 10; const width = 6; var margin = itemsGap / width; var height = dragDst.clientHeight; var topPos = dragDst.offsetTop; var leftPos = isAfterPos ? (dragDst.offsetLeft + dragDst.clientWidth + margin) : (dragDst.offsetLeft - itemsGap + margin); var html = `<div class="drop-marker" style="top:${topPos}px;left:${leftPos}px;height:${height}px;width:${width}px"> &nbsp </div>`; createElement(html, theColumnPicker.hostElement); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Wijmo FlexGrid Column Picker</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <div id="theGrid"></div> <div style="display:none"> <div id="theColumnPicker" class="column-picker"></div> </div> <div class="checkbox"> <label> <input id="enableDrag" type="checkbox"> Enable drag-and-drop </label> </div> <p> You can use the grid's <b>columnLayout</b> property to allow users to save and restore column layouts. Click the buttons below to see how this works:</p> <button id="btnSave" class="btn btn-default"> Save Layout </button> <button id="btnRestore" class="btn btn-default"> Restore Layout </button> </div> </body> </html>
.wj-flexgrid { max-height: 300px; margin: 10px 0; } .column-picker-icon { cursor: pointer; color: #FF8754; margin: 3px; } .wj-listbox.column-picker { display: grid; grid-template-columns: repeat(4, 1fr); grid-gap: 0 10px; columns: 4; /* IE fallback */ padding: 12px; margin-left: 12px; margin-top: 26px; box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23); } .wj-listbox .wj-listbox-item > label { display: block; margin: 0 0 3px 0; } .wj-listbox .wj-listbox-item.wj-state-selected { background: transparent; color: inherit; } .wj-listbox .wj-listbox-item:hover { background: rgba(0,0,0,.05); } .wj-listbox .drop-marker { position: absolute; background: #0085c7; opacity: 0.5; pointer-events: none; z-index: 1000; }
(function (global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, meta: { '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { 'jszip': 'npm:jszip/dist/jszip.js', '@grapecity/wijmo': 'npm:@grapecity/wijmo/index.js', '@grapecity/wijmo.input': 'npm:@grapecity/wijmo.input/index.js', '@grapecity/wijmo.styles': 'npm:@grapecity/wijmo.styles', '@grapecity/wijmo.cultures': 'npm:@grapecity/wijmo.cultures', '@grapecity/wijmo.chart': 'npm:@grapecity/wijmo.chart/index.js', '@grapecity/wijmo.chart.analytics': 'npm:@grapecity/wijmo.chart.analytics/index.js', '@grapecity/wijmo.chart.animation': 'npm:@grapecity/wijmo.chart.animation/index.js', '@grapecity/wijmo.chart.annotation': 'npm:@grapecity/wijmo.chart.annotation/index.js', '@grapecity/wijmo.chart.finance': 'npm:@grapecity/wijmo.chart.finance/index.js', '@grapecity/wijmo.chart.finance.analytics': 'npm:@grapecity/wijmo.chart.finance.analytics/index.js', '@grapecity/wijmo.chart.hierarchical': 'npm:@grapecity/wijmo.chart.hierarchical/index.js', '@grapecity/wijmo.chart.interaction': 'npm:@grapecity/wijmo.chart.interaction/index.js', '@grapecity/wijmo.chart.radar': 'npm:@grapecity/wijmo.chart.radar/index.js', '@grapecity/wijmo.chart.render': 'npm:@grapecity/wijmo.chart.render/index.js', '@grapecity/wijmo.chart.webgl': 'npm:@grapecity/wijmo.chart.webgl/index.js', '@grapecity/wijmo.gauge': 'npm:@grapecity/wijmo.gauge/index.js', '@grapecity/wijmo.grid': 'npm:@grapecity/wijmo.grid/index.js', '@grapecity/wijmo.grid.detail': 'npm:@grapecity/wijmo.grid.detail/index.js', '@grapecity/wijmo.grid.filter': 'npm:@grapecity/wijmo.grid.filter/index.js', '@grapecity/wijmo.grid.search': 'npm:@grapecity/wijmo.grid.search/index.js', '@grapecity/wijmo.grid.grouppanel': 'npm:@grapecity/wijmo.grid.grouppanel/index.js', '@grapecity/wijmo.grid.multirow': 'npm:@grapecity/wijmo.grid.multirow/index.js', '@grapecity/wijmo.grid.transposed': 'npm:@grapecity/wijmo.grid.transposed/index.js', '@grapecity/wijmo.grid.transposedmultirow': 'npm:@grapecity/wijmo.grid.transposedmultirow/index.js', '@grapecity/wijmo.grid.pdf': 'npm:@grapecity/wijmo.grid.pdf/index.js', '@grapecity/wijmo.grid.sheet': 'npm:@grapecity/wijmo.grid.sheet/index.js', '@grapecity/wijmo.grid.xlsx': 'npm:@grapecity/wijmo.grid.xlsx/index.js', '@grapecity/wijmo.grid.selector': 'npm:@grapecity/wijmo.grid.selector/index.js', '@grapecity/wijmo.grid.cellmaker': 'npm:@grapecity/wijmo.grid.cellmaker/index.js', '@grapecity/wijmo.nav': 'npm:@grapecity/wijmo.nav/index.js', '@grapecity/wijmo.odata': 'npm:@grapecity/wijmo.odata/index.js', '@grapecity/wijmo.olap': 'npm:@grapecity/wijmo.olap/index.js', '@grapecity/wijmo.pdf': 'npm:@grapecity/wijmo.pdf/index.js', '@grapecity/wijmo.pdf.security': 'npm:@grapecity/wijmo.pdf.security/index.js', '@grapecity/wijmo.viewer': 'npm:@grapecity/wijmo.viewer/index.js', '@grapecity/wijmo.xlsx': 'npm:@grapecity/wijmo.xlsx/index.js', '@grapecity/wijmo.undo': 'npm:@grapecity/wijmo.undo/index.js', '@grapecity/wijmo.interop.grid': 'npm:@grapecity/wijmo.interop.grid/index.js', '@grapecity/wijmo.touch': 'npm:@grapecity/wijmo.touch/index.js', '@grapecity/wijmo.cloud': 'npm:@grapecity/wijmo.cloud/index.js', '@grapecity/wijmo.barcode': 'npm:@grapecity/wijmo.barcode/index.js', '@grapecity/wijmo.barcode.common': 'npm:@grapecity/wijmo.barcode.common/index.js', '@grapecity/wijmo.barcode.composite': 'npm:@grapecity/wijmo.barcode.composite/index.js', '@grapecity/wijmo.barcode.specialized': 'npm:@grapecity/wijmo.barcode.specialized/index.js', 'jszip': 'npm:jszip/dist/jszip.js', 'bootstrap.css': 'npm:bootstrap/dist/css/bootstrap.min.css', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' }, } }); })(this);