|
| 1 | +(function() { |
| 2 | + // Configuration: Fields for pagination |
| 3 | + data.recordsTable = 'task'; // The table name to query records from |
| 4 | + data.recordsQuery = ''; // The encoded query to filter records (empty means all records) |
| 5 | + data.recordsFields = ['number', 'short_description']; // The fields to retrieve for each record |
| 6 | + data.recordsPerPage = 10; // Number of records to display per page |
| 7 | + |
| 8 | + // Get pagination parameters |
| 9 | + data.page_id = $sp.getParameter('id'); |
| 10 | + data.page = parseInt($sp.getParameter('page'), 10) || 1; // Current page number, default to 1 |
| 11 | + data.display = parseInt(data.recordsPerPage, 10) > 0 ? parseInt(data.recordsPerPage, 10) : 10; // Ensure positive number of records per page |
| 12 | + |
| 13 | + // Count total records |
| 14 | + var countGa = new GlideAggregate(data.recordsTable); |
| 15 | + countGa.addEncodedQuery(data.recordsQuery); |
| 16 | + countGa.addAggregate('COUNT'); |
| 17 | + countGa.query(); |
| 18 | + if (countGa.next()) { |
| 19 | + data.count = parseInt(countGa.getAggregate('COUNT'), 10); |
| 20 | + } |
| 21 | + |
| 22 | + // Calculate pagination details |
| 23 | + data.pages = calculatePaginationPages(data.count, data.display, data.page); |
| 24 | + |
| 25 | + // Adjust current page if it exceeds the total number of pages |
| 26 | + if (data.pages[data.pages.length - 1] < data.page) { |
| 27 | + data.page = Math.ceil(data.count / data.display); |
| 28 | + } |
| 29 | + |
| 30 | + // Calculate the starting row for the current page |
| 31 | + data.rowStart = (data.page - 1) * data.display; |
| 32 | + var rowEnd = data.rowStart + data.display; |
| 33 | + |
| 34 | + // Fetch records for the current page |
| 35 | + data.records = []; |
| 36 | + var recordsGr = new GlideRecord(data.recordsTable); |
| 37 | + recordsGr.addEncodedQuery(data.recordsQuery); |
| 38 | + recordsGr.chooseWindow(data.rowStart, rowEnd); |
| 39 | + recordsGr.query(); |
| 40 | + while (recordsGr.next()) { |
| 41 | + var record = {}; |
| 42 | + for (var i = 0; i < data.recordsFields.length; i++) { |
| 43 | + var fieldName = data.recordsFields[i]; |
| 44 | + record[fieldName] = (recordsGr.getElement(fieldName) + "").trim(); |
| 45 | + } |
| 46 | + data.records.push(record); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Calculates the page numbers to display in the pagination control |
| 51 | + * @param {number} totalRecords - Total number of records |
| 52 | + * @param {number} recordsPerPage - Number of records per page |
| 53 | + * @param {number} currentPage - Current page number |
| 54 | + * @returns {Array} An array of page numbers and ellipses to display |
| 55 | + */ |
| 56 | + function calculatePaginationPages(totalRecords, recordsPerPage, currentPage) { |
| 57 | + if (recordsPerPage <= 0) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + var totalPages = Math.ceil(totalRecords / recordsPerPage); |
| 62 | + var pages = []; |
| 63 | + var startPage, endPage; |
| 64 | + |
| 65 | + // Determine the range of pages to display |
| 66 | + if (totalPages <= 7) { |
| 67 | + // If 7 or fewer pages, show all |
| 68 | + startPage = 1; |
| 69 | + endPage = totalPages; |
| 70 | + } else { |
| 71 | + // For more than 7 pages, use a sliding window |
| 72 | + if (currentPage < 4) { |
| 73 | + startPage = 1; |
| 74 | + endPage = 5; |
| 75 | + } else if (currentPage === 5) { |
| 76 | + startPage = 3; |
| 77 | + endPage = 7; |
| 78 | + } else if (currentPage + 2 >= totalPages) { |
| 79 | + startPage = totalPages - 4; |
| 80 | + endPage = totalPages; |
| 81 | + } else { |
| 82 | + startPage = currentPage - 2; |
| 83 | + endPage = currentPage + 2; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Add first page and ellipsis if necessary |
| 88 | + if (startPage > 1) { |
| 89 | + pages.push(1); |
| 90 | + if (startPage > 2) { |
| 91 | + pages.push('...'); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Add page numbers |
| 96 | + for (var i = startPage; i <= endPage; i++) { |
| 97 | + pages.push(i); |
| 98 | + } |
| 99 | + |
| 100 | + // Add last page and ellipsis if necessary |
| 101 | + if (endPage < totalPages) { |
| 102 | + if (endPage < totalPages - 1) { |
| 103 | + pages.push('...'); |
| 104 | + } |
| 105 | + pages.push(totalPages); |
| 106 | + } |
| 107 | + |
| 108 | + return pages; |
| 109 | + } |
| 110 | +})(); |
0 commit comments