/** * @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: * * *
*
*
*
*
*
* * * *
* ... *
*
*
* * * * * * * * *
Rendering EngineBrowser
* ... * */ (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; }));