I use jqgrid in my rails to show data in my web
and I want to decide which data to show depends on which page the user is surfing
here's the code
show.html.erb
<%=raw jqgrid("people_list", "people", "/people",
[ { :field => "pid", :label => "pid" ,:editable => true},
{ :field => "name", :label => "name" ,:editable => true},
{ :field => "gender", :label => "gender" ,:editable => true},
{ :field => "birthday", :label => "birthday" ,:editable => true}
] ,
{:autowidth=>true,:add => true,:edit => true,:delete => true,:view => true,:edit_url => post_data_people_url,:sortable_rows => 'true'
}
)%>
people_controller.rb
def index
index_columns ||= [:pid,:name,:gender,:birthday,:school]
current_page = params[:page] ? params[:page].to_i : 1
rows_per_page = params[:rows] ? params[:rows].to_i : 10
conditions={:page => current_page, :per_page => rows_per_page}
conditions[:order] = params["sidx"] + " " + params["sord"] unless (params[:sidx].blank? || params[:sord].blank?)
if params[:_search] == "true"
conditions[:conditions]=filter_by_conditions(index_columns)
end
#temp = Person.limit(0).all
KlassesPeople.where(:klass_id => 1).each do |stu|########
#temp.concat( Person.where(:id => stu.person_id) )
end
#people = #temp.paginate(conditions)
total_entries=#people.total_entries
respond_with(#people) do |format|
format.html
format.json { render :json => #people.to_jqgrid_json(index_columns, current_page, rows_per_page, total_entries)}
end
end
I want to pass the value of klass id from the jqgrid to the controller to determine the value of klass_id in the line with "########"
any idea?
I don't know how to format the code for you but you can use the postData parameter to pass a value to your controler every time the jqGrid would get data.
postData: { KeyName: KeyValue },
If you need to set this value dynamically you can set it via something like:
$('#gridName').jqGrid('setGridParam', { postData: { KeyName: KeyValue });
Related
i have two fields in my model and i want add them in Soulmate::Loader:
for example, my "person" model has name and email field. and i want load then in Soulmate:
loader = Soulmate::Loader.new("people")
loader.add("term" => name, "id" => self.id, "data" => {
"link" => Rails.application.routes.url_helpers.person_path(self)
})
i want add name and email in loader.add. but i can't.
def load_into_soulmate
loader = Soulmate::Loader.new("people")
loader.add("term" =>{ name , email }, "id" => self.id, "data" => {
"link" => Rails.application.routes.url_helpers.person_path(self)
})
end
and
def load_into_soulmate
loader = Soulmate::Loader.new("people")
loader.add("term" =>{ "name" => name ,"email" => email }, "id" => self.id, "data" => {
"link" => Rails.application.routes.url_helpers.person_path(self)
})
end
error show when i use Person.find_each(&:save) for add datas to redis:
ArgumentError: ArgumentError
from /var/lib/gems/2.2.0/gems/soulmate-1.1.0/lib/soulmate/loader.rb:31:in `add'
but all is wrong.
I think you have to use aliases.
def load_into_soulmate
loader = Soulmate::Loader.new("people")
loader.add("term" =>"name", "id" => self.id, "aliases" => [email], "data" => {
"link" => Rails.application.routes.url_helpers.person_path(self)
})
end
I'm trying to setup the smart_listing gem in my app. Where and how can I configure the default per page number of pagination results. In the docs of smart_listing is mentioned that it uses kaminari.
If you don't already have a config/initializers/kaminari_config.rb file, run Kaminari's config generator:
bundle exec rails generate kaminari:config
This will create config/initializers/kaminari_config.rb with the default content:
Kaminari.configure do |config|
# config.default_per_page = 25
# config.max_per_page = nil
# config.window = 4
# config.outer_window = 0
# config.left = 0
# config.right = 0
# config.page_method_name = :page
# config.param_name = :page
end
Simply uncomment and edit the configuration options you are interested in changing.
Update:
SmartListing provides it's own configuration options for pagination in config/initializers/smart_listing.rb:
SmartListing.configure do |config|
config.global_options({
#:param_names => { # param names
#:page => :page,
#:per_page => :per_page,
#:sort => :sort,
#},
#:array => false, # controls whether smart list should be using arrays or AR collections
#:max_count => nil, # limit number of rows
#:unlimited_per_page => false, # allow infinite page size
#:paginate => true, # allow pagination
#:memorize_per_page => false, # save per page settings in the cookie
#:page_sizes => DEFAULT_PAGE_SIZES, # set available page sizes array
#:kaminari_options => {:theme => "smart_listing"}, # Kaminari's paginate helper options
})
config.constants :classes, {
#:main => "smart-listing",
#:editable => "editable",
#:content => "content",
#:loading => "loading",
#:status => "smart-listing-status",
#:item_actions => "actions",
#:new_item_placeholder => "new-item-placeholder",
#:new_item_action => "new-item-action",
#:new_item_button => "btn",
#:hidden => "hidden",
#:autoselect => "autoselect",
#:callback => "callback",
#:pagination_per_page => "pagination-per-page text-center",
#:pagination_count => "count",
#:inline_editing => "info",
#:no_records => "no-records",
#:limit => "smart-listing-limit",
#:limit_alert => "smart-listing-limit-alert",
#:controls => "smart-listing-controls",
#:controls_reset => "reset",
#:filtering => "filter",
#:filtering_search => "glyphicon-search",
#:filtering_cancel => "glyphicon-remove",
#:filtering_disabled => "disabled",
#:sortable => "sortable",
#:icon_new => "glyphicon glyphicon-plus",
#:icon_edit => "glyphicon glyphicon-pencil",
#:icon_trash => "glyphicon glyphicon-trash",
#:icon_inactive => "glyphicon glyphicon-circle",
#:icon_show => "glyphicon glyphicon-share-alt",
#:icon_sort_none => "glyphicon glyphicon-resize-vertical",
#:icon_sort_up => "glyphicon glyphicon-chevron-up",
#:icon_sort_down => "glyphicon glyphicon-chevron-down",
}
config.constants :data_attributes, {
#:main => "smart-listing",
#:confirmation => "confirmation",
#:id => "id",
#:href => "href",
#:callback_href => "callback-href",
#:max_count => "max-count",
#:inline_edit_backup => "smart-listing-edit-backup",
#:params => "params",
#:observed => "observed",
#:href => "href",
#:autoshow => "autoshow",
#:popover => "slpopover",
}
config.constants :selectors, {
#:item_action_destroy => "a.destroy",
#:edit_cancel => "button.cancel",
#:row => "tr",
#:head => "thead",
#:filtering_icon => "i"
}
end
Uncomment the page_sizes line and replace DEFAULT_PAGE_SIZES with an array like [10, 20, 50, 100]
No need to comment out page_sizes in initializers/smart_listing.rb.
You can just define per_page value in controller, eg:
users_scope = User.all.includes(:bio)
users_scope = users_scope.like(params[:filter]) if params[:filter]
#users = smart_listing_create :users, users_scope, partial: "users/list", page_sizes: [5, 7, 13, 26]
smart_listing v1.1.2
In my rails application, I have a button which when clicked, copies data from one database and insert it in another.
I am using octopus gem to link my application to 2 databases.
To copy a record from db_A to db_B, I am using the code below:
Octopus.using(:shard_B) do
#book_new_live = Book.create(
:BK_SUB_FK => #book.BK_SUB_FK,
:BK_TITLE => #book.BK_TITLE,
:BK_SOURCE => "",
:BK_PUB => "",
:BK_COVER => "",
:BK_LABEL_PRODUCT => #book.BK_LABEL_PRODUCT,
:BK_FINAL_LABEL => "",
:BK_VISUAL_METHOD => #book.PRB_VISUAL_METHOD,
:BK_DB => "",
:BK_COVERED_REGION => "",
:BK_VERSION_NO => #book.BK_VERSION_NO,
:BK_SEQ_FILE => "",
)do |primary|
primary.BK_ID = #book.BK_ID
end
end
Database 'db_b', to which data in copied, does not accept null values and the columns cannot be null and the default value is 'NONE'.
Also, I am not allowed to modify the structure of the database so that it can accept null values.
If I use the simplified code below, I get an error message which informs me that the columns 'BK_SOURCE', 'BK_PUB', 'BK_COVER'...cannot be null. By default rails is passing null to those columns.
So I have to pass empty strings to the columns which cannot be null.
Octopus.using(:shard_B) do
#book_new_live = Book.create(
:BK_SUB_FK => #book.BK_SUB_FK,
:BK_TITLE => #book.BK_TITLE,
:BK_LABEL_PRODUCT => #book.BK_LABEL_PRODUCT,
:BK_VISUAL_METHOD => #book.PRB_VISUAL_METHOD,
:BK_VERSION_NO => #book.BK_VERSION_NO,
)do |primary|
primary.BK_ID = #book.BK_ID
end
end
Is there a way of preventing rails from passing null values to the columns not mentioned in the above code?
If i understand you correctly, you can just use the or operator to send your values like this:
Octopus.using(:shard_B) do
#book_new_live = Book.create(
:BK_SUB_FK => #book.BK_SUB_FK,
:BK_TITLE => #book.BK_TITLE,
:BK_SOURCE => #book.BK_SOURCE || "NONE",
:BK_PUB => #book.BK_PUB || "NONE",
:BK_COVER => #book.BK_COVER || "NONE",
:BK_LABEL_PRODUCT => #book.BK_LABEL_PRODUCT,
:BK_FINAL_LABEL => "",
:BK_VISUAL_METHOD => #book.PRB_VISUAL_METHOD,
:BK_DB => "",
:BK_COVERED_REGION => "",
:BK_VERSION_NO => #book.BK_VERSION_NO,
:BK_SEQ_FILE => "",
)do |primary|
primary.BK_ID = #book.BK_ID
end
end
By saying that the value to be passed is #book.BK_SOURCE || "NONE", if the attribute is nil, then the string NONE is passed instead.
EDIT
hash = {
:BK_SUB_FK => #book.BK_SUB_FK,
:BK_TITLE => #book.BK_TITLE,
:BK_SOURCE => #book.BK_SOURCE,
:BK_PUB => #book.BK_PUB,
:BK_COVER => #book.BK_COVER,
:BK_LABEL_PRODUCT => #book.BK_LABEL_PRODUCT,
:BK_FINAL_LABEL => #book.BK_FINAL_LABEL,
:BK_VISUAL_METHOD => #book.PRB_VISUAL_METHOD,
:BK_DB => #book.BK_DB,
:BK_VERSION_NO => #book.BK_VERSION_NO
}
hash = hash.delete_if { |k, v| v.nil? }
Octopus.using(:shard_B) do
#book_new_live = Book.create(hash)do |primary|
primary.BK_ID = #book.BK_ID
end
end
Give it a try.
I found that this function (remote_function) was part of the Prototype helper which was removed from the framework with Rails 3.1 and how to change on ajax?
country
%select#country{:name => "country", :onchange => remote_function(:update => 'region_city', :url => { :action => :get_regions_cities_for_country}, :with => 'Form.Element.serialize(this)')}
= options_for_select( Country.find(:all).map {|u| [u.name,u.id]})
%div{:id => "region_city"}
region
%select#region{:name => "region", :onchange => remote_function(:update => 'city', :url => { :action => :get_cities}, :with => 'Form.Element.serialize(this)')}
= options_for_select(Country.find(:first).regions.find(:all).map {|u| [u.name,u.id]})
city
#{select_tag(:city, options_for_select( Country.find(:first).regions.find(:first).cities.find(:all).map {|u| [u.name,u.id]} ))}
Prototype is not in rails anymore. Use jquery-ujs instead. See this other question for inspiration: Ruby on Rails - drop down box on change event
views:
= select_tag(:district,"<option value='0'>#{t('Please select district')}</option>".html_safe+options_from_collection_for_select(District.all, "id", "title"),:'data-remote' => 'true',:'data-url' => url_for(:controller => 'select', :action => 'getdata'),:'data-type' => 'json')
= select_tag(:state,"<option value='0'>#{t('Please select state')}</option>".html_safe,:'data-remote' => 'true',:'data-url' => url_for(:controller => 'select', :action => 'getdata'),:'data-type' => 'json')
= select_tag(:city,"<option value='0'>#{t('Please select city')}</option>".html_safe)
:javascript
$(document).ready(function() {
$('#district').live('ajax:success', function(evt, data, status, xhr) {
var selectbox2 = $('#state');
selectbox2.empty();
var opt = $('<option/>');
opt.attr('value', "0");
opt.text("#{t('Please select state')}");
opt.appendTo(selectbox2);
$.each(data, function(index, value) {
var opt = $('<option/>');
opt.attr('value', value[0]);
opt.text(value[1]);
opt.appendTo(selectbox2);
});
var selectbox3 = $('#city');
selectbox3.empty();
var opt = $('<option/>');
opt.attr('value', "0");
opt.text("#{t('Please select city')}");
opt.appendTo(selectbox3);
});
$('#state').live('ajax:success', function(evt, data, status, xhr) {
var selectbox3 = $('#city');
selectbox3.empty();
$.each(data, function(index, value) {
var opt = $('<option/>');
opt.attr('value', value[0]);
opt.text(value[1]);
opt.appendTo(selectbox3);
});
});
});
controller:
class SelectController < ApplicationController
def getdata
#data_for_select1 = params[:district]
if #data_for_select1
#data_for_select2 = State.where(:district_id => #data_for_select1).all
render :json => #data_for_select2.map{|c| [c.id, c.title]}
else
#data_for_select1 = params[:state]
#data_for_select2 = City.where(:state_id => #data_for_select1).all
render :json => #data_for_select2.map{|c| [c.id, c.name]}
end
end
end
routes:
get 'assets' => 'select#getdata'
note: you must have a localization used phrases, otherwise js does not work
I know how to access foreign key attributes in a scaffold index view. I can simply refer to the attributes using dot notation such as property.que.name. Given the following models:
class Priority < ActiveRecord::Base
belongs_to :que
...
end
class Que < ActiveRecord::Base
has_many :priorities
...
end
In the index view, I can do something like this to get the name value:
<td><%=h priority.que ? priority.que.name : "" %></td>
How do I do this in the jqgrid?
I tried this but the jqgrid comes back empty:
Priorities Controller:
#priorities = Priority.find(:all, :order => "position", :conditions => "multitenant_team_id = " + current_user.team.id.to_s ) do
if params[:_search] == "true"
id =~ "%#{params[:id]}%" if params[:id].present?
issue_id =~ "%#{params[:issue_id]}%" if params[:issue_id].present?
que =~ "%#{params[:que]}%" if params[:que].present?
customer =~ "%#{params[:customer]}%" if params[:customer].present?
title =~ "%#{params[:title]}%" if params[:title].present?
reporting_source =~ "%#{params[:reporting_source]}%" if params[:reporting_source].present?
priority =~ "%#{params[:priority]}%" if params[:priority].present?
product =~ "%#{params[:product]}%" if params[:product].present?
current_owner =~ "%#{params[:current_owner]}%" if params[:current_owner].present?
end
paginate :page => params[:page], :per_page => params[:rows]
order_by "#{params[:sidx]} #{params[:sord]}"
end
if request.xhr?
end
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #priorities.to_jqgrid_json(
[:id, :issue_id, :que.name, :customer, :title, :reporting_source,
:priority, :product, :current_owner],
params[:page], params[:rows], #priorities.total_entries)}
format.xml { render :xml => #priorities }
end
end
Index View:
<%= jqgrid("Priorities", "priorities", "/priorities",
[
{:field => "id", :label => "ID", :width => 35, :resizable => false},
{:field => "issue_id", :label => "Issue Id"},
{:field => "que", :label => "Queue"},
{:field => "customer", :label => "Customer"},
{:field => "title", :label => "Title"},
{:field => "reporting_source", :label => "Reporting Source"},
{:field => "priority", :label => "Priority"},
{:field => "product", :label => "Product"},
{:field => "current_owner", :label => "Current Owner"}
],
{ :rows_per_page => 12, :height => 450 }
)%>
If I specify que instead of que.name, I get the data back in the grid but the Queue field shows a "#" symbol so I suspect the .to_jqgrid_json call doesn't like my syntax.
Has anyone tried this before? I hope so.
I fixed my problem. I ended up changing my find to a find_by_sql so I could do a left outer join on the ques table. I think there were a couple of issues. I think the *to_jqgrid_json* had problems with null foreign key values and I couldn't figure out how to get at the Que.name any other way. I'm using SQLServer so I had to use isnull(ques.name, '') to convert the null to empty space.
So I replaced my find as follows:
#priorities = Priority.find_by_sql ["select priorities.id id, issue_id, isnull(ques.name,' ') queue_name, customer, title, reporting_source, priority, product, current_owner from priorities left outer join ques on priorities.que_id = ques.id where priorities.multitenant_team_id = ? order by issue_id", current_user.team.id.to_s]
This introduced another problem in that find_by_sql returns an array which breaks the #priorities.total_entries call. So I had to replace it with array.count.
format.json { render :json => #priorities.to_jqgrid_json(
[:id, :issue_id, :queue_name, :customer, :title, :reporting_source, :priority, :product, :current_owner],
params[:page], params[:rows], #priorities.count)}
My grid looks great!
Edit
My grid LOOKS great but it doesn't paginate or sort. Back to the drawing board. :(
Okay, I think I fixed it for real this time.
#priorities = Priority.find(:all,
:select => "priorities.id, priorities.issue_id,
priorities.customer, priorities.title,
priorities.reporting_source, priorities.priority,
priorities.product, priorities.current_owner,
priorities.position,
isnull(ques.name,' ') queue_name",
:joins => "LEFT OUTER JOIN ques ON ques.id = priorities.que_id",
:order => "priorities.position",
:conditions => "priorities.multitenant_team_id = " + current_user.team.id.to_s ) do
I had know idea I could specify joins like this. This keeps the resultset in a format the 2dc_jqgrid plugin likes. Sorting, pagination and searching all work now. Now my grid looks good and actually works.