Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to display in a html.erb template #12

Closed
benkoshy opened this issue Feb 1, 2017 · 5 comments
Closed

how to display in a html.erb template #12

benkoshy opened this issue Feb 1, 2017 · 5 comments

Comments

@benkoshy
Copy link

benkoshy commented Feb 1, 2017

Hi

thx for publishing your gem. how do I actually display the table in an html.erb? I can build it but displaying it? I noticed that the build command - this presumably goes into the controller. but what do I put in view template between these tags: <% %> to make the pivot table display?

rgds

Ben

@edjames
Copy link
Owner

edjames commented Feb 1, 2017

Hi Ben,

Using the example from the README, in your erb template you could do this:

<% g.columns.each do |col| %>
  <th><%= col.header %></th>
<% end %>

For the row data, when you build the grid object it would depend on whether you specify a pivot field or not. This can make things a lot simpler, depending on your use-case. See this README section.

One final note, just to be clear, I'm assuming you are using <%= %> tags rather than <% %> tags in your template (which is what your original post specified). I'm sure you are, but just want to be certain.

The usage section in the README should hopefully be clear enough in showing how to use this gem.

Let mw know if you need any more help.

Ed.

@benkoshy
Copy link
Author

benkoshy commented Feb 15, 2017

Hi thx Ed

Another question;

This is given in the main page:

obj_1 = Order.new(city: 'London',   quarter: 'Q1')
obj_2 = Order.new(city: 'London',   quarter: 'Q2')
obj_3 = Order.new(city: 'London',   quarter: 'Q3')
obj_4 = Order.new(city: 'London',   quarter: 'Q4')
obj_5 = Order.new(city: 'New York', quarter: 'Q1')
obj_6 = Order.new(city: 'New York', quarter: 'Q2')
obj_7 = Order.new(city: 'New York', quarter: 'Q3')
obj_8 = Order.new(city: 'New York', quarter: 'Q4')

data = [ obj_1, obj_2, obj_3, obj_4, obj_5, obj_6, obj_7, obj_8 ]

g = PivotTable::Grid.new do |g|
  g.source_data  = data
  g.column_name  = :quarter
  g.row_name     = :city
  g.value_name   = :sales
end

Now you will notice the quarter attribute. In my particular case, the quarter attribute is in fact a related child record.

Let me perhaps explain by adapting the above example.

Order

  • :city

Quarter

  • :name

  • :city_id

A city has many quarters. And quarter belongs to a city.

How would I display the name of the quarter in the above pivot table example.?

in other words:

g = PivotTable::Grid.new do |g|
g.source_data = data
g.column_name = :quarter <------ what should I change this to to make it work? I want to display the quarter name here
g.row_name = :city
g.value_name = :sales
end

I hope im making sense?

clarifications much appreciated.

rgds

Ben

@edjames
Copy link
Owner

edjames commented Feb 17, 2017

Hi Ben

If I understand your example correctly I don't think this will work. I'm only handling a single level of object i.e. nested objects are not supported.

However, I think you can work around this by changing the query you are using to generate your result set. So rather than having related objects, you could change the query to return a composite object i.e. include the joined tables in the query result, rather than using ActiveRecord to load associated objects.

Additionally, it sounds like you have a one-to-many relationship between City and Quarter, so I'm not quite sure how want this data displayed. If you could give me a screenshot of what you want I would be happy to help if possible.

Ed.

@benkoshy
Copy link
Author

thank you for your response mr James, I will get back to on the specifics of this problem. I suspect it's a common one, hence my interest in it. rgds, Ben

@edjames
Copy link
Owner

edjames commented Mar 25, 2019

Stale issue, closing...

@edjames edjames closed this as completed Mar 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants