leikind / wice_grid

A Rails grid plugin to create grids with sorting, pagination, and (automatically generated) filters
MIT License
537 stars 215 forks source link

Totals row #138

Closed gfaraj closed 10 years ago

gfaraj commented 10 years ago

I would like to add a row at the end that shows a total for one or more columns. Is that possible?

leikind commented 10 years ago
g.last_row do |rec|
  # return your html as a string
end

https://github.com/leikind/wice_grid/blob/rails3/lib/wice/grid_renderer.rb#L419

gfaraj commented 10 years ago

Was wondering if there was something that did it automatically, but that works. Thanks!

gfaraj commented 10 years ago

Wait... how would I retrieve the values to sum for a total?

leikind commented 10 years ago

how about using a variable in the block?

leikind commented 10 years ago

something like this

<% sum = 0%>
<% grid() do |g|

  g.column do |rec|
    sum += rec.something

    rec.whatever
  end

  g.last_row do |rec|
    '<tr><td>' + sum.to_s + '</td></tr>'
   end
end %>
gfaraj commented 10 years ago

Oh, yeah that's a good idea. Thanks.