Lançado Adianti Framework 7.6!
Clique aqui para saber mais
Problema com o onSearch Boa tarde, prezados, estou enfrentando um problema com a função do onSearch a qual não consigo gerar resultados ao procurar, já tentei de diversas formas, porém encontro resistência, vale ressaltar que sou novo no framework. Abaixo segue o código para que possam visualizar: ...
PA
Problema com o onSearch  
Boa tarde, prezados, estou enfrentando um problema com a função do onSearch a qual não consigo gerar resultados ao procurar, já tentei de diversas formas, porém encontro resistência, vale ressaltar que sou novo no framework.
Abaixo segue o código para que possam visualizar:

  1. <?php
  2. class RondaList extends TStandardList
  3. {
  4.     protected $form;     // registration form
  5.     protected $datagrid// listing
  6.     protected $pageNavigation;
  7.     
  8.     // trait with onReload, onSearch, onDelete...
  9.     use Adianti\Base\AdiantiStandardListTrait;
  10.     
  11.     /**
  12.      * Class constructor
  13.      * Creates the page, the form and the listing
  14.      */
  15.     public function __construct()
  16.     {
  17.         parent::__construct();
  18.         
  19.         $this->setDatabase('permission');        // defines the database
  20.         $this->setActiveRecord('Ronda');       // defines the active record
  21.         $this->addFilterField('fk_id_cad_vigia''like''fk_id_cad_vigia'); // filter field, operator, form field
  22.         $this->setDefaultOrder('id''asc');  // define the default order
  23.         
  24.         // creates the form
  25.         $this->form = new BootstrapFormBuilder('datagrid_ronda');
  26.         $this->form->setFormTitle('Rondas realizadas');
  27.         
  28.         $descricao = new TEntry('descricao');
  29.         $this->form->addFields( [new TLabel('Ronda')], [$descricao] );
  30.         
  31.         // add form actions
  32.         $this->form->addAction('Buscar', new TAction([$this'onSearch']), 'fa:search white')->class 'btn btn-sm btn-primary';
  33.         $this->form->addActionLink('Limpar',  new TAction([$this'clear']), 'fa:eraser white')->class 'btn btn-sm btn-info';
  34.         
  35.         // creates the DataGrid
  36.         $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  37.         $this->datagrid->width "100%";
  38.         
  39.         // creates the datagrid columns
  40.         $col_cad_vigia  = new TDataGridColumn('fk_id_cad_vigia''Nome''center''25%');
  41.         $data_ronda = new TDataGridColumn('data_tempo''Data''center''50%');
  42.         $status_ronda = new TDataGridColumn('fk_id_dados_unidade''Ronda''center''25%');
  43.         
  44.         $this->datagrid->addColumn($col_cad_vigia);
  45.         $this->datagrid->addColumn($data_ronda);
  46.         $this->datagrid->addColumn($status_ronda);
  47.         //Setar uma máscara ao campo data
  48.         $data_ronda->setTransformer( function($value$object$row) {
  49.             $data TDate::date2br($value );
  50.             return $data;
  51.         });
  52.         
  53.         $status_ronda->setTransformer( function($value$object$row) {
  54.             $class = ($value=='N') ? 'warning' 'success';
  55.             $label = ($value=='N') ? 'A caminho' 'Ronda realizada';
  56.             $div = new TElement('span');
  57.             $div->class="label label-{$class}";
  58.             $div->style="text-shadow:none; font-size:12px; font-weight:lighter";
  59.             $div->add($label);
  60.             return $div;
  61.         });
  62.         
  63.         $col_cad_vigia->setAction( new TAction([$this'onReload']), ['order' => 'fk_id_cad_vigia']);
  64.         $data_ronda->setAction( new TAction([$this'onReload']), ['order' => 'data_tempo']);
  65.         $status_ronda->setAction( new TAction([$this'onReload']), ['order' => 'fk_id_dados_unidade']);
  66.         
  67.         //botão com ação de editar
  68.         $action1 = new TDataGridAction([$this'onEdit']);
  69.         $action1->setLabel('Roteiro');
  70.         $action1->setImage('fa:map-marker blue fa-lg');
  71.         $action1->setFields(['id']);
  72.         $this->datagrid->addAction($action1);
  73.         
  74.         // create the datagrid model
  75.         $this->datagrid->createModel();
  76.         
  77.         // creates the page navigation
  78.         $this->pageNavigation = new TPageNavigation;
  79.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  80.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  81.         
  82.         // creates the page structure using a table
  83.         $vbox = new TVBox;
  84.         $vbox->style 'width: 100%';
  85.         $vbox->add(new TXMLBreadCrumb('menu.xml'__CLASS__));
  86.         $vbox->add($this->form);
  87.         $vbox->add(TPanelGroup::pack(''$this->datagrid$this->pageNavigation));
  88.         
  89.         // add the table inside the page
  90.         parent::add($vbox);
  91.     }
  92.     
  93.     /**
  94.      * Clear filters
  95.      */
  96.     function clear()
  97.     {
  98.         $this->clearFilters();
  99.         $this->onReload();
  100.     }
  101.     function onEdit(){
  102.     }
  103.     function onSearch(){
  104.         $data $this->form->getData();
  105.         if(isset($data->descricao)){
  106.             if (isset($data->descricao) AND strlen(trim($data->descricao)) > 0) {
  107.                 $filter = new TFilter('descricao''ilike'"%{$data->descricao}%"); // create the filter
  108.                 TSession::setValue('SegCadastroRonda_filter_descricao',   $filter); // stores the filter in the session
  109.             }
  110.             //$criterio = new TCriteria;
  111.             //$criterio->add(new TFilter('fk_id_cad_vigia', 'like', '%{$data->fk_id_cad_vigia}%'));
  112.             //TSession::setValue('criterio', $criterio);
  113.             $this->form->setData($data);
  114.         }
  115.         $param=array();
  116.         $param['offset']    =0;
  117.         $param['first_page']=1;
  118.         $this->onReload($param);
  119.         //echo $data->fk_id_cad_vigia;
  120.     }
  121.     function onReload($param NULL){
  122.         try {
  123.             TTransaction::open('permission');
  124.             
  125.             $repositorio = new TRepository('Ronda');
  126.             $objetos $repositorio->load(new TCriteria);
  127.             $this->datagrid->clear();
  128.             if($objetos){
  129.                 foreach ($objetos as $objeto) {
  130.                     $vigia = new Vigia($objeto->fk_id_cad_vigia);
  131.                     $objeto->data_tempo $vigia->data_cadastro;
  132.                     $objeto->fk_id_cad_vigia $vigia->nome;
  133.                     $this->datagrid->addItem($objeto);
  134.                 }
  135.             }
  136.             TTransaction::close();
  137.         } catch (Exception $e) {
  138.             new TMessage('error'$e->getMessage());
  139.         }
  140.     }
  141. }

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 (1)


MG

Amigo, onSearch ele cria na sessão os TCriterias a serem utilizado pelo onReload.
Ocorre que no onReload vc não recupera os filtros para usá-los!

Veja exemplo:

onSeacch
  1. <?php
  2.     /**
  3.      * Register the filter in the session
  4.      */
  5.     public function onSearch()
  6.     {
  7.         // get the search form data
  8.         $data $this->form->getData();
  9.         
  10.         // clear session filters
  11.         TSession::setValue('SystemDocumentList_filter_title',   NULL);
  12.         TSession::setValue('SystemDocumentList_filter_category_id',   NULL);
  13.         TSession::setValue('SystemDocumentList_filter_filename',   NULL);
  14.         if (isset($data->title) AND ($data->title)) {
  15.             $filter = new TFilter('title''like'"%{$data->title}%"); // create the filter
  16.             TSession::setValue('SystemDocumentList_filter_title',   $filter); // stores the filter in the session
  17.         }
  18.         if (isset($data->category_id) AND ($data->category_id)) {
  19.             $filter = new TFilter('category_id''='$data->category_id); // create the filter
  20.             TSession::setValue('SystemDocumentList_filter_category_id',   $filter); // stores the filter in the session
  21.         }
  22.         if (isset($data->filename) AND ($data->filename)) {
  23.             $filter = new TFilter('filename''like'"%{$data->filename}%"); // create the filter
  24.             TSession::setValue('SystemDocumentList_filter_filename',   $filter); // stores the filter in the session
  25.         }
  26.         
  27.         // fill the form with data again
  28.         $this->form->setData($data);
  29.         
  30.         // keep the search data in the session
  31.         TSession::setValue('SystemDocument_filter_data'$data);
  32.         
  33.         $param=array();
  34.         $param['offset']    =0;
  35.         $param['first_page']=1;
  36.         $this->onReload($param);
  37.     }
  38. ?>


onReload

  1. <?php
  2.     /**
  3.      * Load the datagrid with data
  4.      */
  5.     public function onReload($param NULL)
  6.     {
  7.         try
  8.         {
  9.             // open a transaction with database 'communication'
  10.             TTransaction::open('communication');
  11.             
  12.             // creates a repository for SystemDocument
  13.             $repository = new TRepository('SystemDocument');
  14.             $limit 10;
  15.             // creates a criteria
  16.             $criteria = new TCriteria;
  17.             
  18.             // default order
  19.             if (empty($param['order']))
  20.             {
  21.                 $param['order'] = 'id';
  22.                 $param['direction'] = 'asc';
  23.             }
  24.             $criteria->setProperties($param); // order, offset
  25.             $criteria->setProperty('limit'$limit);
  26.             
  27.             if (TSession::getValue('login') !== 'admin')
  28.             {
  29.                 $criteria->add(new TFilter('system_user_id''='TSession::getValue('userid')));
  30.             }
  31.             
  32.             if (TSession::getValue('SystemDocumentList_filter_title')) {
  33.                 $criteria->add(TSession::getValue('SystemDocumentList_filter_title')); // add the session filter
  34.             }
  35.             if (TSession::getValue('SystemDocumentList_filter_category_id')) {
  36.                 $criteria->add(TSession::getValue('SystemDocumentList_filter_category_id')); // add the session filter
  37.             }
  38.             if (TSession::getValue('SystemDocumentList_filter_filename')) {
  39.                 $criteria->add(TSession::getValue('SystemDocumentList_filter_filename')); // add the session filter
  40.             }
  41.             
  42.             // load the objects according to criteria
  43.             $objects $repository->load($criteriaFALSE);
  44.             
  45.             if (is_callable($this->transformCallback))
  46.             {
  47.                 call_user_func($this->transformCallback$objects$param);
  48.             }
  49.             
  50.             $this->datagrid->clear();
  51.             if ($objects)
  52.             {
  53.                 // iterate the collection of active records
  54.                 foreach ($objects as $object)
  55.                 {
  56.                     // add the object inside the datagrid
  57.                     $this->datagrid->addItem($object);
  58.                 }
  59.             }
  60.             
  61.             // reset the criteria for record count
  62.             $criteria->resetProperties();
  63.             $count$repository->count($criteria);
  64.             
  65.             $this->pageNavigation->setCount($count); // count of records
  66.             $this->pageNavigation->setProperties($param); // order, page
  67.             $this->pageNavigation->setLimit($limit); // limit
  68.             
  69.             // close the transaction
  70.             TTransaction::close();
  71.             $this->loaded true;
  72.         }
  73.         catch (Exception $e// in case of exception
  74.         {
  75.             // shows the exception error message
  76.             new TMessage('error'$e->getMessage());
  77.             // undo all pending operations
  78.             TTransaction::rollback();
  79.         }
  80.     }
  81. ?>