From dfb0614b1f5cdcc56376ba9fa317999863995b08 Mon Sep 17 00:00:00 2001 From: joshua-gould Date: Mon, 4 Apr 2016 13:41:10 -0400 Subject: [PATCH] column picker --- src/ui/table.js | 106 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 36 deletions(-) diff --git a/src/ui/table.js b/src/ui/table.js index e509008..8766a64 100644 --- a/src/ui/table.js +++ b/src/ui/table.js @@ -35,6 +35,9 @@ morpheus.Table = function(options) { $gridDiv.appendTo(options.$el); var columns = options.columns; this.columns = columns; + var visibleColumns = columns.filter(function(c) { + return c.visible; + }); var grid = new morpheus.Grid({ gridOptions : { forceFitColumns : true, @@ -47,7 +50,7 @@ morpheus.Table = function(options) { }, $el : $gridDiv, items : options.items, - columns : columns + columns : visibleColumns }); this.grid = grid; if (options.search) { @@ -56,39 +59,59 @@ morpheus.Table = function(options) { tableSearch.setTable(this); this.tableSearch = tableSearch; } + if (visibleColumns.length !== this.columns.length) { + var select = []; + select + .push(''); + var $select = $(select.join('')); + var $div = $('
'); + $select.appendTo($div); + $div.prependTo(options.$el); + $select.selectpicker({ + iconBase : 'fa', + tickIcon : 'fa-check', + style : 'btn-default btn-sm' + }); + $select.on('change', function() { + var selectedItems = $select.val(); + var selectedItemsSet = new morpheus.Set(); + selectedItems.forEach(function(item) { + selectedItemsSet.add(parseInt(item)); + }); + var visibleColumns = []; + _this.columns.forEach(function(c, i) { + if (selectedItemsSet.has(i)) { + visibleColumns.push(c); + } + }); + grid.setColumns(visibleColumns); + _this.resize(); + _this.redraw(); + + }); + } var collapsed = false; + var lastWidth = -1; var resize = function() { if (!_this.options.responsive) { return; } - var gridWidth; - // if (widthFound) { // get size from fraction of window width - // var width = document.body.clientWidth || $(window).width(); - // var viewportIndex = -1; - // var viewportSize = morpheus.Util.viewPortSize(); - // - // for (var i = viewports.length - 1; i >= 0; i--) { - // if (viewportSize === viewports[i]) { - // viewportIndex = i; - // break; - // } - // } - // - // var fraction = 12; - // - // for (var i = viewportIndex; i >= 0; i--) { - // var f = spec[viewports[i]]; - // if (f) { - // fraction = f; - // break; - // } - // } - // fraction /= 12; - // var gridWidth = parseInt(Math.floor(width * fraction - 30)); - // - // } else { - gridWidth = options.$el.width(); - // } + + var gridWidth = options.$el.width(); + if (gridWidth === lastWidth) { + return; + } + lastWidth = gridWidth; $gridDiv.css('width', gridWidth + 'px'); // if (options.responsiveHeight) { @@ -267,11 +290,16 @@ morpheus.Table.prototype = { // show column names } + matches + .sort(function(a, b) { + return (a.value === b.value ? 0 + : (a.value < b.value ? -1 : 1)); + }); return response(matches); } var field = null; var semi = token.indexOf(':'); - + var regex = new RegExp('^' + morpheus.Util.escapeRegex(token), 'i'); if (semi > 0) { // field search? if (token.charCodeAt(semi - 1) !== 92) { // \: var possibleField = $.trim(token.substring(0, semi)); @@ -296,14 +324,17 @@ morpheus.Table.prototype = { } } else if (ncolumns > 1) { + var regex = new RegExp('^' + morpheus.Util.escapeRegex(token), 'i'); for (var j = 0; j < ncolumns; j++) { var field = columns[j].name; - matches.push({ - value : field + ':', - label : '' + field - + ':', - show : true - }); + if (regex.test(field)) { + matches.push({ + value : field + ':', + label : '' + field + + ':', + show : true + }); + } } } var set = new morpheus.Set(); @@ -550,6 +581,9 @@ morpheus.Table.createOptions = function(options) { renderer : morpheus.Table.defaultRenderer }, c); + if (column.visible === undefined) { + column.visible = true; + } if (!column.getter) { column.getter = column.field == null ? function(item) { return item; -- GitLab