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. function __construct()
  10. {
  11. parent::__construct();
  12. new TSession();
  13. //Cria os containers
  14. $notebook = new TNotebook(500,200);
  15. $this->form = new TForm('form_vendas');
  16. $table = new TTable;
  17. $this->form->add($table);
  18. $notebook->appendPage('Vendas de Produtos', $this->form);
  19. // Cria o componente de busca
  20. $id = new TSeekButton('id');
  21. $id->setValue(TSession::getValue('id_produto'));
  22. $codigo = new TEntry('codigo');
  23. $obj = new TStandardSeek;
  24. $action = new TAction(array($obj, 'onSetup'));
  25. $action->setParameter('database', 'loja');
  26. $action->setParameter('parent', 'form_vendas');
  27. $action->setParameter('model', 'produtos');
  28. $action->setParameter('display_field', 'codigo');
  29. $action->setParameter('receive_key', 'id');
  30. $action->setParameter('receive_field', 'codigo');
  31. $id->setAction($action);
  32. $row=$table->addRow();
  33. $row->addCell(new TLabel('Produto'));
  34. $row->addCell($id);
  35. $row=$table->addRow();
  36. $row->addCell(new TLabel('Código'));
  37. $row->addCell($codigo);
  38. $carrega_button = new TButton('load');
  39. $carrega_button->setAction(new TAction(array($this,'onReload')),'Carregar Produtos');
  40. $row=$table->addRow();
  41. $row->addCell($carrega_button);
  42. $this->form->setFields(array($id,$codigo,$carrega_button));
  43. // Cria o datagrid
  44. $this->datagrid = new TDataGrid();
  45. $this->datagrid->setHeight(400);
  46. $codigo = new TDataGridColumn('codigo','Código','left', 30);
  47. $descricao = new TDataGridColumn('descricao','Descrição','left',200);
  48. $valnor = new TDataGridColumn('valnor','Normal',70);
  49. $valpro = new TDataGridColumn('valpro','Promoção',70);
  50. $valult = new TDataGridColumn('valult','Último',70);
  51. $this->datagrid->addColumn($codigo);
  52. $this->datagrid->addColumn($descricao);
  53. $this->datagrid->addColumn($valnor);
  54. $this->datagrid->addColumn($valpro);
  55. $this->datagrid->addColumn($valult);
  56. $this->datagrid->createModel();
  57. // add the form to the page
  58. $table = new TTable();
  59. $table->addRow()->addCell($this->form);
  60. $table->addRow()->addCell($this->datagrid);
  61. //$table->addRow()->addCell($this->pageNavigation);
  62. parent::add($table);
  63. }
  64. function onReload()
  65. {
  66. try {
  67. $data = $this->form->getData();
  68. TTransaction::open('loja');
  69. $conn = TTransaction::get();
  70. $repository = new TRepository('produtos');
  71. $limit=10;
  72. $criteria = new TCriteria();
  73. $criteria = $data;
  74. $produtos = $repository->load($criteria);
  75. // limpa datagrid
  76. $this->datagrid->clear();
  77. if ($produtos)
  78. {
  79. foreach ($produtos as $produto)
  80. {
  81. $this->datagrid->addItem($produto);
  82. }
  83. }
  84. $criteria->resetProperties();
  85. $count= $repository->count($criteria);
  86. //$this->pageNavigation->setCount($count);
  87. //$this->pageNavigation->setProperties($param);
  88. //$this->pageNavigation->setLimit($limit);
  89. TTransaction::close();
  90. $this->loaded = true;
  91. }
  92. catch (Exception $e)
  93. {
  94. new TMessage('error', '<b>Error</b>' . $e->getMessage());
  95. TTransaction::rollback();
  96. }
  97. }
  98. /*
  99. function show()
  100. {
  101. if (!$this->loaded)
  102. {
  103. $this->onReload();
  104. }
  105. parent::show();
  106. }
  107. */
  108. }
  109. ?>

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



Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework 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