Na Lista do PHP-GTK Brasil apareceu uma dessa duvidas. Como paginar uma massa de dados muito grande em um TreeView?
Texto
Neste exemplo uso das funções que o php tem para trabalhar com o ARRAY.
-reset();
-current();
-next();
-prev();
-end();
Código
<?php /** * Exemplo de GTKTreeview. * Paginação * Utilizando Edição de Toggle e GtkCellRendererText Texto. * Com GtkListStore , GtkCellRendererText, GtkCellRendererToggle * By Fernando H. Correa. */
/** * Simula um DadaBase * */ class DataB{
private $dados = null;
/** * Cria os Dados */ public function __construct(){ for ($i = 0; $i < 270; $i++ ){ $dados[] = array( 'cod' => $i, 'mdcinco' => md5("Dados para Teste, esse não aparece $i"), 'boolean' => ($i%2) ); }
$this->dados = $dados; }
/** * Retorna os dados * * @return array */ public function getData(){ return $this->dados; }
/** * Inicia Processo de criação da GUI */ public function __construct(){ $this->criaStored(); $this->criaTreeView(); $this->criaScroll(); $this->criaLabel(); $this->criaHbuttons(); $this->criaWindow(); $this->getDados(); $this->goPage(null, null); $this->showWindow(); }
/** * Cria o GTKListStored * Repare que estou usando os valores não simbolicos e sim os numéricos. */ private function criaStored(){ //$this->stored = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING, Gtk::TYPE_BOOLEAN); $this->stored = new GtkListStore(64, 64, 20 ); $this->stored->clear(); }
/** * Cria o GTKTreeView * * Com 3 Colunas */ private function criaTreeView(){ $this->treeview = new GtkTreeView($this->stored);
$cellRend1 = new GtkCellRendererText(); $col1 = new GtkTreeViewColumn('Cod', $cellRend1, 'text', 0); $this->treeview->append_column($col1);
$cellRend2 = new GtkCellRendererText(); $col2 = new GtkTreeViewColumn('MD5', $cellRend2, 'text', 1); $this->treeview->append_column($col2);
$cellRend3 = new GtkCellRendererToggle(); $col3 = new GtkTreeViewColumn('Boolean', $cellRend3, 'active', 2); $this->treeview->append_column($col3);
}
/** * Cria o GTKScroll */ private function criaScroll(){ $this->scroll = new GtkScrolledWindow(); $this->scroll->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $this->scroll->add($this->treeview); }
/** * Cria o GTKLabel do índice * */ private function criaLabel(){ $this->labelpages = new GtkLabel('0/0'); }
/** * Cria o GTKHButtonBox */ private function criaHbuttons(){ $this->hbuttons = new GtkHButtonBox();
$this->arrayButtons['prim'] = new GtkButton('<<'); $this->arrayButtons['prim']->connect('clicked',array($this,'goPage'),'start');
$this->arrayButtons['ante'] = new GtkButton('<'); $this->arrayButtons['ante']->connect('clicked',array($this,'goPage'),'prev');
$this->arrayButtons['prox'] = new GtkButton('>'); $this->arrayButtons['prox']->connect('clicked',array($this,'goPage'),'next');
$this->arrayButtons['ulti'] = new GtkButton('>>'); $this->arrayButtons['ulti']->connect('clicked',array($this,'goPage'),'end');
/** * Cria e monta a GTKWindow */ private function criaWindow(){ $this->window = new GTKWindow(); $this->window->set_default_size(500, 300); $this->window->set_title('Paginacao por fernandohcorrea');
$vbox = new GtkVBox(); $vbox->pack_start($this->scroll); $vbox->pack_start($this->labelpages, false); $vbox->pack_start($this->hbuttons, false );