This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpatternfly.dataTables.pfColVis.js
151 lines (143 loc) · 4.67 KB
/
patternfly.dataTables.pfColVis.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @summary pfColVis for DataTables
* @description An extension providing columns visibility control functionality for DataTables. This ensures
* DataTables meets the Patternfly design pattern with a toolbar.
*
* To enable a colvis, the user just need to specify a region for placing colvis menu. By default, the colvis
* menu includes the items derived from all columns of Datatables. And of course user can also limit the
* generation of the column's visibility checkbox through picking out column index.
*
* The toolbar is expected to contain the classes as shown in the example below.
*
* Example:
*
* <!-- NOTE: Some configuration may be omitted for clarity -->
* <div class="row toolbar-pf table-view-pf-toolbar" id="toolbar1">
* <div class="col-sm-12">
* <form class="toolbar-pf-actions">
* <div class="form-group toolbar-pf-filter">
* </div>
* <div class="form-group toolbar-pf-sort">
* <div class="dropdown btn-group">
* </div>
* <button class="btn btn-link" type="button">
* <span class="fa fa-sort-alpha-asc"></span>
* </button>
* <div class="dropdown btn-group">
* <button class="btn btn-link dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
* <span class="fa fa-columns"></span>
* </button>
* <ul class="dropdown-menu table-view-pf-colvis-menu">
* <li><input type="checkbox" value="1" checked><label>Rendering Engine</label></li>
* <li><input type="checkbox" value="2" checked><label>Browser</label></li>
* <li><input type="checkbox" value="3" checked><label>Platform(s)</label></li>
* <li><input type="checkbox" value="4" checked><label>Engine Version</label></li>
* <li><input type="checkbox" value="5" checked><label>CSS Grade</label></li>
* </ul>
* </div>
* </div>
* ...
* </form>
* </div>
* </div>
* <table class="table table-striped table-bordered table-hover" id="table1">
* <thead>
* <tr>
* <th><input type="checkbox" name="selectAll"></th>
* <th>Rendering Engine</th>
* <th>Browser</th>
* </tr>
* </thead>
* </table>
* ...
* <script>
* // NOTE: Some properties may be omitted for clarity
* $(document).ready(function() {
* var dt = $("#table1").DataTable({
* columns: [
* { data: null, ... },
* { data: "engine" },
* { data: "browser" }
* ],
* data: [
* { engine: "Gecko", browser: "Firefox" }
* { engine: "Trident", browser: "Mozilla" }
* ],
* dom: "t",
* pfConfig: {
* ...
* colvisMenuSelector: '.table-view-pf-colvis-menu'
* }
* });
*
* });
* </script>
*/
(function (factory) {
"use strict";
if (typeof define === "function" && define.amd ) {
// AMD
define (["jquery", "datatables.net"], function ($) {
return factory ($, window, document);
});
} else if (typeof exports === "object") {
// CommonJS
module.exports = function (root, $) {
if (!root) {
root = window;
}
if (!$ || !$.fn.dataTable) {
$ = require("datatables.net")(root, $).$;
}
return factory($, root, root.document);
};
} else {
// Browser
factory(jQuery, window, document);
}
}(function ($, window, document, undefined) {
"use strict";
var DataTable = $.fn.dataTable;
DataTable.pfColVis = {};
/**
* Initialize
*
* @param {DataTable.Api} dt DataTable
* @private
*/
DataTable.pfColVis.init = function (dt) {
var i;
var ctx = dt.settings()[0];
var opts = (ctx.oInit.pfConfig) ? ctx.oInit.pfConfig : {};
if (opts.colvisMenuSelector === undefined) {
return;
}
ctx._pfColVis = {};
ctx._pfColVis.colvisMenu = $(opts.colvisMenuSelector, opts.toolbarSelector);
// Attach event handler for checkbox of ColVis menu
ctx._pfColVis.colvisMenu.on('click', 'li', { 'dt': dt }, colvisMenuHandler);
};
// Local functions
/**
* Handle actions when ColVis menu items are toggled
*
* @param {object} jquery eventObject - click event of ColVis menu items
* @private
*/
function colvisMenuHandler (event) {
var $check = $(this).children(':checkbox');
if (event.target.nodeName !== 'INPUT') {
event.stopPropagation();
$check.prop('checked', !$check.prop('checked'));
}
event.data.dt.column($check.val()).visible($check.prop('checked'));
}
// DataTables creation
$(document).on("init.dt", function (e, ctx, json) {
if (e.namespace !== "dt") {
return;
}
DataTable.pfColVis.init(new DataTable.Api(ctx));
});
return DataTable.pfColVis;
}));