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?
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?
- <?php e
- /**
- * AgendaFormList Registration
- * @author <your name here>
- */
- class AgendaFormList extends TPage
- {
- protected $form; // form
- protected $datagrid; // datagrid
- protected $pageNavigation;
- protected $loaded;
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct()
- {
- parent::__construct();
- // creates the form
- $this->form = new TQuickForm('form_Agenda');
- $this->form->class = 'tform'; // CSS class
- $this->form->setFormTitle('Agenda'); // define the form title
- // create the form fields
- 2436 = new TEntry('id');
- $data_con = new TDate('data_con');
- $hora = new TCombo('hora');
- $cod_espec = new ">TDBSeekButton('cod_espec','info_clin','form_Agenda','especialidade','descricao','cod_espec','especialidade_descricao');
- $especialidade_descricao = new TEntry('especialidade_descricao');
- $especialidade_nome_medico = new TEntry('especialidade_nome_medico');
- $cod_pac = new ">TDBSeekButton('cod_pac','info_clin','form_Agenda','paciente','nome','cod_pac','paciente_nome');
- $paciente_nome = new TEntry('paciente_nome');
- $observacao = new TText('observacao');
- // add the fields
- $this->form->addQuickField('Identificador', 2436, 100);
- $this->form->addQuickField('Data', $data_con, 100, new TRequiredValidator );
- $this->form->addQuickField('Hora', $hora, 100, new TRequiredValidator );
- $this->form->addQuickField('Cód. Especialidade', $cod_espec, 100, new TRequiredValidator );
- $this->form->addQuickField('Descrição Especialidade', $especialidade_descricao, 400);
- $this->form->addQuickField('Nome Médico', $especialidade_nome_medico, 400);
- $this->form->addQuickField('Cód. Paciente', $cod_pac, 100, new TRequiredValidator );
- $this->form->addQuickField('Nome Paciente', $paciente_nome, 400);
- $this->form->addQuickField('Observação', $observacao, 800);
- $observacao->setSize(800, 40);
- //carregando os estados para o combo de estado
- $itens_hr = array();
- $itens_hr['06:00:00'] = '06:00';
- $itens_hr['06:30:00'] = '06:30';
- $itens_hr['07:00:00'] = '07:00';
- $itens_hr['07:30:00'] = '07:30';
- $itens_hr['08:00:00'] = '08:00';
- $itens_hr['08:30:00'] = '08:30';
- $itens_hr['09:00:00'] = '09:00';
- $itens_hr['09:30:00'] = '09:30';
- $itens_hr['10:00:00'] = '10:00';
- $itens_hr['10:30:00'] = '10:30';
- $itens_hr['11:00:00'] = '11:00';
- $itens_hr['11:30:00'] = '11:30';
- $itens_hr['12:00:00'] = '12:00';
- $itens_hr['12:30:00'] = '12:30';
- $itens_hr['13:00:00'] = '13:00';
- $itens_hr['13:30:00'] = '13:30';
- $itens_hr['14:00:00'] = '14:00';
- $itens_hr['14:30:00'] = '14:30';
- $itens_hr['15:00:00'] = '15:00';
- $itens_hr['15:30:00'] = '15:30';
- $itens_hr['16:00:00'] = '16:00';
- $itens_hr['16:30:00'] = '16:30';
- $itens_hr['17:00:00'] = '17:00';
- $itens_hr['17:30:00'] = '17:30';
- $itens_hr['18:00:00'] = '18:00';
- $itens_hr['18:30:00'] = '18:30';
- $itens_hr['19:00:00'] = '19:00';
- $itens_hr['19:30:00'] = '19:30';
- $itens_hr['20:00:00'] = '20:00';
- $hora->addItems($itens_hr);
- //desabilitando campo chave primaria
- 2436->setEditable(False);
- $paciente_nome->setEditable(False);
- $especialidade_descricao->setEditable(False);
- $especialidade_nome_medico->setEditable(False);
- //setando mascara
- $data_con->setMask('dd/mm/yyyy');
- //chamando validação
- $data_con->addValidation('Data', new TDateConValidator);
- //CRIANDO EXIT PARA BUSCA DE NOME DO MÉDICO
- $exit_action = New TAction(array($this, 'onMedico'));
- $cod_espec->setExitAction($exit_action);
- // create the form actions
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'ico_save.png');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onEdit')), 'ico_new.png');
- // creates a DataGrid
- $this->datagrid = new TQuickGrid;
- $this->datagrid->setHeight(320);
- // creates the datagrid columns
- //2436 = $this->datagrid->addQuickColumn('Identificador', 'id', 'left', 100);
- $data_con = $this->datagrid->addQuickColumn('Data', 'data_con_formatada', 'left', 100);
- $hora = $this->datagrid->addQuickColumn('Hora', 'hora', 'left', 100);
- //$cod_espec = $this->datagrid->addQuickColumn('Cód.Espec', 'cod_espec', 'left', 100);
- $paciente_nome = $this->datagrid->addQuickColumn('Paciente', 'paciente_nome', 'left', 400);
- $especialidade_descricao = $this->datagrid->addQuickColumn('Especialidade', 'especialidade_descricao', 'left', 400);
- $especialidade_nome_medico = $this->datagrid->addQuickColumn('Médico', 'especialidade_nome_medico', 'left', 400);
- //$cod_pac = $this->datagrid->addQuickColumn('Paciente', 'cod_pac', 'left', 100);
- $observacao = $this->datagrid->addQuickColumn('Observação', 'observacao', 'left', 800);
- // create the datagrid actions
- $edit_action = new TDataGridAction(array($this, 'onEdit'));
- $delete_action = new TDataGridAction(array($this, 'onDelete'));
- // add the actions to the datagrid
- $this->datagrid->addQuickAction(_t('Edit'), $edit_action, 'id', 'ico_edit.png');
- $this->datagrid->addQuickAction(_t('Delete'), $delete_action, 'id', 'ico_delete.png');
- // create the datagrid model
- $this->datagrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // create the datagrid model
- $this->datagrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // create the page container
- $container = TVBox::pack( $this->form, $this->datagrid, $this->pageNavigation);
- parent::add($container);
- }
- /**
- * method onReload()
- * Load the datagrid with the database objects
- */
- function onReload($param = NULL)
- {
- try
- {
- // open a transaction with database 'info_clin'
- TTransaction::open('info_clin');
- // creates a repository for Agenda
- $repository = new TRepository('Agenda');
- $limit = 10;
- // creates a criteria
- $criteria = new TCriteria;
- // default order
- if (empty($param['order']))
- {
- $param['order'] = 'data_con, hora, cod_espec';
- $param['direction'] = 'desc, asc, asc';
- }
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('limit', $limit);
- if (TSession::getValue('Agenda_filter'))
- {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('Agenda_filter'));
- }
- // load the objects according to criteria
- $objects = $repository->load($criteria, FALSE);
- $this->datagrid->clear();
- if ($objects)
- {
- // iterate the collection of active records
- foreach ($objects as $object)
- {
- // add the object inside the datagrid
- $this->datagrid->addItem($object);
- }
- //converte datas para gravação e edição em formato BR
- $object->data_con = DateTime::createFromFormat('Y-m-d', $object->data_con)->format( 'd/m/Y' );
- }
- // reset the criteria for record count
- $criteria->resetProperties();
- $count= $repository->count($criteria);
- $this->pageNavigation->setCount($count); // count of records
- $this->pageNavigation->setProperties($param); // order, page
- $this->pageNavigation->setLimit($limit); // limit
- // close the transaction
- TTransaction::close();
- $this->loaded = true;
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * method onDelete()
- * executed whenever the user clicks at the delete button
- * Ask if the user really wants to delete the record
- */
- function onDelete($param)
- {
- // define the delete action
- $action = new TAction(array($this, 'Delete'));
- $action->setParameters($param); // pass the key parameter ahead
- // shows a dialog to the user
- new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
- }
- /**
- * method Delete()
- * Delete a record
- */
- function Delete($param)
- {
- try
- {
- // get the parameter $key
- $key=$param['key'];
- TTransaction::open('info_clin'); // open the transaction
- $object = new Agenda($key, FALSE); // instantiates the Active Record
- $object->delete(); // deletes the object
- TTransaction::close(); // close the transaction
- $this->onReload( $param ); // reload the listing
- new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted')); // success message
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * method onSave()
- * Executed whenever the user clicks at the save button
- */
- function onSave()
- {
- try
- {
- TTransaction::open('info_clin'); // open a transaction with database
- // get the form data into an active record Agenda
- $object = $this->form->getData('Agenda');
- $this->form->validate(); // form validation
- //converte data do BR para US - AQUI CONVERSÃO PARA SALVAR OS DADOS
- $object->data_con = DateTime::createFromFormat('d/m/Y', $object->data_con)->format( 'Y-m-d' );
- $object->store(); // stores the object
- //converte data do US para BR - AQUI CONVERSÃO PARA SALVAR OS DADOS
- $object->data_con = DateTime::createFromFormat('Y-m-d', $object->data_con)->format( 'd/m/Y' );
- $this->form->setData($object); // fill the form with the active record data
- TTransaction::close(); // close the transaction
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved')); // success message
- $this->onReload(); // reload the listing
- }
- catch (Exception $e) // in case of exception
- {
- $resultado = strpos($e->getMessage(),'23000');
- if ($resultado != Null)
- new TMessage('error', '<b>Erro: Já existe consulta marcada para a Data e Hora selecionadas!</b> <BR><BR> ' . $e->getMessage());
- else
- new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * method onEdit()
- * Executed whenever the user clicks at the edit button da datagrid
- */
- function onEdit($param)
- {
- try
- {
- if (isset($param['key']))
- {
- $key=$param['key']; // get the parameter $key
- TTransaction::open('info_clin'); // open a transaction with the database
- $object = new Agenda($key); // instantiates the Active Record
- //converte datas para gravação e edição em formato BR
- $object->data_con = DateTime::createFromFormat('Y-m-d', $object->data_con)->format( 'd/m/Y' );
- $this->form->setData($object); // fill the form with the active record data
- TTransaction::close(); // close the transaction
- }
- else
- {
- $this->form->clear();
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * method show()
- * Shows the page e seu conteúdo
- */
- function show()
- {
- // check if the datagrid is already loaded
- if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
- {
- $this->onReload( func_get_arg(0) );
- }
- parent::show();
- }
- /**
- ** Função de busca do nome do médico
- ** - Desenvolvido por Leandro J N Barbosa
- ** - Utilizando buscar o nome do médico da tabela especialidade em tempo de execuçao
- **/
- public static function onMedico($param)
- {
- if (isset($param['key']))
- {
- $obj = new StdClass;
- $key=$param['key']; // get the parameter $key
- TTransaction::open('info_clin'); // open a transaction with the database
- $object = new Especialidade($key); // instantiates the Active Record
- $resultado = $object->nome_medico;
- }
- $obj->especialidade_nome_medico = $resultado;
- TForm::sendData('form_Agenda', $obj);
- }
- }
- ?>
Leandro, tente o seguinte:
Nataniel show de bola, funcionou perfeitamente.
Obrigado pela atenção e ajuda!