GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Tickit::Widget::Table(3) User Contributed Perl Documentation Tickit::Widget::Table(3)

Tickit::Widget::Table - table widget with support for scrolling/paging

version 0.216

 #!/usr/bin/env perl
 use strict;
 use warnings;
 
 use Tickit;
 use Tickit::Widget::Table;
 
 my $tbl = Tickit::Widget::Table->new;
 $tbl->add_column(
        label => 'Left',
        align => 'left',
        width => 8,
 );
 $tbl->add_column(
        label => 'Second column',
        align => 'centre'
 );
 $tbl->adapter->push([ map [qw(left middle)], 1..100 ]);
 Tickit->new(root => $tbl)->run;

WARNING: This is still a preview release. API might be subject to change in future, please get in contact if you're using this, or wait for version 1.000.

This widget provides a scrollable table implementation for use on larger data sets. Rather than populating the table with values, you provide an adapter which implements the "count" and "get" methods, and the table widget will query the adapter for the current "page" of values.

This abstraction should allow access to larger datasets than would fit in available memory, such as a database table or procedurally-generated data.

See Adapter::Async::OrderedList::Array if your data is stored in a Perl array. Other subclasses may be available if you have a different source.

Apply to:
  • Row
  • Column
  • Cell

Item transformations

This takes the original data item for the row, and returns one of the following:

  • Future - when resolved, the items will be used as cells
  • Arrayref - holds the cells directly

The data item can be anything - an array-backed adapter would return an arrayref, ORM will give you an object for basic collections.

Any number of cells may be returned from a row transformation, but you may get odd results if the cell count is not consistent.

An array adapter needs no row transformation, due to the arrayref behaviour. You could provide a Future alternative:

 $row->apply_transformation(sub {
  my ($item) = @_;
  Future->wrap(
   @$item
  )
 });

For the ORM example, something like this:

 $row->apply_transformation(sub {
  my ($item) = @_;
  Future->wrap(
   map $item->$_, qw(id name created)
  )
 });

Column transformations

Column transformations are used to apply styles and formats.

You get an input value, and return either a string or a Future.

Example date+colour transformation on column:

 $col->apply_transformation(sub {
  my $v = shift;
  Future->wrap(
   String::Tagged->new(strftime '%Y-%m-%d', $v)
   ->apply_tag(0, 4, b => 1)
   ->apply_tag(5, 1, fg => 8)
   ->apply_tag(6, 2, fg => 4)
   ->apply_tag(9, 1, fg => 8)
  );
 });

Cell transformations

Cell transformations are for cases where you need fine control over individual components. They operate similarly to column transformations, taking the input value and returning either a string or a Future.

Typical example would be a spreadsheet:

 $cell->apply_transformation(sub {
  my $v = shift;
  return $v unless blessed $v;
  return eval $v if $v->is_formula;
  return $v->to_string if $v->is_formatted;
  return "$v"
 });

View transformations

This happen every time the row is rendered. They provide the ability to do view-specific modification, such as replacing long strings with an elided version ("Some lengthy messa...")

Instantiate. Will attempt to take focus.

Takes the following named parameters:

  • on_activate - coderef to call when the user hits the Enter key, will be passed the highlighted row or selection when in "multi_select" mode, see "on_activate" for more details.
  • multi_select - when set, the widget will allow selection of multiple rows (typically by pressing Space to toggle a given row)
  • adapter - an Adapter::Async::OrderedList::Array instance
  • data - alternative to passing an adapter, if you want to wrap an existing array without creating an Adapter::Async::OrderedList subclass yourself

Returns a new instance.

Bus for event handling. Normally an Adapter::Async::Bus instance shared by the adapter.

Clear all data in the table.

Expose the given row (provided as an index into the underlying storage).

 $tbl->expose_row(14);

Add a new column. Takes the following named parameters:
  • width - (optional) number of columns
  • type - (optional) data type, currently only supports 'text' (the default)
  • align - (optional) align left, center or right
  • transform - (optional) list of transformations to apply
  • visible - (optional) true if this column should be shown

Returns $self.

Returns the selected row, or multiple rows as a list if multi_select is enabled. If multi_select is enabled it does not return the row currently highlighted (unless that row is also selected).

Accessor for the activation callback - if called without parameters, will return the current coderef (if any), otherwise, will set the new callback.

This callback will be triggered via "key_activate":

 $code->($row_index, $row_data_as_arrayref)

If multiselect is enabled, the callback will have the following:

 $code->(
   [$highlight_row_index, @selected_row_indices],
   $highlight_row_data_as_arrayref,
   @selected_rows_as_arrayrefs
 )

(the selected row data + index list could be empty here)

Accessor for multi_select mode - when set, this allows multiple rows to be selected.

Number of lines to request.

Number of columns to request.

True if there's a vertical scrollbar (currently there is no way to disable this scrollbar).

True if there's a horizontal scrollbar. There isn't one, this always returns false.

Current row offset (vertical scroll position).

Returns the Tickit::Rect representing the header area.

Returns the Tickit::Rect representing the body area.

Returns the Tickit::Rect representing the scroll bar.

Removes the header - the body will expand upwards to compensate. .

Makes the header visible again. See "hide_header".

Returns true if the header is visible, 0 otherwise.

Returns the number of lines in the header. Hardcoded to 1.

Returns the number of lines in the body.

Returns the number of columns in the body.

Returns a storage index from a body row index.

Returns a body row index from a storage index.

Returns a row cache offset from a storage index.

Returns a storage index from a row cache offset.

Returns the index of the currently-highlighted row.

Returns the position of the highlighted row taking scrollbar into account.

Render the table. Called from expose events.

Render the header area.

Render a specific header cell.

Render the scrollbar.

Render the table body.

Renders a given row, using storage index.

Update row cache to reflect a scroll event.

Helper method to apply a series of coderefs to a value.

Row cache accessor.

Apply the transformations just before we render. Can return anything we know how to render.

Handle reshape requests.

Distribute space between columns.

Called when a window has been assigned to the widget.

Expose the given rows.

Update scroll information after changing highlight position.

Move the highlighted row by the given offset (can be negative to move up).

Current vertical scrollbar position.

Total number of rows.

Current scrollbar height.

Positions of the scrollbar indicator.

Rectangle representing the area covered by the current scrollbar.

Size of the vertical scrollbar.

Applies a new adapter, taking care of any cleanup if there was an adapter previously active.

Can be passed undef, to remove the adapter completely.

Invoked by the adapter when data is added to or removed from the data source.

Called by the adapter when all data has been removed from the data source.

Go to the previous row.

Move to the next row.

Move to the first row.

Move to the last row.

Go up a page.

Go down a page.

Move to the next column.

Move to the previous column.

Move to the first column.

Move to the last column.

Call the " on_activate " coderef with either the highlighted item, or the selected items if we're in multiselect mode.

 $on_activate->([ row indices ], [ items... ])

The items will be as returned by the storage adapter, and will not have any of the data transformations applied.

Toggle selected row.

Very broken. Ignore these for now. Sorry.

Sets the visibility of the given row (by index).

Example:

 # Make row 5 hidden
 $tbl->row_visibility(5, 0)
 # Show row 0
 $tbl->row_visibility(0, 1)

This will use the given coderef to set the visibility of each row in the table. The coderef will be called once for each row, and should return true for rows which should be visible, false for rows to be hidden.

The coderef currently takes a single parameter: an arrayref representing the columns of the row to be processed.

 # Hide all rows where the second column contains the text 'OK'
 $tbl->filter(sub { shift->[1] ne 'OK' });

Note that this does not affect row selection: if the multiselect flag is enabled, it is possible to filter out rows that are selected. This behaviour is by design (the idea was to allow union select via different filter criteria), call the "unselect_hidden_rows" method after filtering if you want to avoid this.

Also note that this is a one-shot operation. If you add or change data, you'll need to reapply the filter operation manually.

Helper method to mark any hidden rows as unselected. Call this after "filter" if you want to avoid confusing users with invisible selected rows.

Current list of pending features:
  • Column and cell highlighting modes
  • Proper widget-in-cell support
  • Better header support (more than one row, embedded widgets)
  • More efficient redraw when showing/hiding header (scroll body and redraw just the header lines)

Other tables and table-like things:
  • Tickit::Widget::Table::Paged - earlier version of this module without adapter support
  • Text::ANSITable - not part of Tickit but has some impressive styling capabilities.
  • Term::TablePrint - again, not part of Tickit but provides an interactive table via direct terminal access.

And these are probably important background reading for formatting and data source support:

  • String::Tagged - supported for applying custom formatting (specifically, pen attributes)
  • Adapter::Async - API for dealing with abstract data sources
  • Adapter::Async::OrderedList - subclass of the above for our tabular layout API

Tom Molesworth <TEAM@cpan.org>

With thanks to the following for contribution:
  • Paul "LeoNerd" Evans for testing and suggestions on storage/abstraction handling
  • buu, for testing and patches

Copyright Tom Molesworth 2012-2015. Licensed under the same terms as Perl itself.
2016-12-31 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.