IS
TDataGrid com TCheckButton não funciona (Ajuda)
Duas TDataGrid com TCheckButton na segunda não retorna nada no getData mesmo fazendo addField, alguém pode me ajudar? código abaixo:
- <?php
- class ExameList extends TPage
- {
- private $form; // search form
- private $datagrid; // listing
- private $total;
- private $cartgrid;
- private $pageNavigation;
- private $loaded;
- public function __construct()
- {
- parent::__construct();
- //new TSession;
- // creates the form
- $this->form = new TForm('form_exame');
- // create the form fields
- $description = new TEntry('paciente');
- $check = new THidden('check_');
- $description->setSize(170);
- $description->setValue(TSession::getValue('product_description'));
- $table = new TTable;
- $row = $table->addRow();
- $cell=$row->addCell('');
- $cell->width= 100;
- $row->addCell($description);
- // creates the action button
- $button1=new TButton('find');
- $button1->setAction(new TAction(array($this, 'onSearch')), 'Busca');
- $button1->setImage('fa:search');
- $row->addCell($button1);
- $this->form->add($table);
- $this->form->setFields(array($description, $button1));
- // creates a DataGrid
- $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
- $this->cartgrid = new BootstrapDatagridWrapper(new TDataGrid);
- $this->datagrid->style = 'width:100%';
- $this->cartgrid->style = 'width:100%';
- $this->datagrid->disableDefaultClick();
- $this->cartgrid->disableDefaultClick();
- //$panel = new TPanelGroup(_t('Datagrids with Checkbutton'));
- //$panel->add($this->cartgrid)->style = 'overflow-x:auto';
- //$this->form->add($panel);
- // creates the datagrid columns
- $cartao = new TDataGridColumn('CARTAO', 'Cartão SUS', 'left');
- $paciente = new TDataGridColumn('NOME_PAC', 'Nome', 'left');
- $sexo = new TDataGridColumn('sexo->nome', 'Sexo', 'left');
- $this->datagrid->addColumn($cartao);
- $this->datagrid->addColumn($paciente);
- $this->datagrid->addColumn($sexo);
- $DATA_NASC = new TDataGridColumn('DATA_NASC', 'Idade', 'center');
- $this->datagrid->addColumn($DATA_NASC);
- $DATA_NASC->setTransformer(function($value, $object, $row)
- {
- $rotulo = calcIdade::calcularIdade($value);
- if ($rotulo <= 18)
- {
- $bar = new TProgressBar;
- $bar->setMask('<b>'.$rotulo.'</b>');
- $bar->setValue(100);
- $bar->setClass('warning');
- return $bar;
- }
- else if (($rotulo >= 19) && ($rotulo <= 59))
- {
- $bar = new TProgressBar;
- $bar->setMask('<b>'.$rotulo.'</b>');
- $bar->setValue(100);
- $bar->setClass('success');
- return $bar;
- }
- else
- {
- $bar = new TProgressBar;
- $bar->setMask('<b>'.$rotulo.'</b>');
- $bar->setValue(100);
- $bar->setClass('danger');
- return $bar;
- }
- });
- $status = new TDataGridColumn('status', 'Status', 'center');
- $status->setTransformer(function($value, $object, $row)
- {
- if ($value == 1)
- {
- $bar = new TProgressBar;
- $bar->setMask('<b>Coletado</b>');
- $bar->setValue(100);
- $bar->setClass('warning');
- return $bar;
- }
- else if ($value == 0)
- {
- $bar = new TProgressBar;
- $bar->setMask('<b>Solicitado</b>');
- $bar->setValue(100);
- $bar->setClass('success');
- return $bar;
- }
- });
- $this->datagrid->addColumn($status);
- /////////////////////////////////
- $check = new TDataGridColumn('check_', 'ID', 'center', '30');
- $referencia = new TDataGridColumn('{referencia->procedimento}', 'Procedimento', 'left');
- $material = new TDataGridColumn('{material->nome}', 'Material', 'left');
- $this->cartgrid->addColumn($check);
- $this->cartgrid->addColumn($referencia);
- $this->cartgrid->addColumn($material);
- // creates datagrid actions
- $action1 = new TDataGridAction([$this, 'onSelect'], ['id' => '{id}','CARTAO' => '{CARTAO}'] );
- $action2 = new TDataGridAction([$this, 'onSave'], ['id' => '{id}'] );
- $this->datagrid->addAction($action1, 'Select', 'far:check-circle green');
- $this->datagrid->addAction($action2, 'Save', 'far:trash-alt red');
- //$this->cartgrid->addQuickAction('Delete', new TDataGridAction(array($this, 'onDelete')), 'id', 'fa:trash red');
- // create the datagrid model
- $this->datagrid->createModel();
- $this->cartgrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- // creates the page structure using a table
- $table1 = new TTable;
- $table1->style = 'width: 100%';
- $table1->addRow()->addCell($this->form)->height='50';
- $table1->addRow()->addCell($this->datagrid);
- $table1->addRow()->addCell($this->pageNavigation);
- $this->total = new TLabel('');
- $this->total->setFontStyle('b');
- $table2 = new TTable;
- $table2->style = 'width: 100%';
- $table2->addRow()->addCell($this->total)->height = '50';
- $table2->addRow()->addCell($this->cartgrid);
- $hbox = new THBox;
- $hbox->add($table1)->style.='vertical-align:top; width: 60%';
- $hbox->add($table2)->style.='vertical-align:top; width: 30%';
- // wrap the page content using vertical box
- $vbox = new TVBox;
- $vbox->style = 'width: 100%';
- $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $vbox->add($hbox);
- //$vbox->add($this->pageNavigation);
- parent::add($vbox);
- }
- /**
- * Put a product inside the cart
- */
- public function onSelect($param)
- {
- // reload datagrids
- // $this->onReload( func_get_arg(0) );
- }
- public function onSave($param)
- {
- $data = $this->form->getData();
- $this->form->setData($data);
- try
- {
- TTransaction::open('marca');
- foreach ($this->form->getFields() as $name => $field)
- {
- if ($field instanceof TCheckButton)
- {
- if ($field->getValue() == 'on')
- {
- $obj = new Exame();
- $obj->check_ = 'on';
- $obj->store();
- }
- }
- }
- TTransaction::close();
- }
- catch (Exception $e)
- {
- // show the message
- new TMessage('error', $e->getMessage());
- }
- }
- /**
- * Remove a product from the cart
- */
- public function onDelete($param)
- {
- // get the cart objects from session
- $cart_objects = TSession::getValue('cart_objects');
- unset($cart_objects[$param['key']]);
- TSession::setValue('cart_objects', $cart_objects);
- // reload datagrids
- $this->onReload( func_get_arg(0) );
- }
- /**
- * method onSearch()
- * Register the filter in the session when the user performs a search
- */
- function onSearch()
- {
- // get the search form data
- $data = $this->form->getData();
- var_dump($data);
- // check if the user has filled the form
- if ($data->paciente)
- {
- // creates a filter using what the user has typed
- $filter = new TFilter('paciente', 'like', "%{$data->paciente}%");
- // stores the filter in the session
- TSession::setValue('product_filter1', $filter);
- TSession::setValue('product_description', $data->paciente);
- }
- else
- {
- TSession::setValue('product_filter1', NULL);
- TSession::setValue('product_description', '');
- }
- // fill the form with data again
- $this->form->setData($data);
- $param=array();
- $param['offset'] =0;
- $param['first_page']=1;
- $this->onReload($param);
- }
- /**
- * method onReload()
- * Load the datagrid with the database objects
- */
- function onReload($param = NULL)
- {
- try
- {
- // open a transaction with database 'samples'
- TTransaction::open('marca');
- // creates a repository for Product
- $repository = new TRepository('Exame');
- $limit = 10;
- // creates a criteria
- $criteria = new TCriteria;
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('limit', $limit);
- $criteria->setProperty('order', 'paciente');
- if (TSession::getValue('product_filter1'))
- {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('product_filter1'));
- }
- $conn = TTransaction::get();
- $sth = $conn->prepare("SELECT * FROM exame GROUP BY paciente");
- $sth->execute();
- $resultado = $sth->fetchAll(PDO::FETCH_CLASS);
- $this->datagrid->clear();
- if ($resultado)
- {
- foreach ($resultado as $product)
- {
- $criteria = new TCriteria;
- $criteria->add(new TFilter('CARTAO', '=', $product->paciente));
- // load using repository
- $repository = new TRepository('Paciente');
- $customers = $repository->load($criteria);
- foreach ($customers as $obj)
- {
- $obj->status = $product->status;
- $this->datagrid->addItem($obj);
- }
- }
- }
- if(isset($param['CARTAO']))
- {
- $criteria = new TCriteria;
- $criteria->add(new TFilter('paciente', '=', $param['CARTAO']));
- $repository = new TRepository('Exame');
- $customers = $repository->load($criteria);
- $this->cartgrid->clear();
- if ($customers)
- {
- foreach ($customers as $item)
- {
- $item->check_ = new TCheckButton('check'.$item->id);
- $item->check_->setIndexValue('on');
- $this->cartgrid->addItem($item);
- $this->form->addField($item->check_);
- }
- }
- }
- // 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();
- }
- }
- /**
- * method show()
- * Shows the page
- */
- function show()
- {
- // check if the datagrid is already loaded
- if (!$this->loaded)
- {
- $this->onReload( func_get_arg(0) );
- }
- parent::show();
- }
- }
- ?>
Se está chamando a função onSave por uma ação da grid, não vai funcionar. As ações da grid fazem um get, não enviando as informações do formulário. Você precisa criar um botão, do mesmo modo que o "Busca".
Além disso, a organização dos containers também precisa ser ajustada. A grid precisa ser filha do formulário e atualmente a estrutura está assim:
1 - table
tr -> td -> form
tr -> td -> grid
tr -> td -> navigation
Somente os campos que estiverem dentro da tag form serão enviados no post.
Nataniel Rabaioli ainda não consegui nada, se você tiver como gerar algum modelo ou exemplo, estou criando software para laboratorio, na primeira grid apresenta nomes e informações do paciente, na segunda grid tem que mostrar os exame por paciente e confirmar coleta através do TCheckButton. Fico grato pela atenção.
Nataniel Rabaioli ainda não consegui nada, se você tiver como gerar algum modelo ou exemplo, estou criando software para laboratorio, na primeira grid apresenta nomes e informações do paciente, na segunda grid tem que mostrar os exame por paciente e confirmar coleta através do TCheckButton. Fico grato pela atenção.
Veja o exemplo abaixo:
https://adianti.com.br/framework_files/tutor/index.php?class=DatagridCheckView
A datagrid é adicionada a um panel que por sua vez é adicionado ao form. É mais ou menos isso que você precisa, pois a grid precisa ser filha de form para que os checks sejam enviados.
Se tiver dúvidas inspecione o código fonte, os checks devem estar dentro da tag form.
E na onSave use $param para recuperar os dados ao invés de $this->form->getData().
Nataniel Rabaioli seguindo o modelo do tutor "https://adianti.com.br/framework_files/tutor/index.php?class=MultiCheckView" mas ainda não consegui. Se puder ajudar desta vez eu não consigo fazer a carga das grids.
https://drive.google.com/file/d/1LH9pCLgL6B4eHqm5qhSWXy0uQ17I0I32/view?usp=sharing
Resumo: seleção da primeira grid por paciente gera uma segunda grid alimentada por TCheckButton coleta e resultado de lista de exames por pessoa.
Se alguém quiser ajudar e cobrar eu gostaria de entrar em contato.