From 4bd060d9008be3a16d695d1360b921ff13d32409 Mon Sep 17 00:00:00 2001 From: Matthew Hasbach Date: Fri, 13 Mar 2015 21:33:57 -0400 Subject: [PATCH] Added conditional pagination plugin --- .../dataTables.conditionalPagination.js | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 features/conditionalPagination/dataTables.conditionalPagination.js diff --git a/features/conditionalPagination/dataTables.conditionalPagination.js b/features/conditionalPagination/dataTables.conditionalPagination.js new file mode 100644 index 0000000..e7d72a0 --- /dev/null +++ b/features/conditionalPagination/dataTables.conditionalPagination.js @@ -0,0 +1,70 @@ +/** + * @summary ConditionalPagination + * @description Hide pagination controls when the amount of pages is <= 1 + * @version 1.0.0 + * @file dataTables.conditionalPagination.js + * @author Matthew Hasbach (https://github.com/mjhasbach) + * @contact hasbach.git@gmail.com + * @copyright Copyright 2015 Matthew Hasbach + * + * License MIT - http://datatables.net/license/mit + * + * This feature plugin for DataTables hides pagination controls when the amount + * of pages is <= 1. The controls can either appear / disappear or fade in / out + * + * @example + * $('#myTable').DataTable({ + * conditionalPagination: { + * enable: true, + * fade: { + * enable: true, + * speed: 'fast' + * } + * } + * }); + */ + +(function(window, document, $) { + $(document).on('init.dt', function(e, settings) { + if ($.isPlainObject(settings.oInit.conditionalPagination) && settings.oInit.conditionalPagination.enable) { + var api = new $.fn.dataTable.Api(settings), + fade = settings.oInit.conditionalPagination.fade, + fadeSpeed = 'slow', + fadeEnable = false, + conditionalPagination = function() { + var $pagination = $(api.table().container()).find('div.dataTables_paginate'); + + if (api.page.info().pages <= 1) { + if (fadeEnable) { + $pagination.stop().fadeOut(fadeSpeed); + } + else { + $pagination.hide(); + } + } + else { + if (fadeEnable) { + $pagination.stop().fadeIn(fadeSpeed); + } + else { + $pagination.show(); + } + } + }; + + if ($.isPlainObject(fade)) { + if (fade.enable === true) { + fadeEnable = true; + } + + if ($.isNumeric(fade.speed) || $.type(fade.speed) === 'string') { + fadeSpeed = fade.speed; + } + } + + conditionalPagination(); + + api.on('draw.dt', conditionalPagination); + } + }); +})(window, document, jQuery); \ No newline at end of file