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...
segue o código para ver se alguém pode me ajudar...
- <?php
- /**
- * forCadEmail Form List
- * @author <your name here>
- */
- class forCadEmail extends TPage
- {
- protected $form; // form
- protected $datagrid; // datagrid
- protected $pageNavigation;
- protected $loaded;
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
- // creates the form
- $this->form = new TQuickForm('form_MEmail');
- $this->form->class = 'tform'; // change CSS class
- $this->form = new BootstrapFormWrapper($this->form);
- $this->form->style = 'display: table;width:100%'; // change style
- $this->form->setFormTitle('MEmail');
- // create the form fields
- $id = new TEntry('id');
- $memMembro = new TDBCombo('memMembro','consimpro','mpessoa','mpeNome','mpeNome','mpeNome');
- $memEmail = new TEntry('memEmail');
- // add the fields
- $this->form->addQuickField('Id', $id, 100 );
- $this->form->addQuickField('Pessoa', $memMembro, 200 , new TRequiredValidator);
- $this->form->addQuickField('E-mail', $memEmail, 200 , new TRequiredValidator);
- $memEmail->addValidation('E-mail', new TEmailValidator);
- if (!empty($id))
- {
- $id->setEditable(FALSE);
- }
- /** samples
- $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( 100, 40 ); // set size
- **/
- // create the form actions
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
- // creates a Datagrid
- $this->datagrid = new TDataGrid;
- ##LIST_DECORATOR##
- $this->datagrid->style = 'width: 100%';
- $this->datagrid->setHeight(320);
- // $this->datagrid->datatable = 'true';
- // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
- // creates the datagrid columns
- $column_id = new TDataGridColumn('id', 'Id', 'left');
- $column_memMembro = new TDataGridColumn('memMembro', 'Pessoa', 'left');
- $column_memEmail = new TDataGridColumn('memEmail', 'E-mail', 'left');
- // add the columns to the DataGrid
- ///$this->datagrid->addColumn($column_id);
- $this->datagrid->addColumn($column_memMembro);
- $this->datagrid->addColumn($column_memEmail);
- // creates two datagrid actions
- $action1 = new TDataGridAction(array($this, 'onEdit'));
- $action1->setUseButton(TRUE);
- $action1->setButtonClass('btn btn-default');
- $action1->setLabel(_t('Edit'));
- $action1->setImage('fa:pencil-square-o blue fa-lg');
- $action1->setField('id');
- $action2 = new TDataGridAction(array($this, 'onDelete'));
- $action2->setUseButton(TRUE);
- $action2->setButtonClass('btn btn-default');
- $action2->setLabel(_t('Delete'));
- $action2->setImage('fa:trash-o red fa-lg');
- $action2->setField('id');
- // add the actions to the datagrid
- $this->datagrid->addAction($action1);
- $this->datagrid->addAction($action2);
- // 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());
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add(TPanelGroup::pack('Title', $this->form));
- $container->add($this->datagrid);
- $container->add($this->pageNavigation);
- parent::add($container);
- }
- /**
- * Load the datagrid with data
- */
- public function onReload($param = NULL)
- {
- try
- {
- // open a transaction with database 'consimpro'
- TTransaction::open('consimpro');
- // creates a repository for MEmail
- $repository = new TRepository('MEmail');
- $limit = 10;
- // creates a criteria
- $criteria = new TCriteria;
- // default order
- if (empty($param['order']))
- {
- $param['order'] = 'id';
- $param['direction'] = 'asc';
- }
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('limit', $limit);
- if (TSession::getValue('MEmail_filter'))
- {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('MEmail_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);
- }
- }
- // 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
- {
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * Ask before deletion
- */
- public 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);
- }
- /**
- * Delete a record
- */
- public function Delete($param)
- {
- try
- {
- $key=$param['key']; // get the parameter $key
- TTransaction::open('consimpro'); // open a transaction with database
- $object = new MEmail($key, FALSE); // instantiates the Active Record
- $object->delete(); // deletes the object from the database
- 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
- }
- }
- /**
- * Save form data
- * @param $param Request
- */
- public function onSave( $param )
- {
- try
- {
- TTransaction::open('consimpro'); // open a transaction
- /**
- // Enable Debug logger for SQL operations inside the transaction
- TTransaction::setLogger(new TLoggerSTD); // standard output
- TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
- **/
- $this->form->validate(); // validate form data
- $object = new MEmail; // create an empty object
- $data = $this->form->getData(); // get form data as array
- $object->fromArray( (array) $data); // load the object with data
- $object->store(); // save the object
- // get the generated id
- $data->id = $object->id;
- $this->form->setData($data); // fill form 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
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * Clear form data
- * @param $param Request
- */
- public function onClear( $param )
- {
- $this->form->clear();
- }
- /**
- * Load object to form data
- * @param $param Request
- */
- public function onEdit( $param )
- {
- try
- {
- if (isset($param['key']))
- {
- $key = $param['key']; // get the parameter $key
- TTransaction::open('consimpro'); // open a transaction
- $object = new MEmail($key); // instantiates the Active Record
- $this->form->setData($object); // fill the form
- TTransaction::close(); // close the transaction
- }
- else
- {
- $this->form->clear();
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * method show()
- * Shows the page
- */
- public 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();
- }
- }
- ?>
Continuo com o mesmo problema... deve mesmo ser muito difícil... já estou quase pagando por uma solução...
Não entendi a pergunta.
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.