Class RGhost::Grid::Base
In: grid/base_grid.rb
Parent: RGhost::PsObject

It‘s a helper object to create a tabular representation composed of rows, columns and title. This prototype used to join the common attributes for data grids. To use a Grid you must first setup the columns , then load the data. Example:

 grid=Grid::Matrix.new
 grid.column :title => "Code", :width => 1
 grid.column :title => "Name", :width => 3, :align => :center
 grid.column :title => "Date", :width => 3, :align => :center

Note that both the width and align of the last two columns are identical. To avoid this, you can specify a default width passing the values to the DataGrid::Grid object constructor, so any columns that do not specify these values will use them. Example:

 grid=Grid::Matrix.new :width => 3, :align => :center
 grid.column :title => "Code", :width => 1    #:width => 1, :align => :center
 grid.column :title => "Name"                 #:width => 3, :align => :center
 grid.column :title => "Date"                 #:width => 3, :align => :center

The actual content needs to be passed in as an array of arrays

 values=[
 [1,"Name 1", Time.now],
 [2,"Name 2", Time.now],
 [3,"Name 3", Time.now],
 [4,"Name 4", Time.now],
 [5,"Name 5", Time.now]
 ]

Bind the content to the grid:

 grid.data(values)

Add the Grid to a document

 d=Document.new
 d.set grid

Methods

col   column   data   new   ps   style   width  

Included Modules

RGhost::RubyToPs RGhost::Grid::CallbackFacade

Attributes

column_padding  [RW] 
header  [R] 

Public Class methods

Options

  • :headings - Setting the :headings option to false disables the headers.
  • :column_padding - Space between data and column‘s limit.
  • :width - Width for all columns.
  • :align - Align for all data(:left, :center and :right).
  • :title_align - Align for all the header‘s name(:left, :center and :right).

Public Instance methods

Defines properties of a column, uses the same parameters of new. With :format parameters as additional.

  • :format - Format of the data. You can format data in four different ways with Rghost, passing in a Symbol a String a Class or Proc.

:format Parameters type

  • Symbol - Searches for a method defined as Grid::FieldFormat::method_name
 :format => :eurodate
 :format => MyFormat
  • String - Formats using the same parameters used in sprintf
 :format => "%0.2f"
  • Proc - A block. In the example a text limited to 9 characters.
 :format => lambda {|s| s.gsub(/^(.{9}).*$/,'\1...')}

Customizing formats

Let‘s create a class that replaces empty spaces with a double dash.

 class MyFormat < DataGrid::FieldFormat::Custom
  def format
     @value.to_s.gsub(/ /,'--')
  end
 end

Using

 grid.column :title => "Name", :format => MyFormat

Below, the columns with their proper formats.

 grid.column :title => "Code",:format => "(%d)", :width => 1
 grid.column :title => "Name",  :format => MyFormat
 grid.column :title => "Date",  :format => lambda {|date| date.strftime("%d/%m/%Y") }
 values=[
 [1,"Name 1", Time.now],
 [2,"Name 2", Time.now],
 [3,"Name 3", Time.now],
 [4,"Name 4", Time.now],
 [5,"Name 5", Time.now]
 ]
 grid.data(values)

Add the Grid to a document

 d=Document.new
 d.set grid

Defines data to grid processor.

Grid has 3 preset styles :bottom_lines, :border_lines and old_forms. To set any of them, use:

  grid.style(:border_lines)

:border_lines - instance of Grid::Style::BorderLines

:bottom_lines - instance of Grid::Style::BottomLines

:old_forms - instance of Grid::Style::OldForms

[Validate]