Socialify

Folder ..

Viewing pension.ejs
196 lines (185 loc) • 5.9 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<!DOCTYPE html>
<html lang="en">
  <head>
    <%- include('partials/head') %>
    <style>
      .ui.menu {
        margin-top: 0;
      }
      .scrollY {
        max-height: calc(100vh - 8rem);
        margin-top: 1rem;
        overflow-y: scroll;
      }
      .ui.table thead tr:first-child > th {
        position: sticky !important;
        top: 0;
        z-index: 2;
      }
      .ui.table tfoot tr:first-child > th {
        position: sticky !important;
        bottom: 0;
        z-index: 2;
      }
    </style>
  </head>

  <body>
    <%- include('partials/navbar') %>

    <!-- Drop down for selecting limit -->

    <div class="ui container">
      <div class="ui floating labeled icon dropdown button">
        <i class="list icon"></i>
        <span class="text">Items</span>
        <div class="menu">
          <div class="item" onclick="redirectToLimit(100)">100</div>
          <div class="item" onclick="redirectToLimit(250)">250</div>
          <div class="item" onclick="redirectToLimit(500)">500</div>
          <div class="item" onclick="redirectToLimit(1000)">1000</div>
        </div>
      </div>
    </div>

    <div class="scrollY">
      <table class="ui selectable table">
        <thead>
          <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Date of Disbursement</th>
            <th>Pension Amount</th>
          </tr>
        </thead>
        <tbody>
          <% for(var i=0; i < transactions.length; i++) { %>
          <tr>
            <td><%= transactions[i].first_name %></td>
            <td><%= transactions[i].last_name %></td>
            <td><%= transactions[i].pen_date_disbursment %></td>
            <td><%= transactions[i].pen_amount %></td>
            <% } %>
          </tr>
        </tbody>

        <!-- Display a pagination -->
        <tfoot>
          <tr>
            <th colspan="9">
              <div class="ui right floated pagination menu">
                <a class="icon item" id="pageLeft">
                  <i class="left chevron disabled icon"></i>
                </a>

                <a class="icon item" id="pageRight">
                  <i class="right chevron disabled icon"></i>
                </a>
              </div>
            </th>
          </tr>
        </tfoot>
      </table>
    </div>
  </body>
  <%- include('partials/scripts') %>
  <script>
    const count = "<%= count %>";
  </script>
  <script>
    // Get current page and limit query parameter
    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);
    const page = urlParams.get("page") || 1;
    const limit = urlParams.get("limit") || 100;
    const numberOfPages = Math.ceil(count / limit);
    const pagination = $(".pagination");
    const pageLeft = $("#pageLeft");
    const pageRight = $("#pageRight");
    if (page > 1) {
      pageLeft.removeClass("disabled");
      pageLeft.attr(
        "href",
        `/transactions/pension?page=${parseInt(page) - 1}&limit=${limit}`
      );
    } else {
      pageLeft.addClass("disabled");
    }
    if (page < numberOfPages) {
      pageRight.removeClass("disabled");
      pageRight.attr(
        "href",
        `/transactions/pension?page=${parseInt(page) + 1}&limit=${limit}`
      );
    } else {
      pageRight.addClass("disabled");
    }

    function redirectToLimit(limit) {
      window.location.href = `/transactions/pension?page=1&limit=${limit}`;
    }

    addPageNumbers(numberOfPages);

    function addPageNumbers(numberOfPages) {
      // Add page numbers from current page to 2 pages before and 2 pages after, if there are more than 5 pages
      if (numberOfPages > 5) {
        let startingPoint = page - 2;
        if (page < 3) {
          startingPoint = 1;
        } else if (page > numberOfPages - 2) {
          startingPoint = numberOfPages - 4;
        } else {
          startingPoint = page - 2;
        }
        for (let i = startingPoint; i < startingPoint + 5; i++) {
          const linkElement = document.createElement("a");
          linkElement.innerHTML = i;
          linkElement.setAttribute(
            "href",
            `/transactions/pension?page=${i}&limit=${limit}`
          );
          linkElement.setAttribute("id", `page${i}`);
          linkElement.classList.add("item");
          pageRight.before(linkElement);
        }
        // add dots
        const dots = document.createElement("a");
        dots.innerHTML = "...";
        dots.classList.add("item");
        pageRight.before(dots);

        // add first page if page > 3
        if (page > 3) {
          const firstPage = document.createElement("a");
          firstPage.innerHTML = 1;
          firstPage.setAttribute(
            "href",
            `/transactions/pension?page=1&limit=${limit}`
          );
          firstPage.setAttribute("id", `page1`);
          firstPage.classList.add("item");
          pageLeft.after(dots);
          pageLeft.after(firstPage);
        }

        // add last page
        if (page != numberOfPages) {
          const lastPage = document.createElement("a");
          lastPage.innerHTML = numberOfPages;
          lastPage.setAttribute(
            "href",
            `/transactions/pension?page=${numberOfPages}&limit=${limit}`
          );
          lastPage.classList.add("item");
          pageRight.before(lastPage);
        }
      } else {
        // add page numbers
        for (let i = 1; i <= numberOfPages; i++) {
          // Insert before pageRight
          const linkElement = document.createElement("a");
          linkElement.innerHTML = i;
          linkElement.setAttribute(
            "href",
            `/transactions/pension?page=${i}&limit=${limit}`
          );
          linkElement.setAttribute("id", `page${i}`);
          linkElement.classList.add("item");
          pageRight.before(linkElement);
        }
      }
      const currentPageElement = document.getElementById(`page${page}`);
      currentPageElement.classList.add("active");
    }
  </script>
</html>