我正在使用jqgrid_for_rails gem從數據庫設計表。在我的表格中,很少有列數據來自虛擬屬性。和我的jqgrid軌道loadonce => false(即它不同的單次加載數據與分頁)。如何將數組對象轉換爲ActiveRecord關係對象
在我的搜索功能中,我必須提供searchField搜索的內容,但它直接敲擊數據庫列。所以我想將搜索功能應用到數組中。但不可能,因爲它默認在ActiveRecord模型中搜索。所以我想將我的數組對象轉換爲數據庫ActiveRecord模型。
這怎麼可能?
這裏是我的代碼 - 輔助文件
def order_tracker_jqgrid
options = {:on_document_ready => true, :html_tags => false}
grid = [{
:url => '/order_tracker',
:datatype => 'json',
:loadonce => false,
:mtype => 'GET',
:height => 350,
:altRows => true,
:rownumbers => true,
:colNames => ['#' , 'Order #', 'Tracking #', 'Items', 'Delivery Date', 'Current Status', 'Pickup Location', 'Created By', 'Retailer'],
:colModel => [
{ :name => 'id', :index => 'id', :align => 'center', :search => false, :width => 100, :hidden => true},
{ :name => 'order_number', :index => 'order_number', :classes => 'order-number', :width => 100, :searchoptions => { :sopt => ['eq', 'cn']}},
{ :name => 'tracking_number', :index => 'tracking_number', :width => 110, :classes => 'tracking-number', :searchoptions => { :sopt => ['eq', 'cn']}},
{ :name => 'item_count', :index => 'item_count', :align => 'center' , :width => 80, :search => false},
{ :name => 'delivery_date_with_slot', :index => 'delivery_date_with_slot', :width => 200, :search => false},
{ :name => 'order_state', :index => 'order_state', :search => false},
{ :name => 'pickup_location_data', :index => 'pickup_location_data', :jsonmap => 'pickup_location_data', :search => false},
{ :name => 'creator_name', :index => 'creator_name', :search => false},
{ :name => 'company_name', :index => 'company_name', :search => false}
],
:pager => '#grid-pager',
:rowNum => 10,
:rowList => [10, 20, 30],
:sortname => 'id',
:sortorder => 'asc',
:viewrecords => true,
:autowidth => true,
:loadComplete => "function(){var table = this;setTimeout(function(){updatePagerIcons(table);}, 0);}".to_json_var,
:gridComplete => "function(){
var ids = jQuery('#grid-table').jqGrid('getRowData');
for(var i = 0; i < ids.length; i++)
{
var id = ids[i].id;
var o_number = ids[i].order_number;
var t_number = ids[i].tracking_number;
var order_link = '<a href=\"/orders/'+id+'/show_incomming_order\" class=\"details fancybox fancybox.ajax\">'+o_number+'</a>';
var tracker_link = '<a href=\"/order/'+t_number+'\" class=\"fancybox fancybox.ajax\">'+t_number+'</a>';
$('#grid-table tr#'+id).find('.order-number').html(order_link);
$('#grid-table tr#'+id).find('.tracking-number').html(tracker_link);
}
}".to_json_var
}]
pager = [:navGrid, "#grid-pager", {:del => false, :add => false, :edit => false, :searchicon => 'icon-search orange', :refreshicon => 'icon-refresh blue'}, {:closeAfterEdit => true, :closeOnEscape => true}, {}, {}, {}, {}]
jqgrid_api 'grid-table', grid, pager, options
末
在我的控制器
#If current user is fulflmnt admin
@orders = OrderBucket.orders_from_cate2_3.where(search_params).order("created_at desc") #("created_at >= ? AND created_at <= ?", Time.zone.now.beginning_of_month, Time.zone.now.end_of_month).order("created_at desc")
@columns = ['id' , 'order_number' , 'tracking_number', 'item_count', 'delivery_date_with_slot', 'order_state', 'pickup_location_data', 'creator_name', 'company_name']
if params[:_search] == "true"
@orders = @orders.search(params[:_search],params[:searchField],params[:searchString], params[:page],params[:rows],params[:searchOper],params[:sord])
else
sort_params = params[:sidx]
if sort_params == "order_number"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.order_number}.flatten : @orders.sort_by{|p| p.order_number}.reverse.flatten
elsif sort_params == "tracking_number"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.tracking_number}.flatten : @orders.sort_by{|p| p.tracking_number}.reverse.flatten
elsif sort_params == "pickup_location_data"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.pickup_location_data}.flatten : @orders.sort_by{|p| p.pickup_location_data}.reverse.flatten
elsif sort_params == "order_state"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.order_state}.flatten : @orders.sort_by{|p| p.order_state}.reverse.flatten
elsif sort_params == "creator_name"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.creator_name}.flatten : @orders.sort_by{|p| p.creator_name}.reverse.flatten
elsif sort_params == "company_name"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.company_name}.flatten : @orders.sort_by{|p| p.company_name}.reverse.flatten
elsif sort_params == "delivery_date_with_slot"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.delivery_date_with_slot}.flatten : @orders.sort_by{|p| p.delivery_date_with_slot}.reverse.flatten
elsif sort_params == "item_count"
@orders = params[:sord] == "asc" ? @orders.sort_by{|p| p.item_count}.flatten : @orders.sort_by{|p| p.item_count}.reverse.flatten
end
@orders = @orders.paginate(
:page => params[:page],
:per_page => params[:rows],
:order => order_by_from_params(params))
end
if request.xhr?
render :json => json_for_jqgrid(@orders, @columns)
end
,並在我的模型
搜索方法找到jqGrid的順序表數據
高清self.search(_search,searchField,搜索字符串,頁,行,searchOper,SORD)
if searchOper == 'cn' && _search # contains
wildcard_search = "%#{searchString}%"
where("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'nc' && _search # does not contain
wildcard_search = "%#{searchString}%"
where.not("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'bw' && _search # begin with
wildcard_search = "#{searchString}%"
where("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'bn' && _search # does not begin with
wildcard_search = "#{searchString}%"
where.not("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'en' && _search # does not end with
wildcard_search = "%#{searchString}"
where.not("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'ew' && _search # ends with
wildcard_search = "%#{searchString}"
where("#{searchField} LIKE :search", search: wildcard_search).page(page).per_page(rows)
elsif searchOper == 'eq' && _search # equal
where("#{searchField} = :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'ne' && _search # not equal
where.not("#{searchField} = :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'in' && _search # is in
# where("#{searchField} = :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'ni' && _search # is in
# where("#{searchField} = :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'lt' && _search # less
where("#{searchField} < :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'le' && _search # less or equal
where("#{searchField} <= :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'gt' && _search # greater
where("#{searchField} > :search", search: searchString).page(page).per_page(rows)
elsif searchOper == 'ge' && _search # greater or equal
where("#{searchField} >= :search", search: searchString).page(page).per_page(rows)
else
wildcard_search = "%#{searchString}%" # contains
where("name LIKE :search", search: wildcard_search).page(page).per_page(rows)
end
末
顯示你現有的一些代碼w我會幫忙的! – emaillenin
嘿。我更新了代碼。請立即檢查 – Shubhankar