Filtrar DataGrid Automaticamente... Salve Galera Adianti... Estou com um problema que parece ser simples mas já procurei no livro e nos fóruns e ainda não consegui resolver... Tenho um formulário de Cadastro de pessoa (membro) e um outro para cadastrar os vários e-mail's desta mesma pessoa. Então no formulário de cadastro funciona tudo certinho só que a DataGrid lista todos as pessoas com seus respectivos e-mails enquanto qu...
CC
Filtrar DataGrid Automaticamente...  
Salve Galera Adianti... Estou com um problema que parece ser simples mas já procurei no livro e nos fóruns e ainda não consegui resolver... Tenho um formulário de Cadastro de pessoa (membro) e um outro para cadastrar os vários e-mail's desta mesma pessoa. Então no formulário de cadastro funciona tudo certinho só que a DataGrid lista todos as pessoas com seus respectivos e-mails enquanto que eu queria que somente aparecesse os e-mail da pessoa selecionada na combo do formulário de cadastro...

segue o código para ver se alguém pode me ajudar...

 
  1. <?php
  2. /**
  3. * forCadEmail Form List
  4. * @author <your name here>
  5. */
  6. class forCadEmail extends TPage
  7. {
  8. protected $form; // form
  9. protected $datagrid; // datagrid
  10. protected $pageNavigation;
  11. protected $loaded;
  12. /**
  13. * Form constructor
  14. * @param $param Request
  15. */
  16. public function __construct( $param )
  17. {
  18. parent::__construct();
  19. // creates the form
  20. $this->form = new TQuickForm('form_MEmail');
  21. $this->form->class = 'tform'; // change CSS class
  22. $this->form = new BootstrapFormWrapper($this->form);
  23. $this->form->style = 'display: table;width:100%'; // change style
  24. $this->form->setFormTitle('MEmail');
  25. // create the form fields
  26. $id = new TEntry('id');
  27. $memMembro = new TDBCombo('memMembro','consimpro','mpessoa','mpeNome','mpeNome','mpeNome');
  28. $memEmail = new TEntry('memEmail');
  29. // add the fields
  30. $this->form->addQuickField('Id', $id, 100 );
  31. $this->form->addQuickField('Pessoa', $memMembro, 200 , new TRequiredValidator);
  32. $this->form->addQuickField('E-mail', $memEmail, 200 , new TRequiredValidator);
  33. $memEmail->addValidation('E-mail', new TEmailValidator);
  34. if (!empty($id))
  35. {
  36. $id->setEditable(FALSE);
  37. }
  38. /** samples
  39. $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  40. $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  41. $fieldX->setSize( 100, 40 ); // set size
  42. **/
  43. // create the form actions
  44. $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
  45. $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
  46. // creates a Datagrid
  47. $this->datagrid = new TDataGrid;
  48. ##LIST_DECORATOR##
  49. $this->datagrid->style = 'width: 100%';
  50. $this->datagrid->setHeight(320);
  51. // $this->datagrid->datatable = 'true';
  52. // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  53. // creates the datagrid columns
  54. $column_id = new TDataGridColumn('id', 'Id', 'left');
  55. $column_memMembro = new TDataGridColumn('memMembro', 'Pessoa', 'left');
  56. $column_memEmail = new TDataGridColumn('memEmail', 'E-mail', 'left');
  57. // add the columns to the DataGrid
  58. ///$this->datagrid->addColumn($column_id);
  59. $this->datagrid->addColumn($column_memMembro);
  60. $this->datagrid->addColumn($column_memEmail);
  61. // creates two datagrid actions
  62. $action1 = new TDataGridAction(array($this, 'onEdit'));
  63. $action1->setUseButton(TRUE);
  64. $action1->setButtonClass('btn btn-default');
  65. $action1->setLabel(_t('Edit'));
  66. $action1->setImage('fa:pencil-square-o blue fa-lg');
  67. $action1->setField('id');
  68. $action2 = new TDataGridAction(array($this, 'onDelete'));
  69. $action2->setUseButton(TRUE);
  70. $action2->setButtonClass('btn btn-default');
  71. $action2->setLabel(_t('Delete'));
  72. $action2->setImage('fa:trash-o red fa-lg');
  73. $action2->setField('id');
  74. // add the actions to the datagrid
  75. $this->datagrid->addAction($action1);
  76. $this->datagrid->addAction($action2);
  77. // create the datagrid model
  78. $this->datagrid->createModel();
  79. // creates the page navigation
  80. $this->pageNavigation = new TPageNavigation;
  81. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  82. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  83. // vertical box container
  84. $container = new TVBox;
  85. $container->style = 'width: 90%';
  86. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  87. $container->add(TPanelGroup::pack('Title', $this->form));
  88. $container->add($this->datagrid);
  89. $container->add($this->pageNavigation);
  90. parent::add($container);
  91. }
  92. /**
  93. * Load the datagrid with data
  94. */
  95. public function onReload($param = NULL)
  96. {
  97. try
  98. {
  99. // open a transaction with database 'consimpro'
  100. TTransaction::open('consimpro');
  101. // creates a repository for MEmail
  102. $repository = new TRepository('MEmail');
  103. $limit = 10;
  104. // creates a criteria
  105. $criteria = new TCriteria;
  106. // default order
  107. if (empty($param['order']))
  108. {
  109. $param['order'] = 'id';
  110. $param['direction'] = 'asc';
  111. }
  112. $criteria->setProperties($param); // order, offset
  113. $criteria->setProperty('limit', $limit);
  114. if (TSession::getValue('MEmail_filter'))
  115. {
  116. // add the filter stored in the session to the criteria
  117. $criteria->add(TSession::getValue('MEmail_filter'));
  118. }
  119. // load the objects according to criteria
  120. $objects = $repository->load($criteria, FALSE);
  121. $this->datagrid->clear();
  122. if ($objects)
  123. {
  124. // iterate the collection of active records
  125. foreach ($objects as $object)
  126. {
  127. // add the object inside the datagrid
  128. $this->datagrid->addItem($object);
  129. }
  130. }
  131. // reset the criteria for record count
  132. $criteria->resetProperties();
  133. $count= $repository->count($criteria);
  134. $this->pageNavigation->setCount($count); // count of records
  135. $this->pageNavigation->setProperties($param); // order, page
  136. $this->pageNavigation->setLimit($limit); // limit
  137. // close the transaction
  138. TTransaction::close();
  139. $this->loaded = true;
  140. }
  141. catch (Exception $e) // in case of exception
  142. {
  143. // shows the exception error message
  144. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  145. // undo all pending operations
  146. TTransaction::rollback();
  147. }
  148. }
  149. /**
  150. * Ask before deletion
  151. */
  152. public function onDelete($param)
  153. {
  154. // define the delete action
  155. $action = new TAction(array($this, 'Delete'));
  156. $action->setParameters($param); // pass the key parameter ahead
  157. // shows a dialog to the user
  158. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  159. }
  160. /**
  161. * Delete a record
  162. */
  163. public function Delete($param)
  164. {
  165. try
  166. {
  167. $key=$param['key']; // get the parameter $key
  168. TTransaction::open('consimpro'); // open a transaction with database
  169. $object = new MEmail($key, FALSE); // instantiates the Active Record
  170. $object->delete(); // deletes the object from the database
  171. TTransaction::close(); // close the transaction
  172. $this->onReload( $param ); // reload the listing
  173. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted')); // success message
  174. }
  175. catch (Exception $e) // in case of exception
  176. {
  177. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  178. TTransaction::rollback(); // undo all pending operations
  179. }
  180. }
  181. /**
  182. * Save form data
  183. * @param $param Request
  184. */
  185. public function onSave( $param )
  186. {
  187. try
  188. {
  189. TTransaction::open('consimpro'); // open a transaction
  190. /**
  191. // Enable Debug logger for SQL operations inside the transaction
  192. TTransaction::setLogger(new TLoggerSTD); // standard output
  193. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  194. **/
  195. $this->form->validate(); // validate form data
  196. $object = new MEmail; // create an empty object
  197. $data = $this->form->getData(); // get form data as array
  198. $object->fromArray( (array) $data); // load the object with data
  199. $object->store(); // save the object
  200. // get the generated id
  201. $data->id = $object->id;
  202. $this->form->setData($data); // fill form data
  203. TTransaction::close(); // close the transaction
  204. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved')); // success message
  205. $this->onReload(); // reload the listing
  206. }
  207. catch (Exception $e) // in case of exception
  208. {
  209. new TMessage('error', $e->getMessage()); // shows the exception error message
  210. $this->form->setData( $this->form->getData() ); // keep form data
  211. TTransaction::rollback(); // undo all pending operations
  212. }
  213. }
  214. /**
  215. * Clear form data
  216. * @param $param Request
  217. */
  218. public function onClear( $param )
  219. {
  220. $this->form->clear();
  221. }
  222. /**
  223. * Load object to form data
  224. * @param $param Request
  225. */
  226. public function onEdit( $param )
  227. {
  228. try
  229. {
  230. if (isset($param['key']))
  231. {
  232. $key = $param['key']; // get the parameter $key
  233. TTransaction::open('consimpro'); // open a transaction
  234. $object = new MEmail($key); // instantiates the Active Record
  235. $this->form->setData($object); // fill the form
  236. TTransaction::close(); // close the transaction
  237. }
  238. else
  239. {
  240. $this->form->clear();
  241. }
  242. }
  243. catch (Exception $e) // in case of exception
  244. {
  245. new TMessage('error', $e->getMessage()); // shows the exception error message
  246. TTransaction::rollback(); // undo all pending operations
  247. }
  248. }
  249. /**
  250. * method show()
  251. * Shows the page
  252. */
  253. public function show()
  254. {
  255. // check if the datagrid is already loaded
  256. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  257. {
  258. $this->onReload( func_get_arg(0) );
  259. }
  260. parent::show();
  261. }
  262. }
  263. ?>

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 (3)


CC

Continuo com o mesmo problema... deve mesmo ser muito difícil... já estou quase pagando por uma solução...
PD

Não entendi a pergunta.
CC

Na prática a questão se resume a um filtro que apresente na data grid somente os dados com base em um TDBCombo do formulário de cadastro. Ou seja se eu cadastrar um e-mail para o João e daqui a dois meses for incluir mais um e-mail, quando eu selecionar o joão na TDBCombo, será filtrado os emails que o joão tem cadastrado... no caso um e-mail cadastrado anteriormente... não sei se consegui esclarecer melhor.