From cc5200fd2d3aa3021e748dd117d50f14c79fbdff Mon Sep 17 00:00:00 2001 From: Sanjay B Date: Thu, 28 Oct 2021 21:49:16 +0530 Subject: [PATCH] add sort to films page --- app/static/js/films.js | 15 +++++++++++++++ app/templates/films.html | 27 +++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 app/static/js/films.js diff --git a/app/static/js/films.js b/app/static/js/films.js new file mode 100644 index 0000000..42185dd --- /dev/null +++ b/app/static/js/films.js @@ -0,0 +1,15 @@ +document.getElementById('sort-select').addEventListener('change', function () { + const sortValue = this.value + const $films = [...document.querySelectorAll('.film')] + console.log('$films', $films) + $films.sort((a, b) => { + console.log('a', a) + const aVal = a.dataset[sortValue] + const bVal = b.dataset[sortValue] + return aVal < bVal ? -1 : 1 + }) + document.getElementById('films').innerHTML = '' + $films.forEach($film => { + document.getElementById('films').appendChild($film) + }) +}) \ No newline at end of file diff --git a/app/templates/films.html b/app/templates/films.html index 3b1b15a..2e85e2d 100644 --- a/app/templates/films.html +++ b/app/templates/films.html @@ -1,9 +1,28 @@ {% extends "base.html" %} {% block body_class%}animated animated-text{% endblock %} {% block main %} -
+ +
+ Sort: + +
+