Menu Principal
  O que é PHP-GTK ?
  Lista de discussões
  Fórum de discussões
  Documentação
  Ver Artigos
  Ver Aplicações
  Users Map
  Para Linux
  Para Windows

 Login
 Login
 Senha
  Efetuar Cadastro
  Esqueci a Senha

 Busca no Site
Parceiros


PHP-GTK Brasil :: Paginação Com GTKTreeView
Título
Paginação Com GTKTreeView
Resumo
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;
    }
    
}

/**
 * Exemplo de Paginação
 *
 */
class App {
    
    private 
$data null;
    private 
$stored null;
    private 
$treeview null;
    private 
$scroll null;
    private 
$hbuttons null;
    private 
$labelpages null;
    private    
$arrayButtons null;
    private 
$window null;
    private 
$pagina null;
    private 
$totpaginas null;
    
    
/**
     * 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(nullnull);
        
$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(646420 );
        
$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_AUTOMATICGtk::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');
        
        
$this->hbuttons->add($this->arrayButtons['prim']);
        
$this->hbuttons->add($this->arrayButtons['ante']);
        
$this->hbuttons->add($this->arrayButtons['prox']);
        
$this->hbuttons->add($this->arrayButtons['ulti']);
    }

    
/**
     * Cria e monta a GTKWindow
     */
    
private function criaWindow(){
        
$this->window = new GTKWindow();
        
$this->window->set_default_size(500300);
        
$this->window->set_title('Paginacao por fernandohcorrea');
        
        
$vbox = new GtkVBox();
        
$vbox->pack_start($this->scroll);
        
$vbox->pack_start($this->labelpagesfalse);
        
$vbox->pack_start($this->hbuttonsfalse );

        
$this->window->add($vbox);
        
$this->window->connect_simple('destroy', array('gtk''main_quit'));
    }

    
/**
     * Responde por obter os dados e montar as Páginas
     */
    
private function getDados(){
        
$datab = new DataB();
        
$dados $datab->getData();
        
        
$paginas array_chunk($dados,50,true);
        
        
$idx 1;
        foreach(
$paginas as $key => $pconteudo){
            
$this->data[$idx] = $pconteudo;
            
$idx++;
        }
        
        
$this->totpaginas count($this->data);
    }

    
/**
     * Direciona páginas 
     *
     * @param GTKButton $btn
     * @param string $go
     */
    
public function goPage($btn$go='start'){

        if(
$go == 'end'){
            
$data end($this->data);
        }
        else if(
$go =='next' and ($this->pagina $this->totpaginas)){
            
$data next($this->data);
        }
        else if(
$go =='prev' and ($this->pagina 1)){
            
$data prev($this->data);
        }
        else{
            
reset($this->data);
            
$data current($this->data);
        }
        
        
$this->pagina key($this->data);
        
$this->mostraIndice();
        
$this->montaPag($data);
    }
    
    
/**
     * Monta a Página no stored
     *
     * @param array $data
     */
    
private function montaPag($data){
        
$this->stored->clear();        
        if(
count($data)>0)
        foreach(
$data as $dados){
            
$this->stored->append(
                    array(    
$dados['cod'], $dados['mdcinco'], $dados['boolean'] )
            );
        }
    }
    
    
/**
     * Exibe o Índice
     */
    
private function mostraIndice(){        
        
$text $this->pagina."/".$this->totpaginas;        
        
$this->labelpages->set_text($text);
    }
    
    
/**
     * Show do APP.
     */
    
private function showWindow(){
        
$this->window->show_all();
    } 
}

new 
APP();
GTK::main();

?>

Imagem

Comentários

 Adicionar Comentário
 login
 Senha
 Título
 Comentário




PHP-GTK Brasil
PHP-GTK : Segunda Edição, um livro totalmente novo, abordando PHP5 e GTK2!!