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
@tags = @module_app.tags
@categories = @module_app.categories.enabled
@filter_fields = {
: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 []
@filter_fields = filter_fields(@categories, @tags)
@table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified]
@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)
@table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified]
render :layout => false
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'}
if request.xhr?
render :partial => "index"
end
@sort
end
def new

View File

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

View File

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