TCheckButton -> Você já adicionou um campo chamado... ...
AR
TCheckButton -> Você já adicionou um campo chamado...  
Fechado
 
  1. <?php
  2. /**
  3. * DatagridQuickView
  4. *
  5. * @version 1.0
  6. * @package samples
  7. * @subpackage tutor
  8. * @author Pablo Dall'Oglio
  9. * @copyright Copyright (c) 2006-2014 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class SaleListTStatusC extends TPage
  13. {
  14. private $form;
  15. private $datagrid;
  16. private $pageNavigation;
  17. private $loaded;
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. $this->form = new TForm;
  22. // creates one datagrid
  23. $this->datagrid = new TQuickGrid;
  24. $this->datagrid->disableDefaultClick(); // important!
  25. $this->form->add($this->datagrid);
  26. $this->datagrid->addQuickColumn('Check', 'check', 'left', 40) ;
  27. $this->datagrid->addQuickColumn('Id', 'id', 'left', 40) ;
  28. $this->datagrid->addQuickColumn('Distribuidor', 'usuariocliente_nome', 'left', 300) ;
  29. // $this->datagrid->addQuickColumn(_t('Status'), 'status' , 'left', 100);
  30. $status=$this->datagrid->addQuickColumn(_t('Status'), 'status' , 'left', 100);
  31. //$this->datagrid->addQuickColumn('Data', 'date', 'left', 80);
  32. $date=$this->datagrid->addQuickColumn('Data', 'date', 'left', 80);
  33. // $this->datagrid->addQuickColumn('Valor', 'total', 'left', 80);
  34. $total=$this->datagrid->addQuickColumn('Valor', 'total', 'left', 80);
  35. // creates the datagrid model
  36. $this->datagrid->createModel();
  37. $total->setTransformer(array($this, 'formatMonetario'));
  38. $date->setTransformer(array($this, 'formatDate'));
  39. $status->setTransformer( array($this, 'setStatusColor') );
  40. // creates the action button
  41. $button1=new TButton('action1');
  42. // define the button action
  43. $button1->setAction(new TAction(array($this, 'onLibera')), 'Libera Pedido');
  44. $button1->setImage('fa:arrow-circle-right blue');
  45. $button1->popover = 'true';
  46. $button1->poptitle = 'Ajuda';
  47. $button1->popcontent = 'Libera pedido(s) para podermos processalos.';
  48. // creates the page navigation
  49. $this->pageNavigation = new TPageNavigation;
  50. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  51. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  52. $this->form->addField($button1);
  53. // wrap the page content using vertical box
  54. $vbox = new TVBox;
  55. $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  56. $vbox->add($this->form);
  57. $vbox->add($button1);
  58. parent::add($vbox);
  59. }
  60. public function setStatusColor($status, $object, $row)
  61. {
  62. $color = $object->status_color;
  63. if ($color)
  64. {
  65. return "<span style='color:$color'>" . $status . '</span>';
  66. }
  67. else
  68. {
  69. return $status;
  70. }
  71. }
  72. public function formatDate($datalanc, $object)
  73. {
  74. // print_r($datalanc);
  75. // $date = new DateTime($object->$datalanc);
  76. $date = new DateTime($datalanc);
  77. return $date->format('d/m/Y');
  78. }
  79. /**
  80. * Format Monetários
  81. */
  82. public function formatMonetario($valor_mon, $object, $row)
  83. {
  84. $number = number_format($valor_mon, 2, ',', '.');
  85. return "<span style='color:blue'>$number</span>";
  86. }
  87. /**
  88. * Format Monetários
  89. */
  90. public function formatPercentual($valor_perc, $object, $row)
  91. {
  92. $number = number_format($valor_perc, 3, ',', '.');
  93. return "<span style='color:blue'>$number</span>";
  94. }
  95. /**
  96. * Load the data into the datagrid
  97. */
  98. function onReload()
  99. {
  100. $objeto = $this->form->getData(); // optional parameter: active record class
  101. // put the data back to the form
  102. $this->form->setData($objeto);
  103. try
  104. {
  105. // open a transaction with database 'samples'
  106. // TTransaction::open('catalogo');
  107. //----------------------------------------------------------
  108. TTransaction::open('catalogo');
  109. // TTransaction::setLogger(new TLoggerSTD); // standard output
  110. // TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  111. $user_id='';
  112. $user_name ='';
  113. $user = SystemUser::newFromLogin(TSession::getValue('login') );
  114. if ($user)
  115. {
  116. $user_id = $user->id;
  117. $user_name = $user->name;
  118. }
  119. TTransaction::close();
  120. //-------------------------------------------------------------
  121. TTransaction::open('catalogo');
  122. // get the search form data
  123. // creates a repository for Contrato
  124. $repository = new TRepository('Sale');
  125. $limit = 10;
  126. // creates a criteria
  127. $criteria = new TCriteria;
  128. // default order
  129. if (empty($param['order']))
  130. {
  131. $param['order'] = 'id';
  132. $param['direction'] = 'asc';
  133. }
  134. $criteria->add(new TFilter('system_user_id', '=', $objeto->id = $user_id),TExpression::AND_OPERATOR);
  135. $criteria->add(new TFilter('id_status', '=','1'));
  136. $criteria->setProperties($param); // order, offset
  137. $criteria->setProperty('limit', $limit);
  138. // load the objects according to criteria
  139. $objects = $repository->load($criteria, FALSE);
  140. // print_r($objects);
  141. // $objects = $repository->load($criteria, FALSE);
  142. $this->datagrid->clear();
  143. if ($objects)
  144. {
  145. foreach ($objects as $object)
  146. {
  147. $object->check = new TCheckButton('check_'.$object->id);
  148. $object->check->setIndexValue('on');
  149. $this->form->addField($object->check); // important!
  150. $this->datagrid->addItem($object);
  151. }
  152. }
  153. // reset the criteria for record count
  154. $criteria->resetProperties();
  155. $count= $repository->count($criteria);
  156. $this->pageNavigation->setCount($count); // count of records
  157. $this->pageNavigation->setProperties($param); // order, page
  158. $this->pageNavigation->setLimit($limit); // limit
  159. // close the transaction
  160. TTransaction::close();
  161. $this->loaded = true;
  162. }
  163. catch (Exception $e) // in case of exception
  164. {
  165. // shows the exception error message
  166. new TMessage('error', $e->getMessage());
  167. // undo all pending operations
  168. TTransaction::rollback();
  169. }
  170. }
  171. /**
  172. * Simulates an save button
  173. * Show the form content
  174. */
  175. public function onLibera($param)
  176. {
  177. $data = $this->form->getData(); // optional parameter: active record class
  178. // put the data back to the form
  179. $this->form->setData($data);
  180. // print_r($data);
  181. try
  182. {
  183. TTransaction::open('catalogo');
  184. foreach ($this->form->getFields() as $name => $field)
  185. {
  186. if ($field instanceof TCheckButton)
  187. {
  188. $parts = explode('_', $name);
  189. 2495 = $parts[1];
  190. // print_r($parts);
  191. if ($field->getValue() == 'on')
  192. {
  193. $obj_pedidos = new Sale(2495);
  194. $obj_pedidos->id_status = '9';
  195. $obj_pedidos->store(); // stores th
  196. // print_r('opa');
  197. new TMessage('info', 'Pedido(s) liberado(s) para processamento');
  198. }
  199. }
  200. }
  201. TTransaction::close();
  202. }
  203. catch (Exception $e)
  204. {
  205. // show the message
  206. new TMessage('error', $e->getMessage());
  207. }
  208. $this->datagrid->clear();
  209. $param=array();
  210. $param['offset'] =0;
  211. $param['first_page']=1;
  212. $this->onReload($param);
  213. }
  214. /**
  215. * shows the page
  216. */
  217. function show()
  218. {
  219. $this->onReload();
  220. parent::show();
  221. }
  222. }
  223. 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.

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (6)


NR

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:
 
  1. <?php
  2. public function show()
  3. {
  4. // check if the datagrid is already loaded
  5. if (!$this->loaded AND (!isset($_GET['method']) OR !(in_array($_GET['method'], array('onReload', 'onSearch')))) )
  6. {
  7. if (func_num_args() > 0)
  8. {
  9. $this->onReload( func_get_arg(0) );
  10. }
  11. else
  12. {
  13. $this->onReload();
  14. }
  15. }
  16. parent::show();
  17. }
  18. ?>
AR

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.
NR

Essa função que passei não funcionou?
AR

Não funcionou, continuo recebendo a mesma mensagem.
NR

Tente adicionar esse if antes do addField na onReload:
 
  1. <?php
  2. if (! $this->loaded)
  3. $this->form->addField($object->check);
  4. ?>
AR

Agora funcionou perfeitamente. Muito Obrigado Nataniel.