Add sorting to web_links

This commit is contained in:
Christophe Vilayphiou 2012-04-27 14:20:09 +08:00
parent feb2156fd3
commit bf2c89da54
9 changed files with 186 additions and 75 deletions

View File

@ -1,6 +1,8 @@
<tr id="<%= dom_id bulletin %>" class="with_action"> <tr id="<%= dom_id bulletin %>" class="with_action">
<td><%= check_box_tag 'to_delete[]', bulletin.id, false, :class => "checkbox_in_list" %></td> <td><%= check_box_tag 'to_delete[]', bulletin.id, false, :class => "checkbox_in_list" %></td>
<td> <td>
<div class="label-group">
<div class="label-td">
<% if bulletin.is_top? %> <% if bulletin.is_top? %>
<span class="label label-success"><%= t(:top) %></span> <span class="label label-success"><%= t(:top) %></span>
<% end %> <% end %>
@ -19,6 +21,8 @@
<% if bulletin.is_rejected? %> <% if bulletin.is_rejected? %>
<span class="label"><%= t(:rejected) %></span> <span class="label"><%= t(:rejected) %></span>
<% end %> <% end %>
</div>
</div>
</td> </td>
<td><%= bulletin.bulletin_category.i18n_variable[I18n.locale] %></td> <td><%= bulletin.bulletin_category.i18n_variable[I18n.locale] %></td>
<td> <td>

View File

@ -7,14 +7,32 @@ class Panel::WebResource::BackEnd::WebLinksController < OrbitBackendController
get_categorys(params[:web_link_category_id]) get_categorys(params[:web_link_category_id])
@filter = params[:filter]
new_filter = params[:new_filter]
if @filter && params[:clear]
@filter.delete(params[:type])
elsif @filter && new_filter
if @filter.has_key?(new_filter[:type]) && @filter[new_filter[:type]].include?(new_filter[:id].to_s)
@filter[new_filter[:type]].delete(new_filter[:id].to_s)
elsif @filter.has_key?(new_filter[:type])
@filter[new_filter[:type]] << new_filter[:id].to_s
else
@filter.merge!({new_filter[:type] => [new_filter[:id].to_s]})
end
elsif new_filter
@filter = {new_filter[:type] => [new_filter[:id].to_s]}
end
# @web_links = WebLink.search(params[:category_id]) # @web_links = WebLink.search(params[:category_id])
@web_links = WebLink.all.page(params[:page]).per(10) @web_links = (params[:sort] || @filter) ? get_sorted_and_filtered_web_links : WebLink.all.page(params[:page]).per(10)
get_tags get_tags
respond_to do |format| respond_to do |format|
format.html # index.html.erb format.html # index.html.erb
format.xml { render :xml => @web_links } format.xml { render :xml => @web_links }
format.js
end end
end end
@ -116,4 +134,74 @@ class Panel::WebResource::BackEnd::WebLinksController < OrbitBackendController
@tags = Tag.all(:conditions => {:module_app_id => module_app.id}) @tags = Tag.all(:conditions => {:module_app_id => module_app.id})
end end
def get_sorted_and_filtered_web_links
web_links = WebLink.all
case params[:sort]
when 'category'
category_ids = web_links.distinct(:web_link_category_id)
categories = WebLinkCategory.find(category_ids) rescue nil
if categories
h = Hash.new
categories.each { |category| h[category.i18n_variable[I18n.locale]] = category.id }
sorted = params[:direction].eql?('asc') ? h.sort : h.sort.reverse!
sorted_categorys = sorted.collect {|a| web_links.where(:web_link_category_id => a[1]).entries }
web_links = sorted_categorys.flatten!
end
when 'name'
h = Array.new
web_links.each { |web_link| h << [web_link.name[I18n.locale].downcase, web_link] }
sorted = params[:direction].eql?('asc') ? h.sort : h.sort.reverse!
web_links = sorted.collect {|a| a[1] }
when 'status'
web_links = web_links.order_by(:is_top, params[:direction]).order_by(:is_hot, params[:direction]).order_by(:is_hidden, params[:direction]).order_by(:is_pending, params[:direction]).order_by(:is_checked, params[:direction]).order_by(:is_rejected, params[:direction])
when 'tags'
a = Array.new
WebResourceTag.all.order_by(I18n.locale, params[:direction]).each { |tag| a << tag.web_links }
a.flatten!
a.uniq!
tmp = Array.new
web_links.where(:tag_ids => []).each { |web_link| tmp << [web_link.name[I18n.locale].downcase, web_link] }
sorted = params[:direction].eql?('asc') ? tmp.sort : tmp.sort.reverse!
sorted_names = sorted.collect {|a| a[1] }
a = params[:direction].eql?('asc') ? (sorted_names + a) : (a + sorted_names)
web_links = a.flatten
end
# if @filter
# @filter.each do |key, value|
# case key
# when 'status'
# a = Array.new
# web_links.each do |web_link|
# value.each do |v|
# case v
# when 'pending'
# a << web_link if web_link.is_checked.nil?
# when 'rejected'
# a << web_link if web_link.is_checked.eql?(false)
# else
# a << web_link if web_link[v]
# end
# end
# end
# web_links = a.uniq
# when 'categories'
# a = Array.new
# web_links.each do |web_link|
# a << web_link if value.include?(web_link.web_link_category.id.to_s)
# end
# web_links = a.uniq
# when 'tags'
# a = Array.new
# web_links.each do |web_link|
# web_link.tags.each do |tag|
# a << web_link if value.include?(tag.id.to_s)
# end
# end
# web_links = a.uniq
# end if value.size > 0
# end
# end
Kaminari.paginate_array(web_links).page(params[:page]).per(10)
end
end end

View File

@ -62,6 +62,10 @@ class WebLink
@text ||= I18nVariable.first(:conditions => {:key => 'text', :language_value_id => self.id, :language_value_type => self.class}) rescue nil @text ||= I18nVariable.first(:conditions => {:key => 'text', :language_value_id => self.id, :language_value_type => self.class}) rescue nil
end end
def sorted_tags
tags.order_by(I18n.locale, :asc)
end
protected protected
def set_key def set_key

View File

@ -0,0 +1,7 @@
<div id="filter" class="subnav">
<div class="filters">
<div id="sort_headers" class="table-label">
<%= render 'sort_headers' %>
</div>
</div>
</div>

View File

@ -0,0 +1,18 @@
<table class="table main-list">
<thead>
<tr class="sort-header">
<th class="sort span1 <%= is_sort_active?('status') %>">
<%= link_to (t('web_link.status') + content_tag(:b, nil, :class => is_sort?('status'))).html_safe, panel_web_resource_back_end_web_links_path({:filter => @filter}.merge(sortable('status'))), :class => 'js_history' %>
</th>
<th class="sort span1 <%= is_sort_active?('category') %>">
<%= link_to (t('web_link.category') + content_tag(:b, nil, :class => is_sort?('category'))).html_safe, panel_web_resource_back_end_web_links_path({:filter => @filter}.merge(sortable('category'))), :class => 'js_history' %>
</th>
<th class="sort span3 <%= is_sort_active?('name') %>">
<%= link_to (t('web_link.name') + content_tag(:b, nil, :class => is_sort?('name'))).html_safe, panel_web_resource_back_end_web_links_path({:filter => @filter}.merge(sortable('name'))), :class => 'js_history' %>
</th>
<th class="sort span2 <%= is_sort_active?('tags') %>">
<%= link_to (t('web_link.tags') + content_tag(:b, nil, :class => is_sort?('tags'))).html_safe, panel_web_resource_back_end_web_links_path({:filter => @filter}.merge(sortable('tags'))), :class => 'js_history' %>
</th>
</tr>
</thead>
</table>

View File

@ -0,0 +1,37 @@
<tr id="<%= dom_id web_link %>" class="with_action">
<td>
<div class="label-group">
<div class="label-td">
<% if web_link.is_top? %>
<span class="label label-success"><%= t(:top) %></span>
<% end %>
<% if web_link.is_hot? %>
<span class="label label-important"><%= t(:hot) %></span>
<% end %>
<% if web_link.is_hidden? %>
<span class="label"><%= t(:hidden) %></span>
<% end %>
</div>
</div>
</td>
<td><%= web_link.web_link_category.i18n_variable[I18n.locale] %></td>
<td>
<%= link_to web_link.name[I18n.locale], panel_web_resource_back_end_web_link_path(web_link) %>
<div class="quick-edit">
<ul class="nav nav-pills hide">
<li><%= link_to t('web_link.edit'), edit_panel_web_resource_back_end_web_link_path(web_link) %></li>
<li><%= link_to t('web_link.delete'), panel_web_resource_back_end_web_link_path(web_link), :confirm => t('sure?'), :method => :delete, :remote => true %></li>
</ul>
</div>
</td>
<td>
<div class="label-group">
<div class="label-td">
<% web_link.sorted_tags.each do |tag| %>
<span class="label label-tags"><%= tag[I18n.locale] %></span>
<% end %>
</div>
</div>
</td>
</tr>

View File

@ -1,30 +0,0 @@
<tr id="<%= dom_id post %>" class="with_action">
<td>
<% if post.is_top? %>
<span class="label label-success"><%= t(:top) %></span>
<% end %>
<% if post.is_hot? %>
<span class="label label-important"><%= t(:hot) %></span>
<% end %>
<% if post.is_hidden? %>
<span class="label"><%= t(:hidden) %></span>
<% end %>
</td>
<td><%= post.web_link_category.i18n_variable[I18n.locale] %></td>
<td>
<%= link_to post.name[I18n.locale], panel_web_resource_back_end_web_link_path(post) %>
<div class="quick-edit">
<ul class="nav nav-pills hide">
<li><%= link_to t('web_link.edit'), edit_panel_web_resource_back_end_web_link_path(post) %></li>
<li><%= link_to t('web_link.delete'), panel_web_resource_back_end_web_link_path(post), :confirm => t('sure?'), :method => :delete, :remote => true %></li>
</ul>
</div>
</td>
<td>
<% post.tags.each do |tag| %>
<span class="label label-tags"><%= tag[I18n.locale] %></span>
<% end %>
</td>
</tr>

View File

@ -1,21 +1,4 @@
<%= render 'filter' %>
<%= flash_messages %>
<div id="filter" class="subnav">
<div class="filters">
<div id="sort_headers" class="table-label">
<table class="table main-list">
<thead>
<tr class="sort-header">
<th class="sort span1"><%= t('web_link.status') %></th>
<th class="sort span1"><%= t('web_link.category') %></th>
<th class="sort span3"><%= t('web_link.name') %></th>
<th class="sort span2"><%= t('web_link.tags') %></th>
</tr>
</thead>
</table>
</div>
</div>
</div>
<table class="table main-list"> <table class="table main-list">
<thead> <thead>
<tr> <tr>
@ -25,14 +8,11 @@
<th class="span2"></th> <th class="span2"></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody id="tbody_web_links" class="sort-holder">
<%= render :partial => 'web_link', :collection => @web_links %>
<% @web_links.each do |post| %>
<%= render :partial => 'web_links', :locals => {:post => post} %>
<% end %>
</tbody> </tbody>
</table> </table>
<%= paginate @web_links %> <div id="web_link_pagination">
<%= paginate @web_links, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil} %>
</div>

View File

@ -0,0 +1,3 @@
$("#sort_headers").html("<%= j render 'sort_headers' %>");
$("#tbody_web_links").html("<%= j render :partial => 'web_link', :collection => @web_links %>");
$("#web_link_pagination").html("<%= j paginate @web_links, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil} %>");