Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Localização e preenchimento Preciso criar um formulário onde eu pesquiso algumas informações em uma tabela, insiro outras e todas as informações serão preenchidas no datagrid que posteriormente calcular alguns valor e gravar em uma tabela. Escrevi dois códigos e nenhum funciona, será que alguém pode me dar um help? Código 01 ...
PC
Localização e preenchimento  
Preciso criar um formulário onde eu pesquiso algumas informações em uma tabela, insiro outras e todas as informações serão preenchidas no datagrid que posteriormente calcular alguns valor e gravar em uma tabela.
Escrevi dois códigos e nenhum funciona, será que alguém pode me dar um help?
Código 01
  1. <?php
  2. class FormVendaProdutos extends TWindow
  3. {
  4.     private $form;
  5.     private $datagrid;
  6.     private $loaded;
  7.     private $pageNavigation;
  8.     private $parentForm;
  9.     
  10.     
  11.     function __construct()
  12.     {
  13.         parent::__construct();
  14.         new TSession();
  15.         //Cria os containers
  16.         $notebook = new TNotebook(500,200);
  17.         $this->form = new TForm('form_vendas');
  18.         $table = new TTable;
  19.         $this->form->add($table);
  20.         $notebook->appendPage('Vendas de Produtos',  $this->form); 
  21.         
  22.         // Cria o componente de busca
  23.         $id   = new TSeekButton('id');
  24.         $id->setValue(TSession::getValue('id_produto'));
  25.         $codigo         = new TEntry('codigo');
  26.     
  27.         $obj = new TStandardSeek;
  28.         $action = new TAction(array($obj'onSetup'));
  29.         $action->setParameter('database',      'loja');
  30.         $action->setParameter('parent',        'form_vendas');
  31.         $action->setParameter('model',         'produtos');
  32.         $action->setParameter('display_field''codigo');
  33.         $action->setParameter('receive_key',   'id');
  34.         $action->setParameter('receive_field''codigo');
  35.         $id->setAction($action);  
  36.         
  37.         $row=$table->addRow();
  38.         $row->addCell(new TLabel('Produto'));
  39.         $row->addCell($id);
  40.         
  41.         $row=$table->addRow();
  42.         $row->addCell(new TLabel('Código'));
  43.         $row->addCell($codigo);
  44.         
  45.         $carrega_button = new TButton('load');
  46.         $carrega_button->setAction(new TAction(array($this,'onReload')),'Carregar Produtos');
  47.         
  48.         $row=$table->addRow();
  49.         $row->addCell($carrega_button);
  50.         
  51.         $this->form->setFields(array($id,$codigo,$carrega_button));
  52.         
  53.         // Cria o datagrid
  54.         $this->datagrid = new TDataGrid();
  55.         $this->datagrid->setHeight(400);
  56.         
  57.         $codigo        = new TDataGridColumn('codigo','Código','left'30);
  58.         $descricao     = new TDataGridColumn('descricao','Descrição','left',200);
  59.         $valnor     = new TDataGridColumn('valnor','Normal',70);
  60.         $valpro     = new TDataGridColumn('valpro','Promoção',70);
  61.         $valult        = new TDataGridColumn('valult','Último',70);
  62.            $this->datagrid->addColumn($codigo);
  63.            $this->datagrid->addColumn($descricao);
  64.            $this->datagrid->addColumn($valnor);
  65.            $this->datagrid->addColumn($valpro);
  66.            $this->datagrid->addColumn($valult);
  67.         $this->datagrid->createModel();
  68.         
  69.         
  70.         // add the form to the page
  71.         $table = new TTable();
  72.         $table->addRow()->addCell($this->form);
  73.         $table->addRow()->addCell($this->datagrid);
  74.         //$table->addRow()->addCell($this->pageNavigation);
  75.         parent::add($table);        
  76.       }
  77.       
  78.       function onReload()
  79.       {
  80.           try {
  81.               $data $this->form->getData();
  82.               TTransaction::open('loja');
  83.               $conn TTransaction::get();
  84.               $repository = new TRepository('produtos');
  85.               $limit=10;
  86.               $criteria = new TCriteria();
  87.               $criteria $data;
  88.               $produtos $repository->load($criteria);
  89.               
  90.               // limpa datagrid
  91.               $this->datagrid->clear();
  92.               if ($produtos)
  93.               {
  94.                   foreach ($produtos as $produto)
  95.                   {
  96.                       $this->datagrid->addItem($produto);
  97.                   }
  98.               }
  99.               $criteria->resetProperties();
  100.               $count$repository->count($criteria);
  101.               
  102.               //$this->pageNavigation->setCount($count);
  103.               //$this->pageNavigation->setProperties($param);
  104.               //$this->pageNavigation->setLimit($limit);
  105.               
  106.               TTransaction::close();
  107.               $this->loaded true;
  108.           }
  109.           catch (Exception $e)
  110.           {
  111.               new TMessage('error''<b>Error</b>' $e->getMessage());
  112.               TTransaction::rollback();
  113.           }
  114.     }
  115. /*    
  116.     function show()
  117.     {
  118.         if (!$this->loaded)
  119.         {
  120.             $this->onReload();
  121.         }
  122.         parent::show();        
  123.     }
  124.     */
  125. }
  126. ?>

Código 02
  1. <?php
  2. /**
  3.  * City Seek
  4.  *
  5.  * @version    1.0
  6.  * @package    samples
  7.  * @subpackage tutor
  8.  * @author     Pablo Dall'Oglio
  9.  * @copyright  Copyright (c) 2006-2013 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10.  * @license    http://www.adianti.com.br/framework-license
  11.  */
  12. class FormVendaProd extends TWindow
  13. {
  14.     private $form;      // form
  15.     private $datagrid;  // datagrid
  16.     private $pageNavigation;
  17.     private $parentForm;
  18.     private $loaded;
  19.     
  20.     /**
  21.      * constructor method
  22.      */
  23.     public function __construct()
  24.     {
  25.         parent::__construct();
  26.         new TSession;
  27.         
  28.         // creates the form
  29.         $this->form = new TForm('form_venda_prod');
  30.         // creates the table
  31.         $table = new TTable;
  32.         
  33.         // add the table inside the form
  34.         $this->form->add($table);
  35.         
  36.         // create the form fields
  37.         $id= new TEntry('id');
  38.         // keep the session value
  39.         $id->setValue(TSession::getValue('id_produto'));
  40.         
  41.         // add the field inside the table
  42.         $row=$table->addRow();
  43.         $row->addCell(new TLabel('Código Produto:'));
  44.         $row->addCell($id);
  45.         
  46.         // create a find button
  47.         $find_button = new TButton('search');
  48.         // define the button action
  49.         $find_button->setAction(new TAction(array($this'onSearch')), 'Procurar');
  50.         $find_button->setImage('ico_find.png');
  51.         
  52.         // add a row for the find button
  53.         $row=$table->addRow();
  54.         $row->addCell($find_button);
  55.         
  56.         // define wich are the form fields
  57.         $this->form->setFields(array($id$find_button));
  58.         
  59.         // create the datagrid
  60.         $this->datagrid = new TDataGrid;
  61.         
  62.         // create the datagrid columns
  63.         $id            = new TDataGridColumn('id',    'Id',   'right',   70);
  64.         $codigo          = new TDataGridColumn('codigo',  'Codigo''left',   70);
  65.         $descricao     = new TDataGridColumn('descricao''Descrição''left',  250);
  66.         $valnor     = new TDataGridColumn('valnor''Normal''left',  100);
  67.         $valpro     = new TDataGridColumn('valpro''Promoção''left',  100);
  68.         $valult     = new TDataGridColumn('valult''Último''left',  100);
  69.         
  70.         $order1= new TAction(array($this'onReload'));
  71.         $order2= new TAction(array($this'onReload'));
  72.         
  73.         $order1->setParameter('order''id');
  74.         $order2->setParameter('order''descricao');
  75.         
  76.         // define the column actions
  77.         $id->setAction($order1);
  78.         $descricao->setAction($order2);
  79.         
  80.         // add the columns inside the datagrid
  81.         $this->datagrid->addColumn($id);
  82.         $this->datagrid->addColumn($codigo);
  83.         $this->datagrid->addColumn($descricao);
  84.         $this->datagrid->addColumn($valnor);
  85.         $this->datagrid->addColumn($valpro);
  86.         $this->datagrid->addColumn($valult);
  87.         
  88.         // create one datagrid action
  89.         $action1 = new TDataGridAction(array($this'onSelect'));
  90.         $action1->setLabel('Selecionar');
  91.         $action1->setImage('ico_apply.png');
  92.         $action1->setField('id');
  93.         
  94.         // add the action to the datagrid
  95.         $this->datagrid->addAction($action1);
  96.         
  97.         // create the datagrid model
  98.         $this->datagrid->createModel();
  99.         
  100.         // create the page navigator
  101.         $this->pageNavigation = new TPageNavigation;
  102.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  103.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  104.         
  105.         // create a table for layout
  106.         $table = new TTable;
  107.         // create a row for the form
  108.         $row $table->addRow();
  109.         $row->addCell($this->form);
  110.         
  111.         // create a row for the datagrid
  112.         $row $table->addRow();
  113.         $row->addCell($this->datagrid);
  114.         
  115.         // create a row for the page navigator
  116.         $row $table->addRow();
  117.         $row->addCell($this->pageNavigation);
  118.         
  119.         // add the table inside the page
  120.         parent::add($table);
  121.     }
  122.     
  123.     /**
  124.      * Register a filter in the session
  125.      */
  126.     function onSearch()
  127.     {
  128.         // get the form data
  129.         $data $this->form->getData();
  130.         
  131.         // check if the user has filled the fields
  132.         if (isset($data->id))
  133.         {
  134.             // cria um filtro pelo conteúdo digitado
  135.             $filter = new TFilter('id''='$data->id);
  136.             
  137.             // armazena o filtro na seção
  138.             TSession::setValue('id'$filter);
  139.             TSession::setValue('id'$data->id);
  140.             
  141.             // put the data back to the form
  142.             $this->form->setData($data);
  143.         }
  144.         
  145.         // redefine the parameters for reload method
  146.         $param=array();
  147.         $param['offset']    =0;
  148.         $param['first_page']=1;
  149.         $this->onReload($param);
  150.     }
  151.     
  152.     /**
  153.      * Load the datagrid with the database objects
  154.      */
  155.     function onReload($param NULL)
  156.     {
  157.         try
  158.         {
  159.             // start database transaction
  160.             TTransaction::open('loja');
  161.             
  162.             // create a repository for City table
  163.             $repository = new TRepository('produtos');
  164.             $limit 10;
  165.             // creates a criteria
  166.             $criteria = new TCriteria;
  167.             $criteria->setProperties($param); // order, offset
  168.             $criteria->setProperty('limit'$limit);
  169.             
  170.             if (TSession::getValue('id_produto'))
  171.             {
  172.                 // filter by city name
  173.                 $criteria->add(TSession::getValue('id'));
  174.             }
  175.             
  176.             // load the objects according to the criteria
  177.             $produtos $repository->load($criteria);
  178.             $this->datagrid->clear();
  179.             if ($produtos)
  180.             {
  181.                 foreach ($produtos as $produto)
  182.                 {
  183.                     // add the objects inside the datagrid
  184.                     $this->datagrid->addItem($produto);
  185.                 }
  186.             }
  187.             
  188.             // clear the criteria
  189.             $criteria->resetProperties();
  190.             $count$repository->count($criteria);
  191.             
  192.             $this->pageNavigation->setCount($count); // count of records
  193.             $this->pageNavigation->setProperties($param); // order, page
  194.             $this->pageNavigation->setLimit($limit); // limit
  195.             
  196.             // commit and closes the database transaction
  197.             TTransaction::close();
  198.             $this->loaded true;
  199.         }
  200.         catch (Exception $e// exceptions
  201.         {
  202.             // show the error message
  203.             new TMessage('error''<b>Erro</b> ' $e->getMessage());
  204.             // undo all pending operations
  205.             TTransaction::rollback();
  206.         }
  207.     }
  208.     
  209.     /**
  210.      * Executed when the user chooses the record
  211.      */
  212.     function onSelect($param)
  213.     {
  214.         try
  215.         {
  216.             $key $param['key'];
  217.             TTransaction::open('loja');
  218.             
  219.             // load the active record
  220.             $produtos = new Produtos($key);
  221.           
  222.             // closes the transaction
  223.             TTransaction::close();
  224.             
  225.             $object = new StdClass;
  226.             $object->produto_id1   $produtos->id;
  227.             $object->produto_name1 $produtos->descricao;
  228.             
  229.             TForm::sendData('form_seek_produto'$object);
  230.             parent::closeWindow(); // closes the window
  231.         }
  232.         catch (Exception $e// em caso de exceção
  233.         {
  234.             // clear fields
  235.             $object = new StdClass;
  236.             $object->produto_id1   '';
  237.             $object->produro_name1 '';
  238.             TForm::sendData('form_seek_produto'$object);
  239.             
  240.             // undo pending operations
  241.             TTransaction::rollback();
  242.         }
  243.     }
  244.     
  245.     /**
  246.      * Shows the page
  247.      */
  248.     function show()
  249.     {
  250.         // if the datagrid was not loaded yet
  251.         if (!$this->loaded)
  252.         {
  253.             $this->onReload();
  254.         }
  255.         parent::show();
  256.     }
  257. }
  258. ?>



Curso completo Meu Negócio Pronto
Use para si, ou transforme em um negócio: Inclui aulas e códigos-fontes
Gestor de conteúdo (SITE) + Loja Virtual (E-Commerce) + Emissor de Notas para infoprodutos


Meu negócio pronto Quero me inscrever agora!

Comentários (2)


PD

Paulo,

Como não temos o mesmo ambiente que você (tabelas, arquivos), você terá de ser mais específico na pergunta ;-)

abs,
Pablo
AS

posta a UML e o descritivo do quer fazer