AR
TCheckButton -> Você já adicionou um campo chamado...
Fechado
- <?php
- /**
- * DatagridQuickView
- *
- * @version 1.0
- * @package samples
- * @subpackage tutor
- * @author Pablo Dall'Oglio
- * @copyright Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
- * @license http://www.adianti.com.br/framework-license
- */
- class SaleListTStatusC extends TPage
- {
- private $form;
- private $datagrid;
- private $pageNavigation;
- private $loaded;
- public function __construct()
- {
- parent::__construct();
- $this->form = new TForm;
- // creates one datagrid
- $this->datagrid = new TQuickGrid;
- $this->datagrid->disableDefaultClick(); // important!
- $this->form->add($this->datagrid);
- $this->datagrid->addQuickColumn('Check', 'check', 'left', 40) ;
- $this->datagrid->addQuickColumn('Id', 'id', 'left', 40) ;
- $this->datagrid->addQuickColumn('Distribuidor', 'usuariocliente_nome', 'left', 300) ;
- // $this->datagrid->addQuickColumn(_t('Status'), 'status' , 'left', 100);
- $status=$this->datagrid->addQuickColumn(_t('Status'), 'status' , 'left', 100);
- //$this->datagrid->addQuickColumn('Data', 'date', 'left', 80);
- $date=$this->datagrid->addQuickColumn('Data', 'date', 'left', 80);
- // $this->datagrid->addQuickColumn('Valor', 'total', 'left', 80);
- $total=$this->datagrid->addQuickColumn('Valor', 'total', 'left', 80);
- // creates the datagrid model
- $this->datagrid->createModel();
- $total->setTransformer(array($this, 'formatMonetario'));
- $date->setTransformer(array($this, 'formatDate'));
- $status->setTransformer( array($this, 'setStatusColor') );
- // creates the action button
- $button1=new TButton('action1');
- // define the button action
- $button1->setAction(new TAction(array($this, 'onLibera')), 'Libera Pedido');
- $button1->setImage('fa:arrow-circle-right blue');
- $button1->popover = 'true';
- $button1->poptitle = 'Ajuda';
- $button1->popcontent = 'Libera pedido(s) para podermos processalos.';
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- $this->form->addField($button1);
- // wrap the page content using vertical box
- $vbox = new TVBox;
- $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $vbox->add($this->form);
- $vbox->add($button1);
- parent::add($vbox);
- }
- public function setStatusColor($status, $object, $row)
- {
- $color = $object->status_color;
- if ($color)
- {
- return "<span style='color:$color'>" . $status . '</span>';
- }
- else
- {
- return $status;
- }
- }
- public function formatDate($datalanc, $object)
- {
- // print_r($datalanc);
- // $date = new DateTime($object->$datalanc);
- $date = new DateTime($datalanc);
- return $date->format('d/m/Y');
- }
- /**
- * Format Monetários
- */
- public function formatMonetario($valor_mon, $object, $row)
- {
- $number = number_format($valor_mon, 2, ',', '.');
- return "<span style='color:blue'>$number</span>";
- }
- /**
- * Format Monetários
- */
- public function formatPercentual($valor_perc, $object, $row)
- {
- $number = number_format($valor_perc, 3, ',', '.');
- return "<span style='color:blue'>$number</span>";
- }
- /**
- * Load the data into the datagrid
- */
- function onReload()
- {
- $objeto = $this->form->getData(); // optional parameter: active record class
- // put the data back to the form
- $this->form->setData($objeto);
- try
- {
- // open a transaction with database 'samples'
- // TTransaction::open('catalogo');
- //----------------------------------------------------------
- TTransaction::open('catalogo');
- // TTransaction::setLogger(new TLoggerSTD); // standard output
- // TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
- $user_id='';
- $user_name ='';
- $user = SystemUser::newFromLogin(TSession::getValue('login') );
- if ($user)
- {
- $user_id = $user->id;
- $user_name = $user->name;
- }
- TTransaction::close();
- //-------------------------------------------------------------
- TTransaction::open('catalogo');
- // get the search form data
- // creates a repository for Contrato
- $repository = new TRepository('Sale');
- $limit = 10;
- // creates a criteria
- $criteria = new TCriteria;
- // default order
- if (empty($param['order']))
- {
- $param['order'] = 'id';
- $param['direction'] = 'asc';
- }
- $criteria->add(new TFilter('system_user_id', '=', $objeto->id = $user_id),TExpression::AND_OPERATOR);
- $criteria->add(new TFilter('id_status', '=','1'));
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('limit', $limit);
- // load the objects according to criteria
- $objects = $repository->load($criteria, FALSE);
- // print_r($objects);
- // $objects = $repository->load($criteria, FALSE);
- $this->datagrid->clear();
- if ($objects)
- {
- foreach ($objects as $object)
- {
- $object->check = new TCheckButton('check_'.$object->id);
- $object->check->setIndexValue('on');
- $this->form->addField($object->check); // important!
- $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', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * Simulates an save button
- * Show the form content
- */
- public function onLibera($param)
- {
- $data = $this->form->getData(); // optional parameter: active record class
- // put the data back to the form
- $this->form->setData($data);
- // print_r($data);
- try
- {
- TTransaction::open('catalogo');
- foreach ($this->form->getFields() as $name => $field)
- {
- if ($field instanceof TCheckButton)
- {
- $parts = explode('_', $name);
- 2495 = $parts[1];
- // print_r($parts);
- if ($field->getValue() == 'on')
- {
- $obj_pedidos = new Sale(2495);
- $obj_pedidos->id_status = '9';
- $obj_pedidos->store(); // stores th
- // print_r('opa');
- new TMessage('info', 'Pedido(s) liberado(s) para processamento');
- }
- }
- }
- TTransaction::close();
- }
- catch (Exception $e)
- {
- // show the message
- new TMessage('error', $e->getMessage());
- }
- $this->datagrid->clear();
- $param=array();
- $param['offset'] =0;
- $param['first_page']=1;
- $this->onReload($param);
- }
- /**
- * shows the page
- */
- function show()
- {
- $this->onReload();
- parent::show();
- }
- }
- Pessoal, estou com um problema nessa "TQuickGrid" com o campo check, quase tudo está funcionando perfeitamente, consigo selecionar, gravo o que quero no banco, mas quando vou remontar a tela, "Linha 305 " recebo essa mensagem: Você já adicionou um campo chamado "check_8" ao formulário, como mostra a imagem em anexo. Mas se não uso " $this->onReload($param);" a grid não é refeita. Podem me ajudar por favor ? Grato.
Adriano, o que acontece é que a função onReload está sendo chamada 2 vezes. Tente substituir o método show de sua classe pelo do trait AdiantiStandardListTrait, que já tem algumas verificações para impedir que isso aconteça:
Olá Nataniel, obrigado por sua resposta. Funciona bem quando tenho na gride um só registro, quando tenho mais de um, ocorre o erro, é apresentado a mensagem informando que "check concatenando com o a id que permaneceu na grid, ou seja que não foi selecionado" já está adicionado no formulário. segue anexo 1 e 2.
Essa função que passei não funcionou?
Não funcionou, continuo recebendo a mesma mensagem.
Tente adicionar esse if antes do addField na onReload:
Agora funcionou perfeitamente. Muito Obrigado Nataniel.