aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjpl2016-11-30 18:01:21 +0100
committerjpl2016-11-30 18:02:18 +0100
commitf4f479b82e8a3a1c24aef9d0a4cd0bcd61609817 (patch)
tree6683ae3222b98a27327615ea2e9cc37cd2ad02d3
parentadee9a89a50bad9199e1c3dc4b3b355844439d2e (diff)
downloadchouette-core-f4f479b82e8a3a1c24aef9d0a4cd0bcd61609817.tar.bz2
Refs #2015: adding sortable columns on table builder, actually for ref_lines
-rw-r--r--app/controllers/referential_lines_controller.rb18
-rw-r--r--app/helpers/newfront_helper.rb15
2 files changed, 30 insertions, 3 deletions
diff --git a/app/controllers/referential_lines_controller.rb b/app/controllers/referential_lines_controller.rb
index 04581bdda..efbc856df 100644
--- a/app/controllers/referential_lines_controller.rb
+++ b/app/controllers/referential_lines_controller.rb
@@ -77,10 +77,26 @@ class ReferentialLinesController < ChouetteController
end
@q = referential.lines.search(params[:q])
- @lines ||= @q.result(:distinct => true).order(:number).paginate(:page => params[:page]).includes([:network, :company])
+
+ if sort_column && sort_direction
+ @lines ||= @q.result(:distinct => true).order(sort_column + ' ' + sort_direction).paginate(:page => params[:page]).includes([:network, :company])
+ else
+ @lines ||= @q.result(:distinct => true).order(:number).paginate(:page => params[:page]).includes([:network, :company])
+ end
+
end
private
+
+ def sort_column
+ # params[:sort] || 'number'
+ referential.lines.column_names.include?(params[:sort]) ? params[:sort] : 'number'
+ end
+ def sort_direction
+ # params[:direction] || 'asc'
+ %w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc'
+ end
+
def check_policy
authorize resource
end
diff --git a/app/helpers/newfront_helper.rb b/app/helpers/newfront_helper.rb
index 27dce3af8..806502391 100644
--- a/app/helpers/newfront_helper.rb
+++ b/app/helpers/newfront_helper.rb
@@ -8,7 +8,8 @@ module NewfrontHelper
content_tag :tr do
hcont = []
columns.map do |k, v|
- hcont << content_tag(:th, k.to_s.titleize)
+ # hcont << content_tag(:th, k.to_s.titleize)
+ hcont << content_tag(:th, sortable_columns(collection, k))
end
hcont << content_tag(:th, 'Actions', class: 'text-center') if actions.any?
@@ -91,12 +92,22 @@ module NewfrontHelper
end
+ def sortable_columns collection, key
+ direction = (key == params[:sort] && params[:direction] == 'desc') ? 'asc' : 'desc'
+
+ icon = 'sort-desc' if direction == 'asc'
+ icon = 'sort-asc' if direction == 'desc'
+
+ link_to({sort: key, direction: direction}) do
+ pic = content_tag :span, '', class: "fa fa-#{icon}", style: 'margin-left:5px'
+ (key.to_s.titleize + pic).html_safe
+ end
+ end
# Replacement message
def replacement_msg text
content_tag :div, '', class: 'alert alert-warning' do
icon = content_tag :span, '', class: 'fa fa-lg fa-info-circle', style: 'margin-right:7px;'
-
icon + text
end
end