bogdan / datagrid

Gem to create tables grids with sortable columns and filters
MIT License
1.02k stars 115 forks source link

Aggregate Row #245

Closed brendon closed 6 years ago

brendon commented 6 years ago

I was wondering if there's any capability or workarounds in datagrid for an aggregate row at the bottom of the table? For example, totalling the price in a column of each row on the last row. :)

bogdan commented 6 years ago

You can build one yourself with:

%table
  = datagrid_header @grid
  = datagrid_rows @grid
  %tr.total-row
    %td{rowspan: x} Total
    %td= @grid.total_sum

There is no built-in support so far because there is no good API that would support all the cases...

brendon commented 6 years ago

Ah righty! That's a very decent workaround! Thanks for that :D

bricechapuis commented 5 years ago

%table = datagrid_header @grid = datagrid_rows @grid %tr.total-row %td{rowspan: x} Total %td= @grid.total_sum

Hey ! Sorry I'm a total rookie. I don't understand where I should write these lines and where does total_sum come from ?

bogdan commented 5 years ago

Write those lines the template. If you are rookie, use ERB:

<table>
  <%= datagrid_header @grid %>
  <%= datagrid_rows @grid %>
  <tr class="total-row">
    <td rowspan="<%= x %>">Total</td>
    <td>
      <%= @grid.total_sum %>
    </td>
  </tr>
</table>

Define total_sum as a method on a grid:

class MyGrid
  ...

  def total_sum
  end
end