/** * */ var DataTableColumnFilter = (function ($) { "use strict"; var me = { storage: { /** @property {object} Assoziative Identity-Map der bereits initialisierten DataTables */ tables: {} }, /** * Initialize column filter feature * * @param {String} tableName * @param {Array} settings */ init: function (tableName, settings) { var $table = $('#' + tableName); var table = $table.DataTable(); if ($table.length === 0) { console.warn('DataTableColumnFilter: Table not found.', '#' + tableName); return; } if (typeof settings !== 'object' || !Array.isArray(settings)) { console.warn('DataTableColumnFilter Filter not active.', '#' + tableName); return; } if (settings.length === 0) { console.warn('DataTableColumnFilter: Filter not defined.', '#' + tableName); return; } me.storage.tables[tableName] = table; // Create filter elements me.createFilterRow(tableName, settings); // Show/hide filter cells on responsive resize table.on('responsive-resize', me.onResponsiveResize); // Move filter cells on column reorder //table.on('column-reorder', me.onColumnReorder) }, /** * @param {string} columName * @param {array} filterSettings * * @return {object} Filter settings for given column */ getFilterSettingsByName: function (columName, filterSettings) { return filterSettings.find(function (setting) { if (setting.hasOwnProperty('name') && setting.name === columName) { return setting; } }); }, /** * Create header row with filter elements * * @param {string} tableName * @param {array} filterTypes Types [none|text|number_range] */ createFilterRow: function (tableName, filterTypes) { var $table = me.storage.tables[tableName]; var $thead = $($table.table().header()); var $filterRow = $('
'); var $row1Left = $('') .append('Statement (sql query)
', $debugSql); var $debugBind = $(''); var $debugDuration = $(''); var $row1Right = $('') .append('Bind values (sql query)
', $debugBind) .append('Profiler (sql query)
', $debugDuration); var $row1 = $('').append($row1Left).append($row1Right); var $debugConfig = $(''); var $row2Left = $('') .append('Config options (onInit)
', $debugConfig); var $debugStorage = $('').html(stateLoadedString); var $row2Right = $('') .append('State loaded (onInit)
', $debugStorage); var $row2 = $('').append($row2Left).append($row2Right); $('').append($row1, $row2).appendTo($wrapper); var ajaxResult = api.ajax.json(); me.fillDebugElements($table, ajaxResult); }, /** * @param {jQuery} $table jQuery element */ registerEventListener: function ($table) { $table.on('xhr.dt', function (e, settings, data) { me.fillDebugElements($table, data); }); $table.on('preXhr.dt', function (e, settings, data) { me.fillDebugElements($table, data); }); }, /** * @param {jQuery} $table jQuery element * @param {object} ajaxResult */ fillDebugElements: function ($table, ajaxResult) { if (!ajaxResult.hasOwnProperty('debug')) { return; } var $wrapper = $table.parents('.dataTables_wrapper').first(); var tableName = $table.attr('id'); var initConfigOptions = Helper.getInitConfig(tableName); if (initConfigOptions !== null) { var initConfigString = JSON.stringify(initConfigOptions, undefined, 4); $wrapper.find('.datatable-debug-config').html(initConfigString); } if (ajaxResult.debug.hasOwnProperty('query')) { $wrapper.find('.datatable-debug-sql').html(ajaxResult.debug.query.statement); $wrapper.find('.datatable-debug-bind').html(ajaxResult.debug.query.bindings); } if (ajaxResult.debug.hasOwnProperty('profiler')) { var profilerString = JSON.stringify(ajaxResult.debug.profiler, undefined, 4); $wrapper.find('.datatable-debug-profiler').html(profilerString); } } }; return { init: me.init }; })(jQuery, DataTableHelper); var DataTableRowDetails = (function ($) { "use strict"; var me = { storage: { urls: {}, methods: {} }, init: function () { me.registerEvents(); }, registerEvents: function () { $(document).on('init.dt', function (e, settings) { var tableName = settings.sTableId; var initOptions = settings.oInit; if (!initOptions.hasOwnProperty('rowDetails')) { return; } if (!initOptions.rowDetails.hasOwnProperty('ajax')) { return; } if (!initOptions.rowDetails.ajax.hasOwnProperty('url')) { return; } // Ajax-Url pro DataTable speichern me.storage.urls[tableName] = initOptions.rowDetails.ajax.url; me.storage.methods[tableName] = initOptions.rowDetails.ajax.method || 'POST'; me.registerTableEvents(tableName); }); }, /** * @param {string} tableName */ registerTableEvents: function (tableName) { var $table = $('#' + tableName); if ($table.length === 0) { return; } var ajaxMethod = me.storage.methods[tableName]; var ajaxUrl = me.storage.urls[tableName]; if (typeof ajaxUrl === 'undefined') { return; } $table.on('click', '.dt-details .details', function (e) { e.preventDefault(); var $opener = $(this); var $parentRow = $opener.closest('tr'); var $table = $opener.closest('table.dataTable'); var api = $table.dataTable().api(); var row = api.row($parentRow); var child = row.child; if (child.isShown()) { // Details ausblenden row.child(false); $parentRow.removeClass('parent'); $opener.removeClass('open'); } else { // Details laden und einblenden var ajaxData = $opener.data(); me.fetchChildRow(ajaxMethod, ajaxUrl, ajaxData).then(function (htmlContent) { row.child(htmlContent, 'child').show(); $parentRow.addClass('parent'); $opener.addClass('open'); }); } }); }, /** * @param {string} ajaxMethod * @param {string} ajaxUrl * @param {Object} ajaxParams * * @return {Deferred} */ fetchChildRow: function (ajaxMethod, ajaxUrl, ajaxParams) { return $.ajax({ method: ajaxMethod, url: ajaxUrl, data: ajaxParams, dataType: 'html' }); } }; return { init: me.init }; })(jQuery); /** * Client-side formatter */ // var DataTableFormatter = (function ($) { // "use strict"; // // var me = { // // formatBytes: function (data, type, rowData, meta) { // console.log('formatBytes', data, type); // switch (type) { // case 'display': // return me.formatBytesForDisplay(data); // // case 'sort': // return me.formatBytesForSorting(data); // // case 'type': // return 'num'; // // default: // return data; // } // }, // // /** // * @param {string} value // * // * @return {string} // */ // formatBytesForDisplay: function(value) { // var bytes = parseInt(value); // if (bytes === 0) { // return '0 Bytes'; // } // // var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; // var exponent = Math.floor(Math.log(bytes) / Math.log(1024)); // var decimalString = parseFloat((bytes / Math.pow(1024, exponent)).toFixed(1)) + ''; // // return decimalString.replace('.', ',') + ' ' + sizes[exponent]; // }, // // /** // * @param {string} value // * // * @return {number} // */ // formatBytesForSorting: function (value) { // return parseInt(value); // } // // }; // // return { // formatBytes: me.formatBytes // }; // // })(jQuery); $(document).ready(function () { DataTableHelper.init(); DataTableRowDetails.init(); DataTableDebugger.init(); });