Como implementar ordenação por vários campos Caros, amigos, boa noite! Alguém pode me ajudar com a ordenação de um datagrid em um formulário FormList, vejam no anexo uma consulta em minha base de dados a ordenação funciona e quando seto na propriedade criteria do form não está respeitando a ordenação. Abaixo o código fonte do meu form. Alguém já passou por isso? Ou poderiam me ajudar? ...
LB
Como implementar ordenação por vários campos  
Fechado
Caros, amigos, boa noite!

Alguém pode me ajudar com a ordenação de um datagrid em um formulário FormList, vejam no anexo uma consulta em minha base de dados a ordenação funciona e quando seto na propriedade criteria do form não está respeitando a ordenação. Abaixo o código fonte do meu form. Alguém já passou por isso? Ou poderiam me ajudar?

 
  1. <?php e
  2. /**
  3. * AgendaFormList Registration
  4. * @author <your name here>
  5. */
  6. class AgendaFormList extends TPage
  7. {
  8. protected $form; // form
  9. protected $datagrid; // datagrid
  10. protected $pageNavigation;
  11. protected $loaded;
  12. /**
  13. * Class constructor
  14. * Creates the page and the registration form
  15. */
  16. function __construct()
  17. {
  18. parent::__construct();
  19. // creates the form
  20. $this->form = new TQuickForm('form_Agenda');
  21. $this->form->class = 'tform'; // CSS class
  22. $this->form->setFormTitle('Agenda'); // define the form title
  23. // create the form fields
  24. 2436 = new TEntry('id');
  25. $data_con = new TDate('data_con');
  26. $hora = new TCombo('hora');
  27. $cod_espec = new ">TDBSeekButton('cod_espec','info_clin','form_Agenda','especialidade','descricao','cod_espec','especialidade_descricao');
  28. $especialidade_descricao = new TEntry('especialidade_descricao');
  29. $especialidade_nome_medico = new TEntry('especialidade_nome_medico');
  30. $cod_pac = new ">TDBSeekButton('cod_pac','info_clin','form_Agenda','paciente','nome','cod_pac','paciente_nome');
  31. $paciente_nome = new TEntry('paciente_nome');
  32. $observacao = new TText('observacao');
  33. // add the fields
  34. $this->form->addQuickField('Identificador', 2436, 100);
  35. $this->form->addQuickField('Data', $data_con, 100, new TRequiredValidator );
  36. $this->form->addQuickField('Hora', $hora, 100, new TRequiredValidator );
  37. $this->form->addQuickField('Cód. Especialidade', $cod_espec, 100, new TRequiredValidator );
  38. $this->form->addQuickField('Descrição Especialidade', $especialidade_descricao, 400);
  39. $this->form->addQuickField('Nome Médico', $especialidade_nome_medico, 400);
  40. $this->form->addQuickField('Cód. Paciente', $cod_pac, 100, new TRequiredValidator );
  41. $this->form->addQuickField('Nome Paciente', $paciente_nome, 400);
  42. $this->form->addQuickField('Observação', $observacao, 800);
  43. $observacao->setSize(800, 40);
  44. //carregando os estados para o combo de estado
  45. $itens_hr = array();
  46. $itens_hr['06:00:00'] = '06:00';
  47. $itens_hr['06:30:00'] = '06:30';
  48. $itens_hr['07:00:00'] = '07:00';
  49. $itens_hr['07:30:00'] = '07:30';
  50. $itens_hr['08:00:00'] = '08:00';
  51. $itens_hr['08:30:00'] = '08:30';
  52. $itens_hr['09:00:00'] = '09:00';
  53. $itens_hr['09:30:00'] = '09:30';
  54. $itens_hr['10:00:00'] = '10:00';
  55. $itens_hr['10:30:00'] = '10:30';
  56. $itens_hr['11:00:00'] = '11:00';
  57. $itens_hr['11:30:00'] = '11:30';
  58. $itens_hr['12:00:00'] = '12:00';
  59. $itens_hr['12:30:00'] = '12:30';
  60. $itens_hr['13:00:00'] = '13:00';
  61. $itens_hr['13:30:00'] = '13:30';
  62. $itens_hr['14:00:00'] = '14:00';
  63. $itens_hr['14:30:00'] = '14:30';
  64. $itens_hr['15:00:00'] = '15:00';
  65. $itens_hr['15:30:00'] = '15:30';
  66. $itens_hr['16:00:00'] = '16:00';
  67. $itens_hr['16:30:00'] = '16:30';
  68. $itens_hr['17:00:00'] = '17:00';
  69. $itens_hr['17:30:00'] = '17:30';
  70. $itens_hr['18:00:00'] = '18:00';
  71. $itens_hr['18:30:00'] = '18:30';
  72. $itens_hr['19:00:00'] = '19:00';
  73. $itens_hr['19:30:00'] = '19:30';
  74. $itens_hr['20:00:00'] = '20:00';
  75. $hora->addItems($itens_hr);
  76. //desabilitando campo chave primaria
  77. 2436->setEditable(False);
  78. $paciente_nome->setEditable(False);
  79. $especialidade_descricao->setEditable(False);
  80. $especialidade_nome_medico->setEditable(False);
  81. //setando mascara
  82. $data_con->setMask('dd/mm/yyyy');
  83. //chamando validação
  84. $data_con->addValidation('Data', new TDateConValidator);
  85. //CRIANDO EXIT PARA BUSCA DE NOME DO MÉDICO
  86. $exit_action = New TAction(array($this, 'onMedico'));
  87. $cod_espec->setExitAction($exit_action);
  88. // create the form actions
  89. $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'ico_save.png');
  90. $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onEdit')), 'ico_new.png');
  91. // creates a DataGrid
  92. $this->datagrid = new TQuickGrid;
  93. $this->datagrid->setHeight(320);
  94. // creates the datagrid columns
  95. //2436 = $this->datagrid->addQuickColumn('Identificador', 'id', 'left', 100);
  96. $data_con = $this->datagrid->addQuickColumn('Data', 'data_con_formatada', 'left', 100);
  97. $hora = $this->datagrid->addQuickColumn('Hora', 'hora', 'left', 100);
  98. //$cod_espec = $this->datagrid->addQuickColumn('Cód.Espec', 'cod_espec', 'left', 100);
  99. $paciente_nome = $this->datagrid->addQuickColumn('Paciente', 'paciente_nome', 'left', 400);
  100. $especialidade_descricao = $this->datagrid->addQuickColumn('Especialidade', 'especialidade_descricao', 'left', 400);
  101. $especialidade_nome_medico = $this->datagrid->addQuickColumn('Médico', 'especialidade_nome_medico', 'left', 400);
  102. //$cod_pac = $this->datagrid->addQuickColumn('Paciente', 'cod_pac', 'left', 100);
  103. $observacao = $this->datagrid->addQuickColumn('Observação', 'observacao', 'left', 800);
  104. // create the datagrid actions
  105. $edit_action = new TDataGridAction(array($this, 'onEdit'));
  106. $delete_action = new TDataGridAction(array($this, 'onDelete'));
  107. // add the actions to the datagrid
  108. $this->datagrid->addQuickAction(_t('Edit'), $edit_action, 'id', 'ico_edit.png');
  109. $this->datagrid->addQuickAction(_t('Delete'), $delete_action, 'id', 'ico_delete.png');
  110. // create the datagrid model
  111. $this->datagrid->createModel();
  112. // creates the page navigation
  113. $this->pageNavigation = new TPageNavigation;
  114. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  115. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  116. // create the datagrid model
  117. $this->datagrid->createModel();
  118. // creates the page navigation
  119. $this->pageNavigation = new TPageNavigation;
  120. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  121. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  122. // create the page container
  123. $container = TVBox::pack( $this->form, $this->datagrid, $this->pageNavigation);
  124. parent::add($container);
  125. }
  126. /**
  127. * method onReload()
  128. * Load the datagrid with the database objects
  129. */
  130. function onReload($param = NULL)
  131. {
  132. try
  133. {
  134. // open a transaction with database 'info_clin'
  135. TTransaction::open('info_clin');
  136. // creates a repository for Agenda
  137. $repository = new TRepository('Agenda');
  138. $limit = 10;
  139. // creates a criteria
  140. $criteria = new TCriteria;
  141. // default order
  142. if (empty($param['order']))
  143. {
  144. $param['order'] = 'data_con, hora, cod_espec';
  145. $param['direction'] = 'desc, asc, asc';
  146. }
  147. $criteria->setProperties($param); // order, offset
  148. $criteria->setProperty('limit', $limit);
  149. if (TSession::getValue('Agenda_filter'))
  150. {
  151. // add the filter stored in the session to the criteria
  152. $criteria->add(TSession::getValue('Agenda_filter'));
  153. }
  154. // load the objects according to criteria
  155. $objects = $repository->load($criteria, FALSE);
  156. $this->datagrid->clear();
  157. if ($objects)
  158. {
  159. // iterate the collection of active records
  160. foreach ($objects as $object)
  161. {
  162. // add the object inside the datagrid
  163. $this->datagrid->addItem($object);
  164. }
  165. //converte datas para gravação e edição em formato BR
  166. $object->data_con = DateTime::createFromFormat('Y-m-d', $object->data_con)->format( 'd/m/Y' );
  167. }
  168. // reset the criteria for record count
  169. $criteria->resetProperties();
  170. $count= $repository->count($criteria);
  171. $this->pageNavigation->setCount($count); // count of records
  172. $this->pageNavigation->setProperties($param); // order, page
  173. $this->pageNavigation->setLimit($limit); // limit
  174. // close the transaction
  175. TTransaction::close();
  176. $this->loaded = true;
  177. }
  178. catch (Exception $e) // in case of exception
  179. {
  180. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  181. TTransaction::rollback(); // undo all pending operations
  182. }
  183. }
  184. /**
  185. * method onDelete()
  186. * executed whenever the user clicks at the delete button
  187. * Ask if the user really wants to delete the record
  188. */
  189. function onDelete($param)
  190. {
  191. // define the delete action
  192. $action = new TAction(array($this, 'Delete'));
  193. $action->setParameters($param); // pass the key parameter ahead
  194. // shows a dialog to the user
  195. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  196. }
  197. /**
  198. * method Delete()
  199. * Delete a record
  200. */
  201. function Delete($param)
  202. {
  203. try
  204. {
  205. // get the parameter $key
  206. $key=$param['key'];
  207. TTransaction::open('info_clin'); // open the transaction
  208. $object = new Agenda($key, FALSE); // instantiates the Active Record
  209. $object->delete(); // deletes the object
  210. TTransaction::close(); // close the transaction
  211. $this->onReload( $param ); // reload the listing
  212. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted')); // success message
  213. }
  214. catch (Exception $e) // in case of exception
  215. {
  216. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  217. TTransaction::rollback(); // undo all pending operations
  218. }
  219. }
  220. /**
  221. * method onSave()
  222. * Executed whenever the user clicks at the save button
  223. */
  224. function onSave()
  225. {
  226. try
  227. {
  228. TTransaction::open('info_clin'); // open a transaction with database
  229. // get the form data into an active record Agenda
  230. $object = $this->form->getData('Agenda');
  231. $this->form->validate(); // form validation
  232. //converte data do BR para US - AQUI CONVERSÃO PARA SALVAR OS DADOS
  233. $object->data_con = DateTime::createFromFormat('d/m/Y', $object->data_con)->format( 'Y-m-d' );
  234. $object->store(); // stores the object
  235. //converte data do US para BR - AQUI CONVERSÃO PARA SALVAR OS DADOS
  236. $object->data_con = DateTime::createFromFormat('Y-m-d', $object->data_con)->format( 'd/m/Y' );
  237. $this->form->setData($object); // fill the form with the active record data
  238. TTransaction::close(); // close the transaction
  239. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved')); // success message
  240. $this->onReload(); // reload the listing
  241. }
  242. catch (Exception $e) // in case of exception
  243. {
  244. $resultado = strpos($e->getMessage(),'23000');
  245. if ($resultado != Null)
  246. new TMessage('error', '<b>Erro: Já existe consulta marcada para a Data e Hora selecionadas!</b> <BR><BR> ' . $e->getMessage());
  247. else
  248. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  249. TTransaction::rollback(); // undo all pending operations
  250. }
  251. }
  252. /**
  253. * method onEdit()
  254. * Executed whenever the user clicks at the edit button da datagrid
  255. */
  256. function onEdit($param)
  257. {
  258. try
  259. {
  260. if (isset($param['key']))
  261. {
  262. $key=$param['key']; // get the parameter $key
  263. TTransaction::open('info_clin'); // open a transaction with the database
  264. $object = new Agenda($key); // instantiates the Active Record
  265. //converte datas para gravação e edição em formato BR
  266. $object->data_con = DateTime::createFromFormat('Y-m-d', $object->data_con)->format( 'd/m/Y' );
  267. $this->form->setData($object); // fill the form with the active record data
  268. TTransaction::close(); // close the transaction
  269. }
  270. else
  271. {
  272. $this->form->clear();
  273. }
  274. }
  275. catch (Exception $e) // in case of exception
  276. {
  277. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  278. TTransaction::rollback(); // undo all pending operations
  279. }
  280. }
  281. /**
  282. * method show()
  283. * Shows the page e seu conteúdo
  284. */
  285. function show()
  286. {
  287. // check if the datagrid is already loaded
  288. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  289. {
  290. $this->onReload( func_get_arg(0) );
  291. }
  292. parent::show();
  293. }
  294. /**
  295. ** Função de busca do nome do médico
  296. ** - Desenvolvido por Leandro J N Barbosa
  297. ** - Utilizando buscar o nome do médico da tabela especialidade em tempo de execuçao
  298. **/
  299. public static function onMedico($param)
  300. {
  301. if (isset($param['key']))
  302. {
  303. $obj = new StdClass;
  304. $key=$param['key']; // get the parameter $key
  305. TTransaction::open('info_clin'); // open a transaction with the database
  306. $object = new Especialidade($key); // instantiates the Active Record
  307. $resultado = $object->nome_medico;
  308. }
  309. $obj->especialidade_nome_medico = $resultado;
  310. TForm::sendData('form_Agenda', $obj);
  311. }
  312. }
  313. ?>

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)


NR

Leandro, tente o seguinte:
 
  1. <?php
  2. $param['order'] = 'data_con desc, hora asc, cod_espec';
  3. $param['direction'] = 'asc';
  4. ?>
LB

Nataniel show de bola, funcionou perfeitamente.

Obrigado pela atenção e ajuda!