Update index table

This commit is contained in:
manson 2014-05-14 10:58:06 +08:00
parent 2f775f2ee7
commit f4eab719f8
4 changed files with 49 additions and 91 deletions

View File

@ -11,49 +11,16 @@ class Admin::AnnouncementsController < OrbitAdminController
def index def index
@tags = @module_app.tags @tags = @module_app.tags
@categories = @module_app.categories.enabled @categories = @module_app.categories.enabled
@filter_fields = filter_fields(@categories, @tags)
@filter_fields = { @table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified]
:status=>[{:title=>"is_top",:id=>"is_top"},{:title=>"is_hot",:id=>"is_hot"},{:title=>"is_hidden",:id=>"is_hidden"}],
:category=>@categories.map{|c| {:title=>c.title, :id=>c.id}},
:tags=>@tags.map{|tag| {:title=>tag.name, :id=>tag.id}}
}
end
def index_table
status = params[:filters][:status].blank? ? [] : params[:filters][:status] rescue []
categories = params[:filters][:category].blank? ? [] : params[:filters][:category] rescue []
tags = params[:filters][:tags].blank? ? [] : params[:filters][:tags] rescue []
@bulletins = Kaminari.paginate_array( @bulletins = Kaminari.paginate_array(
Bulletin.order_by(sort).with_categories(categories).with_tags(tags).with_status(status) Bulletin.order_by(sort).with_categories(filters("category")).with_tags(filters("tag")).with_status(filters("status"))
).page(params[:page]).per(10) ).page(params[:page]).per(10)
@table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified] if request.xhr?
render :layout => false render :partial => "index"
end
def sort
unless params[:sort].blank?
case params[:sort]
when "status"
@sort = [[:is_top, params[:order]],
[:is_hot, params[:order]],
[:is_hidden,params[:order]]]
when "category"
@sort = {:category_id=>params[:order]}
when "title"
@sort = {:title=>params[:order]}
when "start_date"
@sort = {:postdate=>params[:order]}
when "end_date"
@sort = {:deadline=>params[:order]}
when "last_modified"
@sort = {:update_user_id=>params[:order]}
end
else
@sort = {:created_at=>'desc'}
end end
@sort
end end
def new def new

View File

@ -13,11 +13,11 @@
<div class="accordion-body collapse" id="collapse-<%= field %>"> <div class="accordion-body collapse" id="collapse-<%= field %>">
<div class="accordion-inner pagination-right" data-toggle="buttons-checkbox"> <div class="accordion-inner pagination-right" data-toggle="buttons-checkbox">
<% @filter_fields[field].each do |val| %> <% @filter_fields[field].each do |val| %>
<%= link_to t(val[:title]), "#", :onclick => "addFilter('filters[#{field}][]=#{val[:id]}')", :class => "btn btn-small #{is_filter_active?(field, val[:id])}" %> <%= link_to t(val[:title]), "#", :onclick => "filter.addFilter('filters[#{field}][]=#{val[:id]}')", :class => "btn btn-small #{is_filter_active?(field, val[:id])}" %>
<% end %> <% end %>
</div> </div>
<div class="filter-clear"> <div class="filter-clear">
<a href="" class="btn btn-link btn-small"><i class="icons-cycle"></i> <%= t(:clear) %></a> <a href="#" onclick="filter.clearFilter();" class="btn btn-link btn-small"><i class="icons-cycle"></i> <%= t(:clear) %></a>
</div> </div>
</div> </div>
<% end %> <% end %>
@ -25,65 +25,57 @@
<% end %> <% end %>
<span id="index_table"> <span id="index_table">
<%= render 'index'%>
</span> </span>
<%= render 'layouts/delete_modal', delete_options: @delete_options %> <%= render 'layouts/delete_modal', delete_options: @delete_options %>
<script type="text/javascript"> <script type="text/javascript">
filters=[]; var Filter = function(dom){
table_url=""; var makeFilters = function(){
return (window.location.search ? window.location.search.replace('?','').split('&') : []);
$(function(){
init();
update_table();
});
var init = function(){
filters = window.location.search.replace('?','').split('&');
table_url = "";
if(window.location.search==""){
filters = [];
table_url = window.location.pathname+'/index_table';
}else{
table_url = window.location.pathname+'/index_table';
} }
}; var filters = makeFilters(),
dom = $(dom),
var update_table = function(url){ mainUrl = window.location.pathname;
if(url==null){ var updateTable = function(url){
if(filters.length==0){ xurl = (url == null ? ( filters.length ? mainUrl + "?" + filters.join('&') : mainUrl ) : null);
url = table_url; $.ajax({
}else{ url : xurl,
url = table_url+'?'+filters.join('&'); type : "get",
} dataType : "html"
}).done(function(data){
history.pushState(null, null, decodeURIComponent(xurl));
filters = makeFilters();
dom.html(data);
$(".pagination a").click(function(){
updateTable($(this).attr('href'));
return false;
});
})
} }
this.addFilter = function(filter){
$.get(url,function(data){ $.each(filters,function(idx,data){
history.pushState(null, null, decodeURIComponent(url.replace('/index_table','')) ); if(data.indexOf("page=")>-1) filters.splice(idx,1);
init();
$("#index_table").html(data);
$(".pagination a").click(function(){
update_table($(this).attr('href'));
return false;
}); });
});
};
var addFilter = function(filter){ if( (index = filters.indexOf(filter) ) > -1){
url = ""; mainUrl = mainUrl.replace(filter,'');
$.each(filters,function(idx,data){ filters.splice(index,1);
if(data.indexOf("page=")>-1) filters.splice(idx,1); }else{
}); filters.push(filter);
}
if( (index = filters.indexOf(filter) ) > -1){ updateTable();
table_url = table_url.replace(filter,''); return false;
filters.splice(index,1); };
}else{
filters.push(filter); this.clearFilter = function(){
$(".filter-group a.active").removeClass("active");
filters = [];
updateTable();
return false;
} }
}
update_table(); var filter = new Filter("#index_table");
return false;
};
</script> </script>

View File

@ -4,7 +4,6 @@ Rails.application.routes.draw do
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do
namespace :admin do namespace :admin do
get 'announcements/index_table' => 'announcements#index_table'
resources :announcements resources :announcements
end end