App-Widget
view release on metacpan or search on metacpan
lib/App/Widget/DataTable2.pm view on Meta::CPAN
$name = "get_data";
$w = App::Widget::DataTable2->new($name);
print $w->html();
=cut
######################################################################
# CONSTANTS
######################################################################
######################################################################
# ATTRIBUTES
######################################################################
# {border} = 0;
# {cellspacing} = 2;
# {cellpadding} = 0;
# {width} = "";
# {bgcolor} = "";
# {nowrap} = "1";
# {font_face} = "verdana,geneva,arial,sans-serif";
# {font_size} = "-2";
# {font_color} = "";
# {heading_bgcolor} = "#cccccc";
# {heading_nowrap} = 0;
# {heading_align} = 0;
# {heading_valign} = 0;
# {column_selectable} = 1;
# {row_selectable} = 0;
# {row_single_selectable} = 0;
# {columns} = [ "Name", "Address", "City", "State", "Country", "Home Phone" ];
# {headings} = [ "Name", "Address", "City", "State", "Country", "Home Phone" ];
# {data} = [ [ "Smith, Harold", "1215 Interloke Pass", "Jonesboro", "GA", "US", "770-603-1810" ],
# [ "Smith, Mike", "1215 Interloke Pass", "Jonesboro", "GA", "US", "770-603-1811" ],
# [ "Smith, Sarah", "1215 Interloke Pass", "Jonesboro", "GA", "US", "770-603-1812" ],
# [ "Smith, Ken", "1215 Interloke Pass", "Jonesboro", "GA", "US", "770-603-1813" ],
# [ "Smith, Mary", "1215 Interloke Pass", "Jonesboro", "GA", "US", "770-603-1814" ], ];
# {startrow} = 1
# {maxrows} = 20
# {scrollable} = 0;
# {sortable} = 0;
# {filterable} = 0;
# {editable} = 0;
# INPUTS FROM THE ENVIRONMENT
=head1 DESCRIPTION
This class is a <input type=submit> HTML element.
In the advanced configurations, it is rendered as an image button.
=cut
######################################################################
# INITIALIZATION
######################################################################
sub _init {
&App::sub_entry if ($App::trace);
my $self = shift;
$self->SUPER::_init(@_);
$self->{table} = $self->{name} if (!$self->{table});
if (!$self->{keycolidx}) {
my $context = $self->{context};
my $table = $self->{table};
my $rep = $self->{repository};
my $r = $context->repository($rep);
$r->_load_table_metadata($table);
my $primary_key = $r->{table}{$table}{primary_key};
if ($primary_key && ref($primary_key) eq "ARRAY" && $#$primary_key > -1) {
my (@keycolidx, %colidx);
my $columns = $self->get_columns();
for (my $col = 0; $col <= $#$columns; $col++) {
$colidx{$columns->[$col]} = $col;
}
for (my $col = 0; $col <= $#$primary_key; $col++) {
if (defined $colidx{$primary_key->[$col]}) {
push(@keycolidx, $colidx{$primary_key->[$col]});
}
}
$self->{keycolidx} = \@keycolidx if ($#keycolidx == $#$primary_key);
}
}
}
######################################################################
# EVENTS
######################################################################
# Usage: $widget->handle_event($event, @args);
sub handle_event {
&App::sub_entry if ($App::trace);
my ($self, $wname, $event, @args) = @_;
my ($name, $context, $column, $x, $y, $startrow, $maxrows, $width, $dir);
#$self->clear_messages();
$name = $self->{name};
$self->{context}->dbgprint("DataTable2($name)->handle_event($wname,$event,@args)")
if ($App::DEBUG && $self->{context}->dbg(1));
my $handled = 0;
if ($wname eq "$name-view") {
$self->set("mode","view");
$self->delete("editdata");
$self->delete("add_data");
$handled = 1;
}
elsif ($wname eq "$name-edit") {
$self->set("mode","edit");
$handled = 1;
}
elsif ($wname eq "$name-refresh") {
$handled = 1;
}
elsif ($wname eq "$name-next") {
$startrow = $self->get("startrow",1,1);
$maxrows = $self->get("maxrows",20,1);
$startrow += $maxrows;
$self->set("startrow",$startrow);
lib/App/Widget/DataTable2.pm view on Meta::CPAN
my $rows_to_add = $self->{rows_to_add} || 1;
my $add_data = $self->{add_data} || [];
for (my $i = 0; $i < $rows_to_add; $i++) {
push(@$add_data, {});
}
$self->set("add_data", $add_data);
$handled = 1;
}
elsif ($wname eq "$name-cancel_add") {
$self->set("add_data", []);
$handled = 1;
}
elsif ($wname eq "$name-delete") {
$self->delete_rows();
$handled = 1;
}
elsif ($wname eq "$name-copy") {
$self->copy();
$self->delete("row_selected");
$handled = 1;
}
elsif ($event eq "sort") {
($column, $dir) = @args;
my $columns = $self->get_columns();
my $order_by = $self->{order_by} || $self->{ordercols}; # ordercols is deprecated in favor of order_by
my $direction = $self->{direction} || $self->{directions}; # directions is deprecated in favor of direction
$direction ||= {};
if (defined $order_by) {
for (my $i = 0; $i <= $#$order_by; $i++) {
if ($order_by->[$i] eq $column) {
splice(@$order_by, $i, 1); # delete the use of $column
last;
}
}
unshift(@$order_by, $column); # put it at the beginning
$direction->{$column} = $dir;
}
else {
$order_by = [ $column ];
$direction = { $column => $dir };
}
$self->set("order_by",$order_by);
$self->set("direction",$direction);
$handled = 1;
}
elsif ($wname =~ /-sort_[_a-zA-Z0-9]*$/) {
($column, $x, $y) = @args;
$context = $self->{context};
$width = $context->widget($wname)->{width} || 50;
if ($x <= $width/2) {
$handled = $self->handle_event($wname, "sort", $column, "asc");
}
else {
$handled = $self->handle_event($wname, "sort", $column, "desc");
}
}
else {
$handled = $self->SUPER::handle_event(@_);
}
&App::sub_exit($handled) if ($App::trace);
return($handled);
}
######################################################################
# METHODS
######################################################################
sub add_row {
&App::sub_entry if ($App::trace);
my ($self, $row) = @_;
my $add_data = $self->{add_data} || [];
push(@$add_data, $row);
$self->set("add_data", $add_data);
$self->set("mode", "edit");
&App::sub_exit() if ($App::trace);
}
sub get_columns {
&App::sub_entry if ($App::trace);
my $self = shift;
my ($columns);
$columns = $self->{columns};
if (defined $columns && ref($columns) eq "") {
$columns = [ $columns ];
$self->set("columns", $columns);
}
if (!defined $columns) {
my ($repository, $rep, $table);
$repository = $self->{repository};
$table = $self->{table};
$rep = $self->{context}->repository($repository);
if ($rep && $table) {
$columns = $rep->get_column_names($table);
}
}
$columns = [] if (!defined $columns || ref($columns) eq "");
&App::sub_exit($columns) if ($App::trace);
$columns;
}
sub get_headings {
&App::sub_entry if ($App::trace);
my $self = shift;
$self->{context}->dbgprint("DataTable2->get_headings()")
if ($App::DEBUG && $self->{context}->dbg(1));
my ($table, $headings, $heading, $columns, $column, $lang);
$table = $self->{table};
$columns = $self->get_columns();
$headings = $self->{headings};
$lang = $self->{lang};
my $column_attribs = $self->{column} || {};
if (!defined $headings) {
$headings = [];
my ($repname, $context, $rep, $columnlabels);
$repname = $self->get("repository");
$context = $self->{context};
( run in 0.544 second using v1.01-cache-2.11-cpan-98e64b0badf )