|
1 | | - /*** |
2 | | - * A control to add a Column Picker (right+click on any column header to reveal the column picker) |
3 | | - * |
4 | | - * USAGE: |
5 | | - * |
6 | | - * Add the slick.columnpicker.(js|css) files and register it with the grid. |
7 | | - * |
8 | | - * Available options, by defining a columnPicker object: |
9 | | - * |
10 | | - * var options = { |
11 | | - * enableCellNavigation: true, |
12 | | - * columnPicker: { |
13 | | - * columnTitle: "Columns", // default to empty string |
14 | | - * |
15 | | - * // the last 2 checkboxes titles |
16 | | - * hideForceFitButton: false, // show/hide checkbox near the end "Force Fit Columns" (default:false) |
17 | | - * hideSyncResizeButton: false, // show/hide checkbox near the end "Synchronous Resize" (default:false) |
18 | | - * forceFitTitle: "Force fit columns", // default to "Force fit columns" |
19 | | - * headerColumnValueExtractor: "Extract the column label" // default to column.name |
20 | | - * syncResizeTitle: "Synchronous resize", // default to "Synchronous resize" |
21 | | - * } |
22 | | - * }; |
23 | | - * |
24 | | - * @class Slick.Controls.ColumnPicker |
25 | | - * @constructor |
26 | | - */ |
| 1 | +/*** |
| 2 | + * A control to add a Column Picker (right+click on any column header to reveal the column picker) |
| 3 | + * |
| 4 | + * USAGE: |
| 5 | + * |
| 6 | + * Add the slick.columnpicker.(js|css) files and register it with the grid. |
| 7 | + * |
| 8 | + * Available options, by defining a columnPicker object: |
| 9 | + * |
| 10 | + * var options = { |
| 11 | + * enableCellNavigation: true, |
| 12 | + * columnPicker: { |
| 13 | + * columnTitle: "Columns", // default to empty string |
| 14 | + * |
| 15 | + * // the last 2 checkboxes titles |
| 16 | + * hideForceFitButton: false, // show/hide checkbox near the end "Force Fit Columns" (default:false) |
| 17 | + * hideSyncResizeButton: false, // show/hide checkbox near the end "Synchronous Resize" (default:false) |
| 18 | + * forceFitTitle: "Force fit columns", // default to "Force fit columns" |
| 19 | + * headerColumnValueExtractor: "Extract the column label" // default to column.name |
| 20 | + * syncResizeTitle: "Synchronous resize", // default to "Synchronous resize" |
| 21 | + * } |
| 22 | + * }; |
| 23 | + * |
| 24 | + * @class Slick.Controls.ColumnPicker |
| 25 | + * @constructor |
| 26 | + */ |
27 | 27 |
|
28 | 28 | 'use strict'; |
29 | 29 |
|
30 | 30 | (function ($) { |
31 | 31 | function SlickColumnPicker(columns, grid, options) { |
32 | 32 | var _grid = grid; |
33 | 33 | var _options = options; |
| 34 | + var $columnTitleElm; |
| 35 | + var columns; |
34 | 36 | var $list; |
35 | 37 | var $menu; |
36 | 38 | var columnCheckboxes; |
|
56 | 58 | _options = $.extend({}, defaults, options); |
57 | 59 |
|
58 | 60 | $menu = $("<div class='slick-columnpicker' style='display:none' />").appendTo(document.body); |
59 | | - var $close = $("<button type='button' class='close' data-dismiss='slick-columnpicker' aria-label='Close'><span class='close' aria-hidden='true'>×</span></button>").appendTo($menu); |
| 61 | + $("<button type='button' class='close' data-dismiss='slick-columnpicker' aria-label='Close'><span class='close' aria-hidden='true'>×</span></button>").appendTo($menu); |
60 | 62 |
|
61 | 63 | // user could pass a title on top of the columns list |
62 | | - if(_options.columnPickerTitle || (_options.columnPicker && _options.columnPicker.columnTitle)) { |
| 64 | + if (_options.columnPickerTitle || (_options.columnPicker && _options.columnPicker.columnTitle)) { |
63 | 65 | var columnTitle = _options.columnPickerTitle || _options.columnPicker.columnTitle; |
64 | | - var $title = $("<div class='title'/>").append(columnTitle); |
65 | | - $title.appendTo($menu); |
| 66 | + $columnTitleElm = $("<div class='title'/>").append(columnTitle); |
| 67 | + $columnTitleElm.appendTo($menu); |
66 | 68 | } |
67 | 69 |
|
68 | 70 | $menu.on("click", updateColumn); |
|
96 | 98 | columnCheckboxes = []; |
97 | 99 |
|
98 | 100 | var $li, $input; |
99 | | - var columnLabel; |
| 101 | + var columnLabel, excludeCssClass; |
100 | 102 | for (var i = 0; i < columns.length; i++) { |
101 | | - $li = $("<li />").appendTo($list); |
| 103 | + excludeCssClass = columns[i].excludeFromColumnPicker ? "hidden" : ""; |
| 104 | + $li = $('<li class="' + excludeCssClass + '" />').appendTo($list); |
102 | 105 | $input = $("<input type='checkbox' />").data("column-id", columns[i].id); |
103 | 106 | columnCheckboxes.push($input); |
104 | 107 |
|
|
127 | 130 | $li = $("<li />").appendTo($list); |
128 | 131 | $input = $("<input type='checkbox' />").data("option", "autoresize"); |
129 | 132 | $("<label />") |
130 | | - .text(forceFitTitle) |
131 | | - .prepend($input) |
132 | | - .appendTo($li); |
| 133 | + .text(forceFitTitle) |
| 134 | + .prepend($input) |
| 135 | + .appendTo($li); |
133 | 136 | if (_grid.getOptions().forceFitColumns) { |
134 | 137 | $input.attr("checked", "checked"); |
135 | 138 | } |
|
140 | 143 | $li = $("<li />").appendTo($list); |
141 | 144 | $input = $("<input type='checkbox' />").data("option", "syncresize"); |
142 | 145 | $("<label />") |
143 | | - .text(syncResizeTitle) |
144 | | - .prepend($input) |
145 | | - .appendTo($li); |
| 146 | + .text(syncResizeTitle) |
| 147 | + .prepend($input) |
| 148 | + .appendTo($li); |
146 | 149 | if (_grid.getOptions().syncColumnCellResize) { |
147 | 150 | $input.attr("checked", "checked"); |
148 | 151 | } |
149 | 152 | } |
150 | 153 |
|
151 | 154 | $menu |
152 | | - .css("top", e.pageY - 10) |
153 | | - .css("left", e.pageX - 10) |
154 | | - .css("max-height", $(window).height() - e.pageY -10) |
155 | | - .fadeIn(_options.fadeSpeed); |
| 155 | + .css("top", e.pageY - 10) |
| 156 | + .css("left", e.pageX - 10) |
| 157 | + .css("max-height", $(window).height() - e.pageY - 10) |
| 158 | + .fadeIn(_options.fadeSpeed); |
156 | 159 |
|
157 | 160 | $list.appendTo($menu); |
158 | 161 | } |
|
167 | 170 | var current = _grid.getColumns().slice(0); |
168 | 171 | var ordered = new Array(columns.length); |
169 | 172 | for (var i = 0; i < ordered.length; i++) { |
170 | | - if ( _grid.getColumnIndex(columns[i].id) === undefined ) { |
| 173 | + if (_grid.getColumnIndex(columns[i].id) === undefined) { |
171 | 174 | // If the column doesn't return a value from getColumnIndex, |
172 | 175 | // it is hidden. Leave it in this position. |
173 | 176 | ordered[i] = columns[i]; |
|
176 | 179 | ordered[i] = current.shift(); |
177 | 180 | } |
178 | 181 | } |
| 182 | + columns = ordered; |
| 183 | + } |
179 | 184 |
|
180 | | - // filter out excluded column header when necessary |
181 | | - // (works with IE9+, older browser requires a polyfill for the filter to work, visit MDN for more info) |
182 | | - if (Array.isArray(ordered) && typeof ordered.filter === 'function') { |
183 | | - columns = ordered.filter(function(columnDef) { |
184 | | - return !columnDef.excludeFromColumnPicker; |
185 | | - }); |
186 | | - } else { |
187 | | - columns = ordered; |
| 185 | + /** Update the Titles of each sections (command, customTitle, ...) */ |
| 186 | + function updateAllTitles(gridMenuOptions) { |
| 187 | + if ($columnTitleElm && $columnTitleElm.text) { |
| 188 | + $columnTitleElm.text(gridMenuOptions.columnTitle); |
188 | 189 | } |
189 | 190 | } |
190 | 191 |
|
191 | 192 | function updateColumn(e) { |
192 | 193 | if ($(e.target).data("option") == "autoresize") { |
193 | 194 | if (e.target.checked) { |
194 | | - _grid.setOptions({forceFitColumns:true}); |
| 195 | + _grid.setOptions({ forceFitColumns: true }); |
195 | 196 | _grid.autosizeColumns(); |
196 | 197 | } else { |
197 | | - _grid.setOptions({forceFitColumns:false}); |
| 198 | + _grid.setOptions({ forceFitColumns: false }); |
198 | 199 | } |
199 | 200 | return; |
200 | 201 | } |
201 | 202 |
|
202 | 203 | if ($(e.target).data("option") == "syncresize") { |
203 | 204 | if (e.target.checked) { |
204 | | - _grid.setOptions({syncColumnCellResize:true}); |
| 205 | + _grid.setOptions({ syncColumnCellResize: true }); |
205 | 206 | } else { |
206 | | - _grid.setOptions({syncColumnCellResize:false}); |
| 207 | + _grid.setOptions({ syncColumnCellResize: false }); |
207 | 208 | } |
208 | 209 | return; |
209 | 210 | } |
|
222 | 223 | } |
223 | 224 |
|
224 | 225 | _grid.setColumns(visibleColumns); |
225 | | - onColumnsChanged.notify({columns: visibleColumns, grid: _grid}); |
| 226 | + onColumnsChanged.notify({ columns: visibleColumns, grid: _grid }); |
226 | 227 | } |
227 | 228 | } |
228 | 229 |
|
|
236 | 237 | "init": init, |
237 | 238 | "getAllColumns": getAllColumns, |
238 | 239 | "destroy": destroy, |
| 240 | + "updateAllTitles": updateAllTitles, |
239 | 241 | "onColumnsChanged": onColumnsChanged |
240 | 242 | }; |
241 | 243 | } |
242 | 244 |
|
243 | 245 | // Slick.Controls.ColumnPicker |
244 | | - $.extend(true, window, { Slick:{ Controls:{ ColumnPicker:SlickColumnPicker }}}); |
| 246 | + $.extend(true, window, { Slick: { Controls: { ColumnPicker: SlickColumnPicker } } }); |
245 | 247 | })(jQuery); |
0 commit comments