Lançado Adianti Framework 7.6!
Clique aqui para saber mais
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 qu...
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...

  1. <?php
  2. /**
  3.  * forCadEmail Form List
  4.  * @author  <your name here>
  5.  */
  6. class forCadEmail extends TPage
  7. {
  8.     protected $form// form
  9.     protected $datagrid// datagrid
  10.     protected $pageNavigation;
  11.     protected $loaded;
  12.     
  13.     /**
  14.      * Form constructor
  15.      * @param $param Request
  16.      */
  17.     public function __construct$param )
  18.     {
  19.         parent::__construct();
  20.         
  21.         // creates the form
  22.         $this->form = new TQuickForm('form_MEmail');
  23.         $this->form->class 'tform'// change CSS class
  24.         $this->form = new BootstrapFormWrapper($this->form);
  25.         $this->form->style 'display: table;width:100%'// change style
  26.         $this->form->setFormTitle('MEmail');
  27.         
  28.         // create the form fields
  29.         $id = new TEntry('id');
  30.         $memMembro = new TDBCombo('memMembro','consimpro','mpessoa','mpeNome','mpeNome','mpeNome');
  31.         $memEmail = new TEntry('memEmail');
  32.         // add the fields
  33.         $this->form->addQuickField('Id'$id,  100 );
  34.         $this->form->addQuickField('Pessoa'$memMembro,  200 , new TRequiredValidator);
  35.         $this->form->addQuickField('E-mail'$memEmail,  200 , new TRequiredValidator);
  36.         
  37.         
  38.         $memEmail->addValidation('E-mail', new TEmailValidator);
  39.         if (!empty($id))
  40.         {
  41.             $id->setEditable(FALSE);
  42.         }
  43.         /** samples
  44.          $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  45.          $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  46.          $fieldX->setSize( 100, 40 ); // set size
  47.          **/
  48.          
  49.         // create the form actions
  50.         $this->form->addQuickAction(_t('Save'), new TAction(array($this'onSave')), 'fa:floppy-o');
  51.         $this->form->addQuickAction(_t('New'),  new TAction(array($this'onClear')), 'bs:plus-sign green');
  52.         
  53.         // creates a Datagrid
  54.         $this->datagrid = new TDataGrid;
  55.         ##LIST_DECORATOR##
  56.         $this->datagrid->style 'width: 100%';
  57.         $this->datagrid->setHeight(320);
  58.         // $this->datagrid->datatable = 'true';
  59.         // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  60.         
  61.         // creates the datagrid columns
  62.         $column_id = new TDataGridColumn('id''Id''left');
  63.         $column_memMembro = new TDataGridColumn('memMembro''Pessoa''left');
  64.         $column_memEmail = new TDataGridColumn('memEmail''E-mail''left');
  65.         // add the columns to the DataGrid
  66.         ///$this->datagrid->addColumn($column_id);
  67.         $this->datagrid->addColumn($column_memMembro);
  68.         $this->datagrid->addColumn($column_memEmail);
  69.         
  70.         // creates two datagrid actions
  71.         $action1 = new TDataGridAction(array($this'onEdit'));
  72.         $action1->setUseButton(TRUE);
  73.         $action1->setButtonClass('btn btn-default');
  74.         $action1->setLabel(_t('Edit'));
  75.         $action1->setImage('fa:pencil-square-o blue fa-lg');
  76.         $action1->setField('id');
  77.         
  78.         $action2 = new TDataGridAction(array($this'onDelete'));
  79.         $action2->setUseButton(TRUE);
  80.         $action2->setButtonClass('btn btn-default');
  81.         $action2->setLabel(_t('Delete'));
  82.         $action2->setImage('fa:trash-o red fa-lg');
  83.         $action2->setField('id');
  84.         
  85.         // add the actions to the datagrid
  86.         $this->datagrid->addAction($action1);
  87.         $this->datagrid->addAction($action2);
  88.         
  89.         // create the datagrid model
  90.         $this->datagrid->createModel();
  91.         
  92.         // creates the page navigation
  93.         $this->pageNavigation = new TPageNavigation;
  94.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  95.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  96.         
  97.         // vertical box container
  98.         $container = new TVBox;
  99.         $container->style 'width: 90%';
  100.         // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  101.         $container->add(TPanelGroup::pack('Title'$this->form));
  102.         $container->add($this->datagrid);
  103.         $container->add($this->pageNavigation);
  104.         
  105.         parent::add($container);
  106.     }
  107.     /**
  108.      * Load the datagrid with data
  109.      */
  110.     public function onReload($param NULL)
  111.     {
  112.         try
  113.         {
  114.             // open a transaction with database 'consimpro'
  115.             TTransaction::open('consimpro');
  116.             
  117.             // creates a repository for MEmail
  118.             $repository = new TRepository('MEmail');
  119.             $limit 10;
  120.             // creates a criteria
  121.             $criteria = new TCriteria;
  122.             
  123.             // default order
  124.             if (empty($param['order']))
  125.             {
  126.                 $param['order'] = 'id';
  127.                 $param['direction'] = 'asc';
  128.             }
  129.             $criteria->setProperties($param); // order, offset
  130.             $criteria->setProperty('limit'$limit);
  131.             
  132.             if (TSession::getValue('MEmail_filter'))
  133.             {
  134.                 // add the filter stored in the session to the criteria
  135.                 $criteria->add(TSession::getValue('MEmail_filter'));
  136.             }
  137.             
  138.             // load the objects according to criteria
  139.             $objects $repository->load($criteriaFALSE);
  140.             
  141.             $this->datagrid->clear();
  142.             if ($objects)
  143.             {
  144.                 // iterate the collection of active records
  145.                 foreach ($objects as $object)
  146.                 {
  147.                     // add the object inside the datagrid
  148.                     $this->datagrid->addItem($object);
  149.                 }
  150.             }
  151.             
  152.             // reset the criteria for record count
  153.             $criteria->resetProperties();
  154.             $count$repository->count($criteria);
  155.             
  156.             $this->pageNavigation->setCount($count); // count of records
  157.             $this->pageNavigation->setProperties($param); // order, page
  158.             $this->pageNavigation->setLimit($limit); // limit
  159.             
  160.             // close the transaction
  161.             TTransaction::close();
  162.             $this->loaded true;
  163.         }
  164.         catch (Exception $e// in case of exception
  165.         {
  166.             // shows the exception error message
  167.             new TMessage('error''<b>Error</b> ' $e->getMessage());
  168.             
  169.             // undo all pending operations
  170.             TTransaction::rollback();
  171.         }
  172.     }
  173.     
  174.     /**
  175.      * Ask before deletion
  176.      */
  177.     public function onDelete($param)
  178.     {
  179.         // define the delete action
  180.         $action = new TAction(array($this'Delete'));
  181.         $action->setParameters($param); // pass the key parameter ahead
  182.         
  183.         // shows a dialog to the user
  184.         new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  185.     }
  186.     
  187.     /**
  188.      * Delete a record
  189.      */
  190.     public function Delete($param)
  191.     {
  192.         try
  193.         {
  194.             $key=$param['key']; // get the parameter $key
  195.             TTransaction::open('consimpro'); // open a transaction with database
  196.             $object = new MEmail($keyFALSE); // instantiates the Active Record
  197.             $object->delete(); // deletes the object from the database
  198.             TTransaction::close(); // close the transaction
  199.             $this->onReload$param ); // reload the listing
  200.             new TMessage('info'TAdiantiCoreTranslator::translate('Record deleted')); // success message
  201.         }
  202.         catch (Exception $e// in case of exception
  203.         {
  204.             new TMessage('error''<b>Error</b> ' $e->getMessage()); // shows the exception error message
  205.             TTransaction::rollback(); // undo all pending operations
  206.         }
  207.     }
  208.     
  209.     /**
  210.      * Save form data
  211.      * @param $param Request
  212.      */
  213.     public function onSave$param )
  214.     {
  215.         try
  216.         {
  217.             TTransaction::open('consimpro'); // open a transaction
  218.             
  219.             /**
  220.             // Enable Debug logger for SQL operations inside the transaction
  221.             TTransaction::setLogger(new TLoggerSTD); // standard output
  222.             TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  223.             **/
  224.             
  225.             $this->form->validate(); // validate form data
  226.             
  227.             $object = new MEmail;  // create an empty object
  228.             $data $this->form->getData(); // get form data as array
  229.             $object->fromArray( (array) $data); // load the object with data
  230.             $object->store(); // save the object
  231.             
  232.             // get the generated id
  233.             $data->id $object->id;
  234.             
  235.             $this->form->setData($data); // fill form data
  236.             TTransaction::close(); // close the transaction
  237.             
  238.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved')); // success message
  239.             $this->onReload(); // reload the listing
  240.         }
  241.         catch (Exception $e// in case of exception
  242.         {
  243.             new TMessage('error'$e->getMessage()); // shows the exception error message
  244.             $this->form->setData$this->form->getData() ); // keep form data
  245.             TTransaction::rollback(); // undo all pending operations
  246.         }
  247.     }
  248.     
  249.     /**
  250.      * Clear form data
  251.      * @param $param Request
  252.      */
  253.     public function onClear$param )
  254.     {
  255.         $this->form->clear();
  256.     }
  257.     
  258.     /**
  259.      * Load object to form data
  260.      * @param $param Request
  261.      */
  262.     public function onEdit$param )
  263.     {
  264.         try
  265.         {
  266.             if (isset($param['key']))
  267.             {
  268.                 $key $param['key'];  // get the parameter $key
  269.                 TTransaction::open('consimpro'); // open a transaction
  270.                 $object = new MEmail($key); // instantiates the Active Record
  271.                 $this->form->setData($object); // fill the form
  272.                 TTransaction::close(); // close the transaction
  273.             }
  274.             else
  275.             {
  276.                 $this->form->clear();
  277.             }
  278.         }
  279.         catch (Exception $e// in case of exception
  280.         {
  281.             new TMessage('error'$e->getMessage()); // shows the exception error message
  282.             TTransaction::rollback(); // undo all pending operations
  283.         }
  284.     }
  285.     
  286.     /**
  287.      * method show()
  288.      * Shows the page
  289.      */
  290.     public function show()
  291.     {
  292.         // check if the datagrid is already loaded
  293.         if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  294.         {
  295.             $this->onReloadfunc_get_arg(0) );
  296.         }
  297.         parent::show();
  298.     }
  299. }
  300. ?>

Pacotão Dominando o Adianti Framework 7
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4


Dominando o Adianti 7 Quero me inscrever agora!

Comentários (3)


CC

Continuo com o mesmo problema... deve mesmo ser muito difícil... já estou quase pagando por uma solução...
PD

Não entendi a pergunta.
CC

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.