Conheça as melhorias da versão 8.0, 8.1, 8.2!
Clique aqui para saber mais
Como filtrar data da Busca para o datagrid Olá, Galera! Peço ajuda dos colegas, preciso parar a data da busca para o datagrid. ...
JA
Como filtrar data da Busca para o datagrid  
Olá, Galera!

Peço ajuda dos colegas, preciso parar a data da busca para o datagrid.

 
  1. <?php
  2. /**
  3. * ExemploCheckList Listing
  4. * @author <your name here>
  5. */
  6. class SystemAlunoFrequenciaUpdate extends TPage
  7. {
  8. private $form; // form
  9. private $datagrid; // listing
  10. private $pageNavigation;
  11. private $formgrid;
  12. private $loaded;
  13. private $deleteButton;
  14. private static $database = 'db_guiansoft';
  15. private static $activeRecord = 'SystemAlunoTurma';
  16. private static $primaryKey = 'id';
  17. private static $formName = 'form_system_aluno_frequencia';
  18. /**
  19. * Class constructor
  20. * Creates the page, the form and the listing
  21. */
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. // creates the form
  26. $this->form = new BootstrapFormBuilder(self::$formName);
  27. // define the form title
  28. $this->form->setFormTitle('Frequência');
  29. $id = new TEntry('id');
  30. $periodo_id = new TDBCombo('periodo_id', 'db_guiansoft', 'SystemPeriodo', 'id', 'nome');
  31. $curso_id = new TDBCombo('curso_id', 'db_guiansoft', 'SystemCurso', 'id', 'nome');
  32. $modulo_id = new TDBCombo('modulo_id', 'db_guiansoft', 'SystemModulo', 'id', 'nome');
  33. $turma_id = new TDBCombo('turma_id', 'db_guiansoft', 'SystemTurma', 'id', 'nome');
  34. $situacao_id = new TDBCombo('situacao_id', 'db_guiansoft', 'SystemSituacaoAlunoTurma', 'id', 'nome');
  35. $data_frequencia = new TDate('data_frequencia');
  36. $id->setSize(100);
  37. $curso_id->setSize('100%');
  38. $modulo_id->setSize('100%');
  39. $turma_id->setSize('100%');
  40. $situacao_id->setSize('100%');
  41. $data_frequencia->setSize('100%');
  42. $this->form->addFields( [new TLabel(('Período'))], [$periodo_id], [new TLabel(('Curso'))], [$curso_id] );
  43. $this->form->addFields( [new TLabel(('Módulo'))], [$modulo_id], [new TLabel(('Turma'))], [$turma_id] );
  44. $this->form->addFields( [new TLabel(('Situação'))], [$situacao_id], [new TLabel(('Data'))], [$data_frequencia] );
  45. // keep the form filled during navigation with session data
  46. $this->form->setData( TSession::getValue(__CLASS__.'_filter_data') );
  47. $btn_onsearch = $this->form->addAction('Buscar', new TAction([$this, 'onSearch']), 'fa:search');
  48. $btn_onsearch->addStyleClass('btn');
  49. $bt_marcar = $this->form->addAction('Presente', new TAction([$this, 'onSelectedAll']), 'fa:check #ffffff');
  50. $bt_marcar->addFunction("$('input:checkbox').prop('frequencia_check',true);");
  51. $bt_marcar->addStyleClass('btn-success');
  52. $bt_desmarcar = $this->form->addAction('Faltou', new TAction([$this, 'offSelectedAll']), 'fa:check #ffffff');
  53. $bt_desmarcar->addFunction("$('input:checkbox').prop('frequencia_check',false);");
  54. $bt_desmarcar->addStyleClass('btn-danger');
  55. $btn_onsave = $this->form->addAction('Salvar', new TAction([$this, 'onSearch']), 'fa:floppy-o #ffffff');
  56. $btn_onsave->addStyleClass('btn-primary');
  57. // creates a Datagrid
  58. $this->datagrid = new TDataGrid;
  59. $this->datagrid = new BootstrapDatagridWrapper($this->datagrid);
  60. $this->datagrid->style = 'width: 100%';
  61. $this->datagrid->setHeight(320);
  62. $column_id = new TDataGridColumn('id', 'Id', 'center' , '70px');
  63. $column_aluno_id = new TDataGridColumn('SystemAluno->name', 'Aluno', 'left');
  64. $column_data_frequencia = new TDataGridColumn('data_frequencia', 'Data', 'left');
  65. $column_frequencia_check = new TDataGridColumn('frequencia_check', 'Frequência', 'left');
  66. $column_frequencia_check->setTransformer( function($value, $object, $row) {
  67. $frequencia_check = new TCheckButton('check_'.$object->id);
  68. $frequencia_check->setSize('70%');
  69. $frequencia_check->setFormName(self::$formName);
  70. $frequencia_check->setValue($value);
  71. $frequencia_check->setIndexValue('on');
  72. $frequencia_check->onchange = 'mudarCor(this)';
  73. $this->formgrid->addField($frequencia_check); // important
  74. return $frequencia_check;
  75. });
  76. $order_id = new TAction(array($this, 'onReload'));
  77. $order_id->setParameter('order', 'id');
  78. $column_id->setAction($order_id);
  79. $this->datagrid->addColumn($column_id);
  80. $this->datagrid->addColumn($column_aluno_id);
  81. $this->datagrid->addColumn($column_data_frequencia);
  82. $this->datagrid->addColumn($column_frequencia_check);
  83. // create the datagrid model
  84. $this->datagrid->createModel();
  85. // creates the page navigation
  86. $this->pageNavigation = new TPageNavigation;
  87. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  88. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  89. $this->datagrid->disableDefaultClick();
  90. // put datagrid inside a form
  91. $this->formgrid = new TForm;
  92. $this->formgrid->add($this->datagrid);
  93. $panel = new TPanelGroup;
  94. $panel->add($this->formgrid);
  95. $panel->addFooter($this->pageNavigation);
  96. // vertical box container
  97. $container = new TVBox;
  98. $container->style = 'width: 100%';
  99. $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  100. $container->add($this->form);
  101. $container->add($panel);
  102. parent::add($container);
  103. }
  104. /**
  105. * method onSelectedAll()
  106. * Executed when the user clicks at the marcar todos button
  107. */
  108. function onSelectedAll()
  109. {
  110. $data = $this->form->getData();
  111. if($data->aluno_id != '')
  112. {
  113. $data->frequencia_check = 'true';
  114. TButton::enableField('form_system_aluno_frequencia','bt_desmarcar');
  115. //$this->onReload($data);
  116. }
  117. }
  118. /**
  119. * method offSelectedAll()
  120. * Executed when the user clicks at the desmarcar todos button
  121. */
  122. function offSelectedAll()
  123. {
  124. $data = $this->form->getData();
  125. if($data->aluno_id != '')
  126. {
  127. $data->frequencia_check = 'false';
  128. TButton::disableField('form_system_aluno_frequencia','bt_desmarcar');
  129. //$this->onReload($data);
  130. }
  131. }
  132. public static function onSave($param)
  133. {
  134. $frequencia_check = $param['_field_frequencia_check'];
  135. $value = $param['_field_value'];
  136. $column = $param['column'];
  137. $parts = explode('_', $frequencia_check);
  138. $id = end($parts);
  139. try
  140. {
  141. // open transaction
  142. TTransaction::open('db_guiansoft');
  143. $class = self::$activeRecord;
  144. $object = $class::find($id);
  145. if ($object)
  146. {
  147. $object->$column = $value;
  148. $object->store();
  149. }
  150. // close transaction
  151. TTransaction::close();
  152. }
  153. catch (Exception $e)
  154. {
  155. // show the exception message
  156. new TMessage('error', $e->getMessage());
  157. }
  158. }
  159. /**
  160. * Register the filter in the session
  161. */
  162. public function onSearch()
  163. {
  164. // get the search form data
  165. $data = $this->form->getData();
  166. $filters = [];
  167. TSession::setValue(__CLASS__.'_filter_data', NULL);
  168. TSession::setValue(__CLASS__.'_filters', NULL);
  169. if (isset($data->id) AND ($data->id))
  170. {
  171. $filters[] = new TFilter('id', '=', $data->id);// create the filter
  172. }
  173. if (isset($data->periodo_id) AND ($data->periodo_id))
  174. {
  175. $filters[] = new TFilter('periodo_id', 'like', "%{$data->periodo_id}%");// create the filter
  176. }
  177. if (isset($data->curso_id) AND ($data->curso_id))
  178. {
  179. $filters[] = new TFilter('curso_id', 'like', "%{$data->curso_id}%");// create the filter
  180. }
  181. if (isset($data->modulo_id) AND ($data->modulo_id))
  182. {
  183. $filters[] = new TFilter('modulo_id', 'like', "%{$data->modulo_id}%");// create the filter
  184. }
  185. if (isset($data->turma_id) AND ($data->turma_id))
  186. {
  187. $filters[] = new TFilter('turma_id', 'like', "%{$data->turma_id}%");// create the filter
  188. }
  189. if (isset($data->situacao_id) AND ($data->situacao_id))
  190. {
  191. $filters[] = new TFilter('situacao_id', 'like', "%{$data->situacao_id}%");// create the filter
  192. }
  193. // fill the form with data again
  194. $this->form->setData($data);
  195. // keep the search data in the session
  196. TSession::setValue(__CLASS__.'_filter_data', $data);
  197. TSession::setValue(__CLASS__.'_filters', $filters);
  198. $param=array();
  199. $param['offset'] =0;
  200. $param['first_page']=1;
  201. $this->onReload($param);
  202. }
  203. /**
  204. * Load the datagrid with data
  205. */
  206. public function onReload($param = NULL)
  207. {
  208. try
  209. {
  210. // open a transaction with database 'exemplo1'
  211. TTransaction::open(self::$database);
  212. // creates a repository for CheckList
  213. $repository = new TRepository(self::$activeRecord);
  214. $limit = 20;
  215. // creates a criteria
  216. $criteria = new TCriteria;
  217. if (empty($param['order']))
  218. {
  219. $param['order'] = 'id';
  220. }
  221. if (empty($param['direction']))
  222. {
  223. $param['direction'] = 'desc';
  224. }
  225. $criteria->setProperties($param); // order, offset
  226. $criteria->setProperty('limit', $limit);
  227. if($filters = TSession::getValue(__CLASS__.'_filters'))
  228. {
  229. foreach ($filters as $filter)
  230. {
  231. $criteria->add($filter);
  232. }
  233. }
  234. // load the objects according to criteria
  235. $objects = $repository->load($criteria, FALSE);
  236. $this->datagrid->clear();
  237. if ($objects)
  238. {
  239. // iterate the collection of active records
  240. foreach ($objects as $object)
  241. {
  242. // add the object inside the datagrid
  243. $this->datagrid->addItem($object);
  244. }
  245. }
  246. // reset the criteria for record count
  247. $criteria->resetProperties();
  248. $count= $repository->count($criteria);
  249. $this->pageNavigation->setCount($count); // count of records
  250. $this->pageNavigation->setProperties($param); // order, page
  251. $this->pageNavigation->setLimit($limit); // limit
  252. // close the transaction
  253. TTransaction::close();
  254. $this->loaded = true;
  255. }
  256. catch (Exception $e) // in case of exception
  257. {
  258. // shows the exception error message
  259. new TMessage('error', $e->getMessage());
  260. // undo all pending operations
  261. TTransaction::rollback();
  262. }
  263. }
  264. public function onShow()
  265. {
  266. }
  267. /**
  268. * method show()
  269. * Shows the page
  270. */
  271. public function show()
  272. {
  273. // frequencia_check if the datagrid is already loaded
  274. if (!$this->loaded AND (!isset($_GET['method']) OR !(in_array($_GET['method'], array('onReload', 'onSearch')))) )
  275. {
  276. if (func_num_args() > 0)
  277. {
  278. $this->onReload( func_get_arg(0) );
  279. }
  280. else
  281. {
  282. $this->onReload();
  283. }
  284. }
  285. parent::show();
  286. }
  287. }
  288. ?>


Att,
Jonathas Alves

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


NR

Consegue detalhar um pouco mais? Não entendi exatamente o que você quer fazer
JA

Olá, Nataniel!

SystemAlunoFrequenciaUpdate faz uma consulta:

Período, Curso, Módulo, Turma, Situação e Data;
OBS: Situação = situação do aluno na turma que pode ser cursando, cancelado, transferido, reprovado ou aprovado.

Data: data do registro da frequencia.

Essas informações para consulta estão na SystemAlunoTurma que retorna no list da SystemAlunoFrequenciaUpdate.

então queria armazenar para cada aluno que esta no list essas informações e se ele esta presente ou faltou.

tem dois botoes um presente em verde e faltou em vermelho ao clicar posso marcar todos como presente ou como faltou.

e em seguida gravo essas informações em SystemAlunoFrequencia.

essa é a ideia

Att,
Jonathas Alves

NR

Quando você clica em Buscar, a função onSearch guarda os dados dos formulário na sessão:
 
  1. <?php
  2. // onSearch
  3. $data = $this->form->getData();
  4. TSession::setValue(__CLASS__.'_filter_data', $data);
  5. ?>

Com isso você consegue recuperar a data em outras funções da classe, como a onReload, onSelectedAll, etc
JA

Olá, Nataniel!
Obrigado pela atenção

Retorna o campo data_frequencia null
object(SystemAlunoTurma)#114 (9) { ["system_aluno":"SystemAlunoTurma":private]=> NULL ["system_periodo":"SystemAlunoTurma":private]=> NULL ["system_curso":"SystemAlunoTurma":private]=> NULL ["system_modulo":"SystemAlunoTurma":private]=> NULL ["system_turma":"SystemAlunoTurma":private]=> NULL ["system_situacao_turma":"SystemAlunoTurma":private]=> NULL ["data":protected]=> array(9) { ["id"]=> string(1) "2" ["matricula"]=> string(8) "20180001" ["aluno_turma_id"]=> string(1) "2" ["periodo_id"]=> string(1) "5" ["curso_id"]=> string(1) "2" ["modulo_id"]=> string(1) "2" ["turma_id"]=> string(1) "2" ["situacao_id"]=> string(1) "5" ["data_frequencia"]=> NULL } ["vdata":protected]=> NULL ["attributes":protected]=> array(0) { } } object(SystemAlunoTurma)#113 (9) { ["system_aluno":"SystemAlunoTurma":private]=> NULL ["system_periodo":"SystemAlunoTurma":private]=> NULL ["system_curso":"SystemAlunoTurma":private]=> NULL ["system_modulo":"SystemAlunoTurma":private]=> NULL ["system_turma":"SystemAlunoTurma":private]=> NULL ["system_situacao_turma":"SystemAlunoTurma":private]=> NULL ["data":protected]=> array(9) { ["id"]=> string(1) "3" ["matricula"]=> string(8) "20180001" ["aluno_turma_id"]=> string(1) "1" ["periodo_id"]=> string(1) "5" ["curso_id"]=> string(1) "2" ["modulo_id"]=> string(1) "2" ["turma_id"]=> string(1) "2" ["situacao_id"]=> string(1) "5" ["data_frequencia"]=> NULL } ["vdata":protected]=> NULL ["attributes":protected]=> array(0) { } }



Fiz essa alteração no código, mais ainda não estou conseguindo pegar o valor da data da frequência.
 
  1. <?php
  2. /**
  3. * Register the filter in the session
  4. */
  5. public function onSearch()
  6. {
  7. // get the search form data
  8. $data = $this->form->getData();
  9. // clear session filters
  10. TSession::setValue('_filter_perido', NULL);
  11. TSession::setValue('_filter_curso', NULL);
  12. TSession::setValue('_filter_modulo', NULL);
  13. TSession::setValue('_filter_turma', NULL);
  14. TSession::setValue('_filter_situacao', NULL);
  15. if (isset($data->periodo_id) AND ($data->periodo_id)) {
  16. $filter = new TFilter('periodo_id', 'like', "%{$data->periodo_id}%"); // create the filter
  17. TSession::setValue('_filter_periodo', $filter); // stores the filter in the session
  18. }
  19. if (isset($data->curso_id) AND ($data->curso_id)) {
  20. $filter = new TFilter('curso_id', 'like', "%{$data->curso_id}%"); // create the filter
  21. TSession::setValue('_filter_curso', $filter); // stores the filter in the session
  22. }
  23. if (isset($data->modulo_id) AND ($data->modulo_id)) {
  24. $filter = new TFilter('modulo_id', 'like', "%{$data->modulo_id}%"); // create the filter
  25. TSession::setValue('_filter_modulo', $filter); // stores the filter in the session
  26. }
  27. if (isset($data->turma_id) AND ($data->turma_id)) {
  28. $filter = new TFilter('turma_id', 'like', "%{$data->turma_id}%"); // create the filter
  29. TSession::setValue('_filter_turma', $filter); // stores the filter in the session
  30. }
  31. if (isset($data->situacao_id) AND ($data->situacao_id)) {
  32. $filter = new TFilter('situacao_id', 'like', "%{$data->situacao_id}%"); // create the filter
  33. TSession::setValue('_filter_situacao', $filter); // stores the filter in the session
  34. }
  35. // fill the form with data again
  36. $this->form->setData($data);
  37. // keep the search data in the session
  38. TSession::setValue('_filter_data', $data);
  39. $param=array();
  40. $param['offset'] =0;
  41. $param['first_page']=1;
  42. $this->onReload($param);
  43. }
  44. /**
  45. * Load the datagrid with data
  46. */
  47. public function onReload($param = NULL)
  48. {
  49. try
  50. {
  51. // open a transaction with database 'samples'
  52. TTransaction::open('db_guiansoft');
  53. TTransaction::setLogger(new TLoggerSTD); // standard output
  54. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  55. // creates a repository for Prospeccao
  56. $repository = new TRepository('SystemAlunoTurma');
  57. $limit = 10;
  58. // creates a criteria
  59. $criteria = new TCriteria;
  60. // default order
  61. if (empty($param['order']))
  62. {
  63. $param['order'] = 'id';
  64. $param['direction'] = 'asc';
  65. }
  66. $criteria->setProperties($param); // order, offset
  67. $criteria->setProperty('limit', $limit);
  68. if (TSession::getValue('_filter_periodo')) {
  69. $criteria->add(TSession::getValue('_filter_periodo')); // add the session filter
  70. }
  71. if (TSession::getValue('_filter_curso')) {
  72. $criteria->add(TSession::getValue('_filter_curso')); // add the session filter
  73. }
  74. if (TSession::getValue('_filter_modulo')) {
  75. $criteria->add(TSession::getValue('_filter_modulo')); // add the session filter
  76. }
  77. if (TSession::getValue('_filter_turma')) {
  78. $criteria->add(TSession::getValue('_filter_turma')); // add the session filter
  79. }
  80. if (TSession::getValue('_filter_situacao')) {
  81. $criteria->add(TSession::getValue('_filter_situacao')); // add the session filter
  82. }
  83. // load the objects according to criteria
  84. $objects = $repository->load($criteria, FALSE);
  85. if (is_callable($this->transformCallback))
  86. {
  87. call_user_func($this->transformCallback, $objects, $param);
  88. }
  89. $this->datagrid->clear();
  90. if ($objects)
  91. {
  92. // iterate the collection of active records
  93. foreach ($objects as $object)
  94. {
  95. $object->data_frequencia = TDate::date2br($object->data_frequencia);
  96. // add the object inside the datagrid
  97. $this->datagrid->addItem($object);
  98. var_dump($object);
  99. }
  100. }
  101. // reset the criteria for record count
  102. $criteria->resetProperties();
  103. $count= $repository->count($criteria);
  104. $this->pageNavigation->setCount($count); // count of records
  105. $this->pageNavigation->setProperties($param); // order, page
  106. $this->pageNavigation->setLimit($limit); // limit
  107. // close the transaction
  108. TTransaction::close();
  109. $this->loaded = true;
  110. }
  111. catch (Exception $e) // in case of exception
  112. {
  113. // shows the exception error message
  114. new TMessage('error', $e->getMessage());
  115. // undo all pending operations
  116. TTransaction::rollback();
  117. }
  118. }
  119. ?>
NR

Jonathas, você está fazendo um var_dump da variável $object, que é uma instância da classe SystemAlunoTurma. Nessa variável você tem os valores de cada registro gravados no banco de dados. Se quiser adicionar outras informaçõe que não estejam no banco de dados, como uma data do formulário, você vai precisar especificar isso:
 
  1. <?php
  2. // iterate the collection of active records
  3. $dados_sessao = TSession::getValue(__CLASS__.'_filter_data');
  4. foreach ($objects as $object)
  5. {
  6. // $object->data_frequencia = TDate::date2br($object->data_frequencia); data frequencia nao existe ou é nula no banco de dados
  7. $object->data_frequencia = $dados_sessao->data_frequencia; // pega valor do campo do formulario que foi salvo na sessao apos clicar em Buscar e chamar a funcao onSearch
  8. // add the object inside the datagrid
  9. $this->datagrid->addItem($object);
  10. var_dump($object);
  11. }
  12. ?>