Skip to content

Commit

Permalink
feat: add pagination functionality
Browse files Browse the repository at this point in the history
(cherry picked from commit 5865df1)
  • Loading branch information
krantheman authored and mergify[bot] committed Dec 12, 2023
1 parent 28c745e commit d09bda5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
12 changes: 8 additions & 4 deletions hrms/www/jobs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
endblock title %} {% block header %}
<h3 class="mt-0 mb-10 jobs-page">{{ _("Job Openings") }}</h3>
{% endblock header %} {% block page_content %}
<meta id="data" data-filters="{{ all_filters }}" data-no-of-pages="{{ no_of_pages }}"/>
<meta
id="data"
data-filters="{{ all_filters }}"
data-no-of-pages="{{ no_of_pages }}"
/>
<div class="row">
<div class="col-3 text-15">
<div class="flex align-items-center">
Expand Down Expand Up @@ -215,7 +219,7 @@ <h3 class="mt-0 mb-10 jobs-page">{{ _("Job Openings") }}</h3>
page|int > no_of_pages or page|int < 1) else page %}
<button
id="previous"
class="btn btn-default border-right"
class="btn btn-default border-right flex align-items-center"
style="background-color: white"
>
<svg class="icon icon-sm" style="--icon-stroke: var(--gray-600)">
Expand All @@ -224,7 +228,7 @@ <h3 class="mt-0 mb-10 jobs-page">{{ _("Job Openings") }}</h3>
</button>
{% set initial_page = 1 if page|int == 1 else ((page|int / 3 +
0.5)|round(method='floor')|int * 3 - 2) %}
<div style="background-color: white">
<div class="flex" style="background-color: white">
{% set no_of_displayed_pages = 5 if no_of_pages - initial_page > 5
else no_of_pages - initial_page + 1 %} {% for i in
range(no_of_displayed_pages) %} {% set pg = i + initial_page %}
Expand All @@ -245,7 +249,7 @@ <h3 class="mt-0 mb-10 jobs-page">{{ _("Job Openings") }}</h3>
</div>
<button
id="next"
class="btn btn-default border-left"
class="btn btn-default border-left flex align-items-center"
style="background-color: white"
>
<svg class="icon icon-sm" style="--icon-stroke: var(--gray-600)">
Expand Down
17 changes: 9 additions & 8 deletions hrms/www/jobs/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
def get_context(context):
context.parents = [{"name": _("My Account"), "route": "/"}]
context.body_class = "jobs-page"
filters, txt, sort = get_filters_txt_and_sort()
context.job_openings = get_job_openings(filters, txt, sort)
# context.no_of_pages = get_no_of_pages(filters, txt)
context.no_of_pages = 10
page_len = 20
filters, txt, sort, offset = get_filters_txt_sort_offset(page_len)
context.job_openings = get_job_openings(filters, txt, sort, page_len, offset)
context.no_of_pages = get_no_of_pages(filters, txt, page_len)
context.all_filters = get_all_filters(filters)
context.sort = sort

Expand Down Expand Up @@ -60,12 +60,10 @@ def get_job_openings(filters=None, txt=None, sort=None, limit=20, offset=0):
query = query.where((jo.job_title.like(f"%{txt}%")) | (jo.description.like(f"%{txt}%")))

query = query.orderby("posted_on", order=Order.asc if sort == "asc" else Order.desc)

results = query.run(as_dict=True)

for d in results:
d.posted_on = pretty_date(d.posted_on)

return results


Expand Down Expand Up @@ -108,11 +106,12 @@ def get_all_filters(filters=None):
return {key: sorted(value) for key, value in all_filters.items()}


def get_filters_txt_and_sort():
def get_filters_txt_sort_offset(page_len=20):
args = frappe.request.args.to_dict(flat=False)
filters = {}
txt = ""
sort = None
offset = 0
allowed_filters = ["company", "department", "location", "employment_type"]

for d in args:
Expand All @@ -123,5 +122,7 @@ def get_filters_txt_and_sort():
elif d == "sort":
if args["sort"][0]:
sort = args["sort"][0]
elif d == "page":
offset = (int(args["page"][0]) - 1) * page_len

return filters, txt, sort
return filters, txt, sort, offset

0 comments on commit d09bda5

Please sign in to comment.