Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Sortable table using Gtk2 in UI application

by zentara (Archbishop)
on Sep 30, 2011 at 17:34 UTC ( [id://928870]=note: print w/replies, xml ) Need Help??


in reply to Sortable table using Gtk2 in UI application

Here is a simpler way to sort a list, using SimpleList. There are other ways too.
#!/usr/bin/perl use warnings; use strict; use Gtk2 -init; use Gtk2::SimpleList; # click on the "int" column header to sort on it my $win = Gtk2::Window->new; $win->signal_connect (delete_event => sub { Gtk2->main_quit; }); my $vbox = Gtk2::VBox->new; $win->add ($vbox); my $slist = Gtk2::SimpleList->new ( 'Int' => 'int', 'Text' => 'text' ) +; @{$slist->{data}} = ( [11, 'text1'], [21, 'text2'], [3, 'text3'] ); $slist -> set_reorderable( 1 ); #my @columns = $slist->get_columns; #for (my $i = 0 ; $i < @columns ; $i++) { #$columns[$i]->set_sort_column_id ($i); #} # or $slist->get_column (0)->set_sort_column_id (0); #$slist->get_column (1)->set_sort_column_id (1); #in case you want to catch the sort occuring $slist->get_column(0)->signal_connect( clicked => sub { warn "clicked!\n" } ); $vbox->add ($slist); $win->show_all; Gtk2->main;

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Sortable table using Gtk2 in UI application
by SyneRohit (Initiate) on Oct 03, 2011 at 10:10 UTC
    Thank you. This solution worked for me.
      Your welcome. After posting that example code, I extended it to multicolumn sorting. You may find this example even better. It sorts on each column, by clicking that column header.
      #!/usr/bin/perl use warnings; use strict; use Gtk2 -init; use Gtk2::SimpleList; my $win = Gtk2::Window->new; $win->signal_connect (delete_event => sub { Gtk2->main_quit; }); my $vbox = Gtk2::VBox->new; $win->add ($vbox); my $slist = Gtk2::SimpleList->new ( 'Int' => 'int', 'Text' => 'text' ) +; @{$slist->{data}} = ( [11, 'text1'], [21, 'text2'], [3, 'text3'], [41, + '4text'] ); $slist -> set_reorderable( 1 ); #my @columns = $slist->get_columns; #for (my $i = 0 ; $i < @columns ; $i++) { #$columns[$i]->set_sort_column_id ($i); #} # or $slist->get_column (0)->set_sort_column_id (0); $slist->get_column (1)->set_sort_column_id (1); $slist->get_column(0)->signal_connect( clicked => sub { warn "clicked 0!\n" } ); $slist->get_column(1)->signal_connect( clicked => sub { warn "clicked 1!\n" } ); $vbox->add ($slist); my $button = Gtk2::Button->new('Show Data'); $vbox->pack_end( $button, 0, 0, 0 ); $button->signal_connect( clicked => sub { print @{$slist->{data}},"\n"; # demonstrates reordering foreach my $arr ( @{$slist->{data}} ){ print "$arr->[0]\t$arr->[1]\n" } print "\n\n"; } ); $win->show_all; Gtk2->main;

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://928870]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2024-04-19 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found