From 22501d710ca2afe8bc0b681920c1cabd578b42c2 Mon Sep 17 00:00:00 2001 From: Lewis Marshall Date: Mon, 15 Oct 2012 15:48:27 +0100 Subject: [PATCH] Add ability to order fields Asc / Desc --- app.rb | 58 ++++++++++++++++++++++++++++--------------------- public/app.css | 16 ++++++++++++++ public/app.js | 45 ++++++++++++++++++++++++++++++++++++-- views/index.erb | 21 ++++++++++++++++++ 4 files changed, 113 insertions(+), 27 deletions(-) diff --git a/app.rb b/app.rb index 585661d..9264717 100644 --- a/app.rb +++ b/app.rb @@ -44,30 +44,38 @@ get '/data' do content_type :json - case params['data_type'] - when 'Accounts' - 50.times.map { - { - :first_name => Faker::Name.first_name, - :last_name => Faker::Name.last_name, - :email_address => Faker::Internet.email - } - }.to_json - when 'Reports' - 50.times.map { - { - :name => Faker::Lorem.sentence, - :published_at => (rand 365).days.ago, - :featured => rand(5) != 0 - } - }.to_json - when 'Blog Posts' - 50.times.map { - { - :title => Faker::Lorem.sentence, - :published_at => (rand 365).days.ago, - :no_of_views => rand(10000) - } - }.to_json + data = case params['data_type'] + when 'Accounts' + 50.times.map { + { + :first_name => Faker::Name.first_name, + :last_name => Faker::Name.last_name, + :email_address => Faker::Internet.email + } + } + when 'Reports' + 50.times.map { + { + :name => Faker::Lorem.sentence, + :published_at => (rand 365).days.ago, + :featured => rand(5) != 0 + } + } + when 'Blog Posts' + 50.times.map { + { + :title => Faker::Lorem.sentence, + :published_at => (rand 365).days.ago, + :no_of_views => rand(10000) + } + } + end + + if params['filters'] && order = params['filters']['order'] + attribute, direction = order.split(',') + data = data.sort_by { |e| e[attribute.to_sym] } + data = data.reverse if direction == 'Desc' end + + data.to_json end diff --git a/public/app.css b/public/app.css index 190fb18..2372db9 100644 --- a/public/app.css +++ b/public/app.css @@ -2,3 +2,19 @@ float: left; width: 200px; } + +table th { + cursor: pointer; +} + +.modal-body h4 { + float: left; + width: 200px; + line-height: 30px; + margin-top: 0; +} + +.modal-body .btn-group { + float: left; + width: 300px; +} diff --git a/public/app.js b/public/app.js index 2a1e7ea..f60e676 100644 --- a/public/app.js +++ b/public/app.js @@ -51,11 +51,17 @@ DataColumnView = Backbone.View.extend({ this.$el.text(this.model.get('name')); return this; }, + events: { + click: 'viewFilters' + }, toggleVisible: function() { if(this.model.get('visible')) $('table thead tr').append(this.render().el); else this.remove(); + }, + viewFilters: function() { + (new DataFilterView({ model: this.model })).render(); } }); @@ -63,8 +69,39 @@ DataCollection = new (Backbone.Collection.extend({ url: "/data" })); +DataFilterView = Backbone.View.extend({ + template: function(attributes) { + return _.template($('#data-filter-template').html(), attributes); + }, + render: function() { + $('body').append(this.$el.html(this.template(this.model.toJSON()))); + this.$el.modal(); + return this; + }, + events: { + 'click .close': 'close', + 'click .apply': 'apply' + }, + close: function() { + this.$el.modal('hide'); + this.remove(); + }, + apply: function() { + order_selection = this.$el.find('.btn.active'); + + if(order_selection) { + App.filters.order = this.model.get('attribute') + ',' + order_selection.text(); + } + + App.fetchData(); + + this.close(); + } +}); + App = new (Backbone.View.extend({ initialize: function() { + this.filters = {} DataTypeCollection.bind('reset', this.renderDataTypes, this); DataFieldCollection.bind('reset', this.renderDataFields, this); DataCollection.bind('reset', this.renderData, this); @@ -96,8 +133,12 @@ App = new (Backbone.View.extend({ Button.set('visible', DataFieldCollection.where({ visible: true }).length > 0); }, fetchData: function() { - console.log('fetching data'); - DataCollection.fetch({ data: { data_type: App.data_type.get('name') } }); + DataCollection.fetch({ + data: { + data_type: App.data_type.get('name'), + filters: App.filters + } + }); }, renderData: function() { tbody = $('table tbody'); diff --git a/views/index.erb b/views/index.erb index 7e843fc..7e3c54e 100644 --- a/views/index.erb +++ b/views/index.erb @@ -40,5 +40,26 @@ + +