Wx::Perl::DbLinker::Wxdatasheet - a module that display data from a database in a tabular format using a WxGrid object
See Version in Wx::Perl::DbLinker
The code build a table having 6 columns: 3 text entries, 2 combo, 1 toogle. A dataManager object is needed for each combo, and a dataManager for the table itself. The example here use Rose::DB::Object to access the tables.
This gets the Rose::DB::Object::Manager (Plain sql command and DBI, or DBIx::Class object could have been used instead)
my $datasheet_rows = Rdb::Mytable::Manager->get_mytable(sort_by => 'field1');
This object is used to instanciante a RdbDataManager, that will be used in the datasheet constructor.
my $dman = Gtk2::Ex::DbLinker::RdbDataManager->new({data => $datasheet_rows, meta => Rdb::Mytable->meta });
The RdbDataManager for the combo rows is created:
my $combo_data = Rdb::Combotable::Manager->get_combotable( select => [qw/t1.id t1.name/], sort_by => 'name'); my $dman_combo_1 = Gtk2::Ex::DbLinker::RdbDataManager->new({data => $combo_data, meta => Rdb::Combotable->meta}); my $combo2_data = Rdb::Combotable2::Manager->get_combotable2( sort_by => 'country'); my $dman_combo_2 = Gtk2::Ex::DbLinker::RdbDataManager->new({ data =>$combo2_data, meta => Rdb::Combotable2->meta, });
The Wxdatasheet object with the columns description is created:
#$where_name is the name of the panel that will receive the grid my $where = Wx::Window::FindWindowById( Wx::XmlResource::GetXRCID($where_name), $top_panel ); my $s = $where->GetSizer(); $s = ( defined $s ? $s : Wx::BoxSizer->new(wxVERTICAL)); my $sw = Wx::ScrolledWindow->new($where, wxID_ANY); #, Wx::Point->new(0, 0), Wx::Size->new(400, 400), wxVSCROLL | wxHSCROLL); $sw->SetScrollbars(1,1,1,1); $s->Add($sw, 1, wxALL|wxEXPAND, 1); $where->SetSizer($s); my $dsparam = { fields => $sfref->{fields}, data_manager => $sfref->{rs}, on_row_select => sub { $self->on_row_select($self->{sf}->{$sfname}, $sfname, $self->{sf}->{$sfname}->{pk})}, on_changed => sub {$self->on_row_changed($self->{sf}->{$sfname})}, borders_size => [20, 25], #horizontal: row_1: heigth, vertical : col_1: witdh }; my $grid = Wx::Perl::Wxdatasheet->new($sw, $dsparam); $s = Wx::BoxSizer->new(wxVERTICAL); $sw->SetSizer($s); $s->Add($grid, 1, wxALL|wxEXPAND, 1);
To change the rows displayed in the table, the new set of rows is fetched. An object derived from Rose::DB::Object::Manager is passed to the Gt2::Ex::DbLinker::RdbDatamanager object using the query method:
my $data = Rdb::Mytable::Manager->get_mytable(query =>[pk_field =>{eq=> $primarykey_value}], sort_by => 'field1'); $self->{dataseet}->get_data_manager->query($data); $self->{datasheet}->update();
This module automates the process of setting up a WxGrid based on field definitions you pass it. The first column show the state of the reccord : - blank : unchanged - ! : changed and not saved to the database - x : mark for deletion - o : locked
Steps for use:
Instanciate a xxxDataManager that will fetch a set of rows.
Get a reference to the Wx::Pane that will received the grid, add a Wx::ScrolledWindow using sizer.
Create a xxxDataManager holding the rows to display, if the datasheet has combo box, create the corresponding DataManager that hold the combo box content.
Create a Wxdatasheet object and pass it the following parameters: the scrolledwindow and a hash reference. The constructor return the Wx::Grid object that. Add the grid to the scrolledwindow using a sizer.
You would then typically connect some buttons to methods such as inserting, deleting, etc.
The new() method expects a Wx::window object and a hash reference of key / value pairs.
new()
data_manager a instance of a xxxDataManager object.
data_manager
on_changed a code ref that will be called when a value is changed in the grid
on_changed
on_row_select a code ref that will be called when a row is selected
on_row_select
border_size an array ref that holds the height of the field labels row and the width of the left column that displays the records state
border_size
fields a reference to an array of hash. Each hash has the following key / value pairs.
fields
name / name of the field to display.
name
renderer / one of "text combo toggle hidden".
renderer
renderer_function for text render, a coderef can be pass to set the cell value. The function will received the row and column number and the base table object. For example, this method
renderer_function
sub display_url { my ($self, $row, $col, $table) =@_; my $column_no = $self->{foo}->colnumber_from_name( "fooid" ); # the column where a value is taken from my $key_value = $table->GetValue($row, $column_no ); my $result=""; if ($key_value) { $result = "http://somewhere/on_the_internet/record.cgi?&fooid=" . $key_value; } return $result; }
It can be passed to the Wxdatasheet constructor with
{ fields => [{name=>"bar", renderer=>"text"}, {name=>"fooid"}, {name=>"url", renderer=>"text", renderer_function => sub {display_url ($self, @_);}}, ], data_manager => ... }
if the renderer is a combo (Wx::ComboBox or Wx::ListBox) the following key / values are needed in the same hash reference:
data_manager / an instance holding the rows of the combo.
fieldnames / a reference to an array of the fields that populate the combo. The first one is the return value that correspond to the field given in name.
fieldnames
update();
Reflect in the user interface the changes made after the data manager has been queried, or on the datasheet creation.
get_data_manager();
Returns the data manager to be queried.
set_column_value($field_name, $value);
Set $value in $field_name for the grid current row (where the cursor is).
get_column_value($field_name);
Return the content of $field_name for the grid current row.
insert();
Displays an empty rows.
delete();
Marks the current row to be deleted. The delele itself will be done on apply.
apply();
Save a new row, save changes on an existing row, or delete the row(s) marked for deletion.
undo();
Revert the row to the original state in displaying the values fetch from the database.
Any Wx::Perl::DbLinker questions or problems can be posted to me (rappazf) on my gmail account.
The current state of the source can be extract using Mercurial from http://sourceforge.net/projects/wx-perl-dblinker/.
François Rappaz <rappazf@gmail.com>
Copyright (c) 2015 by François Rappaz. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Wx::Perl::DbLinker, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Wx::Perl::DbLinker
CPAN shell
perl -MCPAN -e shell install Wx::Perl::DbLinker
For more information on module installation, please visit the detailed CPAN module installation guide.