Re: SimpleList - sorting by column
- From: "muppet" <scott asofyet org>
- To: gtk-perl-list gnome org
- Subject: Re: SimpleList - sorting by column
- Date: Wed, 26 Nov 2003 12:05:41 -0500 (EST)
David Sword said:
Is it possible to sort items in a simplelist, in the way that is
possible with a TreeView column?
indeed it is. in fact, you'd do it the same way you do with TreeView, because
a SimpleList is just a reblessed TreeView that guarantees a 1-to-1 mapping in
the order of the columns in the view with columns in the model, and which
makes it a lot easier to get data in and out of the model.
you will want to fetch the columns and set their sort ids accordingly:
@columns = $simplelist->get_columns;
for (my $i = 0 ; $i < @columns ; $i++) {
$columns[$i]->set_sort_column_id ($i);
}
# or
$simplelist->get_column (0)->set_sort_column_id (0);
$simplelist->get_column (2)->set_sort_column_id (2);
and that about does it.
.... BUT ....
if the data in the column you are making sortable is not a known type, you'll
have to set a sort function. for example, in the simplelist.pl example,
column 7, the "sum of array" column, is of type GPerlSV and generates the
warning "Attempting to sort on invalid type GPerlSV". the solution:
# add at line 86 of Gtk2/examples/simplelist.pl
$slist->get_column (7)->set_sort_column_id (7);
$slist->get_model->set_sort_func (7, sub {
my ($sortable, $iter_left, $iter_right) = @_;
my $info = $sortable->get ($iter_left, 7);
my $left = 0;
foreach my $i (@$info) { $left += $i }
$info = $sortable->get ($iter_right, 7);
my $right = 0;
foreach my $i (@$info) { $right += $i }
return $left <=> $right;
});
and, voila, you can click the column header of the "Sum of Array" column to
sort by the sum of the array.
note that i use 7 everywhere because simplelist guarantees that column 7 in
the view displays the contents of column 7 in the model. if, however, i
wanted column 7 to display the contents of column 4 in the model, and sort
column 7 in the view by the contents of column 5 in the model, i could do
that, too.
--
muppet <scott at asofyet dot org>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]