From 38c9d869f3a781bc8e95e7cef0316d203d7ff5e4 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Mon, 22 Dec 2014 20:08:43 +0100 Subject: improve pagination --- imdb-lookup/index.jinja2.html | 3 ++- imdb-lookup/js/app.js | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) (limited to 'imdb-lookup') diff --git a/imdb-lookup/index.jinja2.html b/imdb-lookup/index.jinja2.html index b2bde45..d02ccf9 100644 --- a/imdb-lookup/index.jinja2.html +++ b/imdb-lookup/index.jinja2.html @@ -28,7 +28,8 @@
{{#each page in pages}} - + {{page.runningLabel}} + {{/each}}
diff --git a/imdb-lookup/js/app.js b/imdb-lookup/js/app.js index d5f7c53..c2bcbd1 100644 --- a/imdb-lookup/js/app.js +++ b/imdb-lookup/js/app.js @@ -51,24 +51,49 @@ App.Movie = DS.Model.extend({ App.IndexController = Ember.ArrayController.extend({ page: 1, perPage: "10", + sortProperties: ["title"], visiblePerPage: ["10", "20", "25", "50", "100", "250", "500"], pages: function() { var list = []; + var prev = null; + var sortProperty = this.get("sortProperties")[0]; for (var i = 1; i <= this.get("pagesCount"); i++) { - list.push(i); + var pageContent = this._getPaginatedContent(i); + var p1 = pageContent[0].get(sortProperty); + var runningLabel = ""; + if (sortProperty == "title") { + p1 = p1[0]; + } else if (sortProperty == "imdbRating") { + p1 = Math.round(p1); + } else if (sortProperty == "imdbVotes") { + p1 = Math.floor(p1 / 1000) + "k"; + } else if (sortProperty == "release") { + p1 = parseInt(p1.slice(0,4)); + p1 = Math.floor(p1 / 10) * 10; + } else { + prev = p1; + } + if (p1 != prev) { + runningLabel = "" + p1; + prev = p1; + } + list.push({number: i, runningLabel: runningLabel}); } return list; - }.property('page','perPage'), + }.property('page','perPage','sortProperties'), paginatedContent: function() { var page = this.get('page'); - var perPage = parseInt(this.get('perPage')); - var start = (page - 1 ) * perPage; - var end = page * perPage; - return this.get('arrangedContent').slice(start, end) + return this._getPaginatedContent(page); }.property('arrangedContent.[]', 'page', 'perPage'), pagesCount: function() { return Math.ceil(this.get("content.length") / parseInt(this.get("perPage"))) }.property('content.[]', 'perPage'), + _getPaginatedContent: function(page) { + var perPage = parseInt(this.get('perPage')); + var start = (page - 1 ) * perPage; + var end = page * perPage; + return this.get('arrangedContent').slice(start, end) + }, actions: { sortBy: function(property) { this.set('sortProperties', [property]); @@ -80,6 +105,7 @@ App.IndexController = Ember.ArrayController.extend({ } }); + Ember.Handlebars.helper('filelink', function(value, options) { var escaped = encodeURIComponent(value); return new Ember.Handlebars.SafeString('' + value + ''); -- cgit v1.2.1