|
NAMETickit::Widget::Table - table widget with support for scrolling/pagingVERSIONversion 0.216SYNOPSIS#!/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; DESCRIPTIONWARNING: 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. TransformationsApply to:
Item transformations This takes the original data item for the row, and returns one of the following:
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...") METHODSnewInstantiate. Will attempt to take focus.Takes the following named parameters:
Returns a new instance. busBus for event handling. Normally an Adapter::Async::Bus instance shared by the adapter.METHODS - Table contentclearClear all data in the table.expose_rowExpose the given row (provided as an index into the underlying storage).$tbl->expose_row(14); add_columnAdd a new column. Takes the following named parameters:
Returns $self. selected_rowsReturns 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).METHODS - Callbackson_activateAccessor 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) multi_selectAccessor for multi_select mode - when set, this allows multiple rows to be selected.METHODS - OtherlinesNumber of lines to request.colsNumber of columns to request.vscrollTrue if there's a vertical scrollbar (currently there is no way to disable this scrollbar).hscrollTrue if there's a horizontal scrollbar. There isn't one, this always returns false.row_offsetCurrent row offset (vertical scroll position).header_rectReturns the Tickit::Rect representing the header area.body_rectReturns the Tickit::Rect representing the body area.scrollbar_rectReturns the Tickit::Rect representing the scroll bar.hide_headerRemoves the header - the body will expand upwards to compensate. .show_headerMakes the header visible again. See "hide_header".header_visibleReturns true if the header is visible, 0 otherwise.header_linesReturns the number of lines in the header. Hardcoded to 1.body_linesReturns the number of lines in the body.body_colsReturns the number of columns in the body.idx_from_rowReturns a storage index from a body row index.row_from_idxReturns a body row index from a storage index.row_cache_idxReturns a row cache offset from a storage index.idx_from_row_cacheReturns a storage index from a row cache offset.highlight_rowReturns the index of the currently-highlighted row.highlight_visible_rowReturns the position of the highlighted row taking scrollbar into account.METHODS - Renderingrender_to_rbRender the table. Called from expose events.render_headerRender the header area.render_header_cellRender a specific header cell.render_scrollbarRender the scrollbar.render_bodyRender the table body.render_rowRenders a given row, using storage index.on_scrollUpdate row cache to reflect a scroll event.fold_futureHelper method to apply a series of coderefs to a value.row_cacheRow cache accessor.apply_view_transformationsApply the transformations just before we render. Can return anything we know how to render.reshapeHandle reshape requests.distribute_columnsDistribute space between columns.window_gainedCalled when a window has been assigned to the widget.expose_rowsExpose the given rows.scroll_highlightUpdate scroll information after changing highlight position.move_highlightMove the highlighted row by the given offset (can be negative to move up).scroll_positionCurrent vertical scrollbar position.row_countTotal number of rows.sb_heightCurrent scrollbar height.scroll_rowsPositions of the scrollbar indicator.active_scrollbar_rectRectangle representing the area covered by the current scrollbar.scroll_dimensionSize of the vertical scrollbar.on_adapter_changeApplies a new adapter, taking care of any cleanup if there was an adapter previously active.Can be passed undef, to remove the adapter completely. on_splice_eventInvoked by the adapter when data is added to or removed from the data source.on_clear_eventCalled by the adapter when all data has been removed from the data source.METHODS - Key bindingskey_previous_rowGo to the previous row.key_next_rowMove to the next row.key_first_rowMove to the first row.key_last_rowMove to the last row.key_previous_pageGo up a page.key_next_pageGo down a page.key_next_columnMove to the next column.key_previous_columnMove to the previous column.key_first_columnMove to the first column.key_last_columnMove to the last column.key_activateCall 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. key_select_toggleToggle selected row.METHODS - FilteringVery broken. Ignore these for now. Sorry.row_visibilitySets 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) filterThis 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. unselect_hidden_rowsHelper method to mark any hidden rows as unselected. Call this after "filter" if you want to avoid confusing users with invisible selected rows.TODOCurrent list of pending features:
SEE ALSOOther tables and table-like things:
And these are probably important background reading for formatting and data source support:
AUTHORTom Molesworth <TEAM@cpan.org>CONTRIBUTORSWith thanks to the following for contribution:
LICENSECopyright Tom Molesworth 2012-2015. Licensed under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |