diff --git a/event_tracker/static/scripts/bootstrap_input.js b/event_tracker/static/scripts/bootstrap_input.js index 4ca21cd..01a103d 100644 --- a/event_tracker/static/scripts/bootstrap_input.js +++ b/event_tracker/static/scripts/bootstrap_input.js @@ -1,6 +1,8 @@ /** * Plug-in offers the same functionality as `default` pagination type - * (see `pagingType` option) but with input field for jumping pages, for use with bootstrap theme. + * (see `pagingType` option) but with an input field for jumping pages, for use with bootstrap theme. + * A combination of Dist-DataTables-Bootstrap5/js/dataTables.bootstrap5.js and + * Plugins/pagination/input.js * * @example * $(document).ready(function() { @@ -10,35 +12,89 @@ * } ); */ -$.fn.DataTable.ext.pager.bootstrap_input = function (page, pages) { - return ['first', 'previous', 'input', 'next', 'last']; -}; +(function ($) { + /** + * Add bootstrap themed input field (based on input.js) with addition UX tweaks of disabling for single + * page tables and reacting to the enter key on text input. + */ + function createInputElement(settings) { + const pageInfo = $(settings.nTable).DataTable().page.info(); + const $input = $('', { + class: 'form-control rounded-0 text-end', + type: 'number', + min: 1, + max: pageInfo.pages, + }).val(pageInfo.page + 1); + if (pageInfo.pages === 1) { + $input.prop("disabled", true) + } -main_pageButtonFunc = $.fn.DataTable.ext.renderer.pageButton.bootstrap + var changeHandler = function (e) { + if (e.target.value === '' || e.target.value.match(/[^0-9]/)) { + /* Nothing entered or non-numeric character */ + e.target.value = e.target.value.replace(/[^\d]/g, ''); // don't even allow anything but digits + return; + } -$.fn.DataTable.ext.renderer.pageButton = $.extend(true, $.fn.DataTable.ext.renderer.pageButton, - { - bootstrap: function (settings, host, idx, buttons, page, pages) { - main_pageButtonFunc(settings, host, idx, buttons, page, pages); + const page = Number(e.target.value - 1); + $(settings.nTable).DataTable().page(page).draw(false); + }; - pages = Math.max(pages, 1); + $input.on("change", changeHandler) + $input.on('keyup', function (e) { + if (e.key === 'Enter' || e.keyCode === 13) { + changeHandler(e); + } + }); - let input_html = '