Two jqgrid on the same view
Closed this issue · 2 comments
Hi Juan,
I am using jgrid_for_rails for a new web app, and it is really great. Nice job !
At the beginning, I was only using jqgrid_for_rails to display simple lists of customers, invoices, complaints etc... I was following this tutorial : http://rubydoc.info/gems/jqgrid_for_rails/1.0.1/frames
However, I am facing a problem.
- On my customers list, i added a button called "Customer details" which led me to the "show" action of the customers controller.
- In this new page, I have two tabs. One is concerning the invoices of my customer and the other one is concerning the complatins he made. I would like to display two grids for both.
=> In the customers_helper.rb, i have created two helpers to display invoices and complaints.
=> In the customers_helper.rb, i have created two variables.
@invoices = Invoice.paginate(
:page => params[:page],
:per_page => params[:rows],
:order => order_by_from_params(params),
:conditions => "customer_id = " + (params[:customer_id])
)
@complaints= Complaints.paginate(
:page => params[:page],
:per_page => params[:rows],
:order => order_by_from_params(params),
:conditions => "customer_id = " + (params[:customer_id])
)
=> In my controller, the first problem is the line "render :json => json_for_jqgrid(@invoices, @columns)"
How can I do to use json_for_jqgrid for @invoices and @complaints ?
=> In my customers/show.html.erb, how can I specify to Rails that I want to display @invoices in raw(invoices_jqgrid) and @complaints in raw(complaints_jqgrid) ?
I don't know if it's possible, but as I am trying to find a solution for two weeks now, so I allow myself to contact you.
Do you have any idea about my issue ?
Thank you for your help.
Hi,
First, create the two grids in the view:
<table id=invoices_list></table>
<div id=invoices_pager></div>
<% content_for :head do %>
<%= raw(invoices_jqgrid) %>
<% end %>
<table id=complaints_list></table>
<div id=complaints_pager></div>
<% content_for :head do %>
<%= raw(complaints_jqgrid) %>
<% end %>
Then, you will need to recognize in the controller the grid which is making the request. Change your helpers and modify the url
param for each grid. Something like:
# for the invoices helper
:url => '/invoices?grid=invoices'
# for the complaints helper
:url => '/invoices?grid=complaints'
This will send an extra param to the controller which you will be able to use for retrieving the records from the right table and render them to json. Something like:
def index
case params[:grid]
when 'invoices'
@resources = Invoice.paginate ...
when 'complaints'
@resources = Complaint.paginate ...
end
render :json => json_for_jqgrid(@resources, @columns)
...
end
You will have to do the same for the columns.
This is just to show you how you could do it. Would be better to move this code to a before_filter
to keep the code in the action cleaner.
Another advice. To avoid SQL injection do not use:
# Allows SQL injection
:conditions => "customer_id = " + (params[:customer_id])
Instead, use something like:
# Prevents SQL injection
:conditions => ["customer_id = ?", (params[:customer_id])]
See more on this here:
http://guides.rubyonrails.org/active_record_querying.html#pure-string-conditions
http://guides.rubyonrails.org/security.html#sql-injection
Hope it helps!
Thank you for your quick answer.
I managed to have my two jqgrids, so you helped me a lot !
As I see it, it make two GET requests (show action twice), to fill the jqgrids.
Thx for the sql injection advice too, you rock :)