17
17

More than 5 years have passed since last update.

ThinReportsで複数レイアウトを使って一覧表を作成する

Last updated at Posted at 2014-04-17

ThinReportsで、reportの作成方法やpageの作成方法はいくつも書き方があるので、
自分の使った書き方をメモ。

  • 同一ファイル内に複数レイアウトを使用
  • 一覧表を使用
  • 一覧表のフッタに合計を表示
user_contents = { user_name: 'ユーザー名', user_address: 'sample@example.com' }

report = ThinReports::Report.create do |r|
  # ユーザーページレイアウト
  r.start_new_page :layout => File.join(Rails.root, 'app', 'reports', 'user.tlf') do |page|
    page.values(user_contents)
  end

  # 本一覧ページレイアウト(表id: details)
  r.start_new_page :layout => File.join(Rails.root, 'app', 'reports', 'books.tlf') do |page|

    # フッタ作成時の処理
    page.layout.config.list(:details) do
      use_stores :total_cost => 0
      events.on :footer_insert do |e|
        e.section.item(:total_cost).value(total_cost)
      end
    end

    # リスト作成処理
    @books.each do |book|
      book_contents = {title: book.title, cost: book.cost}

      page.list(:details).add_row(book_contents)
      page.list(:details).store.total_cost += cost
    end
  end
end

send_data report.generate,
  filename: 'report.pdf',
  type: 'application/pdf',
  disposition: 'attachment'
17
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
17