Top > 開発ガイド > コンポーネント開発Tips集 > 印刷ページ

印刷ページ

対象バージョン

当ドキュメントはRubricks-0.6.x向けです。

概要

印刷用ページを出力するための方法を記述します。

実装サンプル

メインページ

<script type="text/javascript">
  rubricks.sample = {
    print: function() {
      if(!Element.hasClassName('print_button', 'disabled')) {
        rubricks.common.fire_submit('operationbar_button_print');
        rubricks.common.rubricks_wait_message_close();
      }
    },
    print_target_clear: function() {
      $('samplet_print_id').value = '';
      if(!Element.hasClassName('print_button', 'disabled')) {
        Element.addClassName('print_button', 'disabled');
      }
    },
    print_target_set: function(id) {
      $('samplet_print_id').value = id;
      Element.removeClassName('print_button', 'disabled');
    }
  };
</script>
<% render_operation_bar do %>
  <%= form_tag({:controller => '/sample/index', :action => 'print', :print_action => 'print_content'}, {:id => 'operationbar_button_print', :class => 'operationbar_button_right_inactive swap',  :target => "_blank"}) %>
    <input type="hidden" id="samplet_print_id" name="id" value="" />
    <span id="print_button" class="buttons disabled darkicon_print textcolor_white" onclick="rubricks.sample.print();"><%= hl('[_Common_Print]') %></span>
  </form>
<% end %>

オペレーションバー上に印刷ボタンを配置します。ボタンクリック時に実行するJavaScript関数等を定義します。

ボタンは「rubricks.sample.print_target_set([ActiveRecordのID])」が発行されると押下可能になります。記事を詳細表示する際に同メソッドを発行して下さい。

Controller

def print_content
  @sample_model = Sample::SampleModel.find(params[:id])
  render :inline => %Q|ID:#{@sample_model.id}<br>Name:#{@sample_model.name}|
end

印刷用ページを出力するためのアクションを定義します

View

<table>
  <tr>
    <td>Column1</td>
    <td><%= hl(@sample_model.c1) %></td>
  </tr>
  <tr>
    <td>Column2</td>
    <td><%= hl(@sample_model.c2) %></td>
  </tr>
<table>

印刷用ページとして出力する内容を記述します。 また、印刷用ページと通常ページのrhtmlを共用する場合などは、 印刷用ページであることを判定するための 「print?」メソッドを利用すると便利です。