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

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!