Editar registros usando TCheckGroup Estou com uma dúvida cruel aqui. Tentei de todas das maneiras editar um registro em um form com um TCheckGroup. Não consigo marcar as opcões no TCheckGroup do registro editado. Quem puder me ajudar, agradeceria muito. ...
CA
Editar registros usando TCheckGroup  
Fechado
Estou com uma dúvida cruel aqui. Tentei de todas das maneiras editar um registro em um form com um TCheckGroup. Não consigo marcar as opcões no TCheckGroup do registro editado.
Quem puder me ajudar, agradeceria muito.

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


FC

Posta o código.
CA

Só pra explicar melhor a situação tenho uma tabela com os meses do ano (campos janeiro, fevereiro, etc). Esses campos são numericos e recebem o valor 1 ou 0 para identificar se estão selecionados ou não. Já consegui gravar essas informações na tabela isto está ok. O problema e na Action OnEdit como farei para marcar os campos selecionados corretamente ao editar um registro. Segue o codigo da class completo.
 
  1. <?php class processosFormList extends TPage
  2. {
  3. protected $form; // form
  4. protected $datagrid; // datagrid
  5. protected $pageNavigation;
  6. protected $loaded;
  7. function __construct()
  8. {
  9. parent::__construct();
  10. // creates the form
  11. $this->form = new TForm('form_processos');
  12. // creates a table
  13. $table = new TTable;
  14. // add the table inside the form
  15. $this->form->add($table);
  16. // create the form fields
  17. $id = new TEntry('id');
  18. $processo = new TEntry('processo');
  19. $codsecretaria = new TDBCombo('codsecretaria', 'conex', 'secretaria', 'id', 'nome');
  20. $codfornecedor = new TDBCombo('codfornecedor', 'conex', 'fornecedor', 'id', 'nome');
  21. $codreceita = new TDBCombo('codreceita', 'conex', 'fontereceita', 'id', 'nome');
  22. $ano = new TEntry('ano');
  23. $meses = new TCheckGroup('meses');
  24. $previsaomensal = new TEntry('previsaomensal');
  25. $items = array();
  26. $items['1'] ='Jan';
  27. $items['2'] ='Fev';
  28. $items['3'] ='Mar';
  29. $items['4'] ='Abr';
  30. $items['5'] ='Mai';
  31. $items['6'] ='Jun';
  32. $items['7'] ='Jul';
  33. $items['8'] ='Ago';
  34. $items['9'] ='Set';
  35. $items['10'] ='Out';
  36. $items['11'] ='Nov';
  37. $items['12'] ='Dez';
  38. // define the sizes
  39. $id->setSize(20);
  40. $id->setEditable(FALSE);
  41. $processo->setSize(50);
  42. $codsecretaria->setSize(400);
  43. $codfornecedor->setSize(400);
  44. $codreceita->setSize(400);
  45. $ano->setSize(30);
  46. $meses->setLayout('horizontal');
  47. $meses->addItems($items);
  48. $meses->setValue(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
  49. $previsaomensal->setSize(80);
  50. $previsaomensal->setNumericMask(2,'.',',');
  51. // add one row for each form field
  52. $table->addRowSet( new TLabel('Código:'), $id );
  53. $table->addRowSet( new TLabel('Processo:'), $processo );
  54. $table->addRowSet( new TLabel('Secretaria:'), $codsecretaria );
  55. $table->addRowSet( new TLabel('Beneficiário:'), $codfornecedor );
  56. $table->addRowSet( new TLabel('Fonte da Receita:'), $codreceita );
  57. $table->addRowSet( new TLabel('Exercício:'), $ano );
  58. $table->addRowset( new TLabel('Meses:'), $meses );
  59. $table->addRowSet( new TLabel('Previsão Mensal (R$):'), $previsaomensal );
  60. // create an action button (save)
  61. $save_button=new TButton('save');
  62. $save_button->setAction(new TAction(array($this, 'onSave')), 'Salvar');
  63. $save_button->setImage('ico_save.png');
  64. // create an new button (edit with no parameters)
  65. $new_button=new TButton('new');
  66. $new_button->setAction(new TAction(array($this, 'onEdit')), 'Novo');
  67. $new_button->setImage('ico_new.png');
  68. $this->form->setFields(array($id,$processo,$codsecretaria,$codfornecedor,$codreceita,$ano,$meses,$previsaomensal,$save_button,$new_button));
  69. // creates a DataGrid
  70. $this->datagrid = new TDataGrid;
  71. $this->datagrid->setHeight(320);
  72. // creates the datagrid columns
  73. $id = new TDataGridColumn('id', 'Cod.', 'left', 20);
  74. $processo = new TDataGridColumn('processo', 'Processo', 'left', 40);
  75. $codsecretaria = new TDataGridColumn('secretaria_nome', 'Secretaria', 'left', 110);
  76. $codfornecedor = new TDataGridColumn('fornecedor_nome', 'Beneficiário', 'left', 110);
  77. $codreceita = new TDataGridColumn('fontereceita_nome', 'Fonte da Receita', 'left', 110);
  78. $ano = new TDataGridColumn('ano', 'Exerc', 'left', 15);
  79. $jan = new TDataGridColumn('jan', 'J', 'left', 10);
  80. $fev = new TDataGridColumn('fev', 'F', 'left', 10);
  81. $mar = new TDataGridColumn('mar', 'M', 'left', 10);
  82. $abr = new TDataGridColumn('abr', 'A', 'left', 10);
  83. $mai = new TDataGridColumn('mai', 'M', 'left', 10);
  84. $jun = new TDataGridColumn('jun', 'J', 'left', 10);
  85. $jul = new TDataGridColumn('jul', 'J', 'left', 10);
  86. $ago = new TDataGridColumn('ago', 'A', 'left', 10);
  87. $set = new TDataGridColumn('set', 'S', 'left', 10);
  88. $out = new TDataGridColumn('out', 'O', 'left', 10);
  89. $nov = new TDataGridColumn('nov', 'N', 'left', 10);
  90. $dez = new TDataGridColumn('dez', 'D', 'left', 10);
  91. $previsaomensal = new TDataGridColumn('previsaomensal', 'Previsão Mensal (R$)', 'left', 70);
  92. // add the columns to the DataGrid
  93. $this->datagrid->addColumn($id);
  94. $this->datagrid->addColumn($processo);
  95. $this->datagrid->addColumn($codsecretaria);
  96. $this->datagrid->addColumn($codfornecedor);
  97. $this->datagrid->addColumn($codreceita);
  98. $this->datagrid->addColumn($ano);
  99. $this->datagrid->addColumn($jan);
  100. $this->datagrid->addColumn($fev);
  101. $this->datagrid->addColumn($mar);
  102. $this->datagrid->addColumn($abr);
  103. $this->datagrid->addColumn($mai);
  104. $this->datagrid->addColumn($jun);
  105. $this->datagrid->addColumn($jul);
  106. $this->datagrid->addColumn($ago);
  107. $this->datagrid->addColumn($set);
  108. $this->datagrid->addColumn($out);
  109. $this->datagrid->addColumn($nov);
  110. $this->datagrid->addColumn($dez);
  111. $this->datagrid->addColumn($previsaomensal);
  112. // creates two datagrid actions
  113. $action1 = new TDataGridAction(array($this, 'onEdit'));
  114. $action1->setLabel('Editar');
  115. $action1->setImage('ico_edit.png');
  116. $action1->setField('id');
  117. $action2 = new TDataGridAction(array($this, 'onDelete'));
  118. $action2->setLabel('Deletar');
  119. $action2->setImage('ico_delete.png');
  120. $action2->setField('id');
  121. $action3 = new TDataGridAction(array($this, 'onPrevisao'));
  122. $action3->setLabel('Editar Previsão');
  123. $action3->setImage('ico_find.png');
  124. $action3->setField('id');
  125. // add the actions to the datagrid
  126. $this->datagrid->addAction($action1);
  127. $this->datagrid->addAction($action2);
  128. $this->datagrid->addAction($action3);
  129. // create the datagrid model
  130. $this->datagrid->createModel();
  131. // creates the page navigation
  132. $this->pageNavigation = new TPageNavigation;
  133. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  134. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  135. $buttons_box = new THBox;
  136. $buttons_box->add($save_button);
  137. $buttons_box->add($new_button);
  138. // create the page container
  139. $container = new TVBox;
  140. $container->add($this->form);
  141. $container->add($buttons_box);
  142. $container->add($this->datagrid);
  143. $container->add($this->pageNavigation);
  144. parent::add($container);
  145. }
  146. /**
  147. * method onReload()
  148. * Load the datagrid with the database objects
  149. */
  150. function onReload($param = NULL)
  151. {
  152. try
  153. {
  154. // open a transaction with database 'conex'
  155. TTransaction::open('conex');
  156. // creates a repository for processos
  157. $repository = new TRepository('processos');
  158. $limit = 10;
  159. // creates a criteria
  160. $criteria = new TCriteria;
  161. // default order
  162. if (empty($param['order']))
  163. {
  164. $param['order'] = 'id';
  165. $param['direction'] = 'asc';
  166. }
  167. $criteria->setProperties($param); // order, offset
  168. $criteria->setProperty('limit', $limit);
  169. if (TSession::getValue('processos_filter'))
  170. {
  171. // add the filter stored in the session to the criteria
  172. $criteria->add(TSession::getValue('processos_filter'));
  173. }
  174. // load the objects according to criteria
  175. $objects = $repository->load($criteria, FALSE);
  176. $this->datagrid->clear();
  177. if ($objects)
  178. {
  179. // iterate the collection of active records
  180. foreach ($objects as $object)
  181. {
  182. $item = new stdClass;
  183. $item->id = $object->id;
  184. $item->processo = $object->processo;
  185. $item->secretaria_nome = $object->secretaria_nome;
  186. $item->fornecedor_nome = $object->fornecedor_nome;
  187. $item->fontereceita_nome = $object->fontereceita_nome;
  188. $item->ano = $object->ano;
  189. if ($object->janeiro == 1){$item->jan = 'X';}else{$item->jan = '';}
  190. if ($object->fevereiro == 1){$item->fev = 'X';}else{$item->fev = '';}
  191. if ($object->marco == 1){$item->mar = 'X';}else{$item->mar = '';}
  192. if ($object->abril == 1){$item->abr = 'X';}else{$item->abr = '';}
  193. if ($object->maio == 1){$item->mai = 'X';}else{$item->mai = '';}
  194. if ($object->junho == 1){$item->jun = 'X';}else{$item->jun = '';}
  195. if ($object->julho == 1){$item->jul = 'X';}else{$item->jul = '';}
  196. if ($object->agosto == 1){$item->ago = 'X';}else{$item->ago = '';}
  197. if ($object->setembro == 1){$item->set = 'X';}else{$item->set = '';}
  198. if ($object->outubro == 1){$item->out = 'X';}else{$item->out = '';}
  199. if ($object->novembro == 1){$item->nov = 'X';}else{$item->nov = '';}
  200. if ($object->dezembro == 1){$item->dez = 'X';}else{$item->dez = '';}
  201. $item->previsaomensal = number_format($object->previsaomensal, 2, ',', '.');
  202. // add the object inside the datagrid
  203. $this->datagrid->addItem($item);
  204. }
  205. }
  206. // reset the criteria for record count
  207. $criteria->resetProperties();
  208. $count= $repository->count($criteria);
  209. $this->pageNavigation->setCount($count); // count of records
  210. $this->pageNavigation->setProperties($param); // order, page
  211. $this->pageNavigation->setLimit($limit); // limit
  212. // close the transaction
  213. TTransaction::close();
  214. $this->loaded = true;
  215. }
  216. catch (Exception $e) // in case of exception
  217. {
  218. // shows the exception error message
  219. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  220. // undo all pending operations
  221. TTransaction::rollback();
  222. }
  223. }
  224. /**
  225. * method onDelete()
  226. * executed whenever the user clicks at the delete button
  227. * Ask if the user really wants to delete the record
  228. */
  229. function onDelete($param)
  230. {
  231. // define the delete action
  232. $action = new TAction(array($this, 'Delete'));
  233. $action->setParameters($param); // pass the key parameter ahead
  234. // shows a dialog to the user
  235. new TQuestion(TAdiantiCoreTranslator::translate('Deseja realmente deletar o registro?'), $action);
  236. }
  237. /**
  238. * method Delete()
  239. * Delete a record
  240. */
  241. function Delete($param)
  242. {
  243. try
  244. {
  245. // get the parameter $key
  246. $key=$param['key'];
  247. // open a transaction with database 'conex'
  248. TTransaction::open('conex');
  249. // instantiates object processos
  250. $object = new processos($key);
  251. // deletes the object from the database
  252. $object->delete();
  253. // close the transaction
  254. TTransaction::close();
  255. // reload the listing
  256. $this->onReload( $param );
  257. // shows the success message
  258. new TMessage('info', 'Registro deletado');
  259. }
  260. catch (Exception $e) // in case of exception
  261. {
  262. // shows the exception error message
  263. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  264. // undo all pending operations
  265. TTransaction::rollback();
  266. }
  267. }
  268. /**
  269. * method onSave()
  270. * Executed whenever the user clicks at the save button
  271. */
  272. function onSave()
  273. {
  274. try
  275. {
  276. // open a transaction with database 'conex'
  277. TTransaction::open('conex');
  278. // get the form data
  279. $object = $this->form->getData();
  280. if ($object->id == '')
  281. {
  282. $processos = new Processos();
  283. }
  284. else
  285. {
  286. $processos = new Processos($object->id);
  287. }
  288. $processos->processo = $object->processo;
  289. $processos->codsecretaria = $object->codsecretaria;
  290. $processos->codfornecedor = $object->codfornecedor;
  291. $processos->codreceita = $object->codreceita;
  292. $processos->ano = $object->ano;
  293. $i = 0;
  294. $jan1 = 0;
  295. $fev1 = 0;
  296. $mar1 = 0;
  297. $abr1 = 0;
  298. $mai1 = 0;
  299. $jun1 = 0;
  300. $jul1 = 0;
  301. $ago1 = 0;
  302. $set1 = 0;
  303. $out1 = 0;
  304. $nov1 = 0;
  305. $dez1 = 0;
  306. foreach ($object->meses as $opcoes[])
  307. {
  308. if($opcoes[$i] == 1){$jan1 = 1;}
  309. if($opcoes[$i] == 2){$fev1 = 1;}
  310. if($opcoes[$i] == 3){$mar1 = 1;}
  311. if($opcoes[$i] == 4){$abr1 = 1;}
  312. if($opcoes[$i] == 5){$mai1 = 1;}
  313. if($opcoes[$i] == 6){$jun1 = 1;}
  314. if($opcoes[$i] == 7){$jul1 = 1;}
  315. if($opcoes[$i] == 8){$ago1 = 1;}
  316. if($opcoes[$i] == 9){$set1 = 1;}
  317. if($opcoes[$i] == 10){$out1 = 1;}
  318. if($opcoes[$i] == 11){$nov1 = 1;}
  319. if($opcoes[$i] == 12){$dez1 = 1;}
  320. $i++;
  321. }
  322. if($jan1 == 1){$processos->janeiro = 1;}else{$processos->janeiro = 0;}
  323. if($fev1 == 1){$processos->fevereiro = 1;}else{$processos->fevereiro = 0;}
  324. if($mar1 == 1){$processos->marco = 1;}else{$processos->marco = 0;}
  325. if($abr1 == 1){$processos->abril = 1;}else{$processos->abril = 0;}
  326. if($mai1 == 1){$processos->maio = 1;}else{$processos->maio = 0;}
  327. if($jun1 == 1){$processos->junho = 1;}else{$processos->junho = 0;}
  328. if($jul1 == 1){$processos->julho = 1;}else{$processos->julho = 0;}
  329. if($ago1 == 1){$processos->agosto = 1;}else{$processos->agosto = 0;}
  330. if($set1 == 1){$processos->setembro = 1;}else{$processos->setembro = 0;}
  331. if($out1 == 1){$processos->outubro = 1;}else{$processos->outubro = 0;}
  332. if($nov1 == 1){$processos->novembro = 1;}else{$processos->novembro = 0;}
  333. if($dez1 == 1){$processos->dezembro = 1;}else{$processos->dezembro = 0;}
  334. $prevmensal = str_replace(',', '', $object->previsaomensal);
  335. $prevmensal = str_replace(',', '', $prevmensal);
  336. $prevmensal = str_replace(',', '', $prevmensal);
  337. $prevmensal = number_format($prevmensal, 2, ".", "");
  338. $processos->previsaomensal = $prevmensal;
  339. $processos->store();
  340. // fill the form with the active record data
  341. //$this->form->setData($object);
  342. // close the transaction
  343. TTransaction::close();
  344. // shows the success message
  345. new TMessage('info', 'Registro salvo');
  346. // reload the listing
  347. $this->form->clear();
  348. $items1 = array();
  349. $items1['1'] ='Jan';
  350. $items1['2'] ='Fev';
  351. $items1['3'] ='Mar';
  352. $items1['4'] ='Abr';
  353. $items1['5'] ='Mai';
  354. $items1['6'] ='Jun';
  355. $items1['7'] ='Jul';
  356. $items1['8'] ='Ago';
  357. $items1['9'] ='Set';
  358. $items1['10'] ='Out';
  359. $items1['11'] ='Nov';
  360. $items1['12'] ='Dez';
  361. $obj = new StdClass;
  362. $obj->meses->addItems($items1);
  363. TForm::sendData('form_processos', $obj);
  364. $this->onReload();
  365. }
  366. catch (Exception $e) // in case of exception
  367. {
  368. // shows the exception error message
  369. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  370. // undo all pending operations
  371. TTransaction::rollback();
  372. }
  373. }
  374. /**
  375. * method onEdit()
  376. * Executed whenever the user clicks at the edit button da datagrid
  377. */
  378. function onEdit($param)
  379. {
  380. try
  381. {
  382. if (isset($param['key']))
  383. {
  384. // get the parameter $key
  385. $key=$param['key'];
  386. // open a transaction with database 'conex'
  387. TTransaction::open('conex');
  388. // instantiates object processos
  389. $object = new processos($key);
  390. // fill the form with the active record data
  391. $this->form->setData($object);
  392. // close the transaction
  393. TTransaction::close();
  394. }
  395. else
  396. {
  397. $this->form->clear();
  398. }
  399. }
  400. catch (Exception $e) // in case of exception
  401. {
  402. // shows the exception error message
  403. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  404. // undo all pending operations
  405. TTransaction::rollback();
  406. }
  407. }
  408. function onPrevisao($param)
  409. {
  410. }
  411. /**
  412. * method show()
  413. * Shows the page e seu conteúdo
  414. */
  415. function show()
  416. {
  417. // check if the datagrid is already loaded
  418. if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  419. {
  420. $this->onReload( func_get_arg(0) );
  421. }
  422. parent::show();
  423. }
  424. }?>
CA

Só como contribuição ao fórum, desculpe-me se não for a melhor solução mas consegui resolver desta forma. Segue evento onEdit com a rotina.
 
  1. <?php
function onEdit($param) { try { if (isset($param['key'])) { // get the parameter $key $key=$param['key']; // open a transaction with database 'conex' TTransaction::open('conex'); // instantiates object processos $object = new processos($key); //rotina para selecionar as opcoes marcadas no registro //criada variavel do tipo array $meses_check = array(); if ($object->janeiro == 1) {$meses_check[] = 1;} if ($object->fevereiro == 1) {$meses_check[] = 2;} if ($object->marco == 1) {$meses_check[] = 3;} if ($object->abril == 1) {$meses_check[] = 4;} if ($object->maio == 1) {$meses_check[] = 5;} if ($object->junho == 1) {$meses_check[] = 6;} if ($object->julho == 1) {$meses_check[] = 7;} if ($object->agosto == 1) {$meses_check[] = 8;} if ($object->setembro == 1) {$meses_check[] = 9;} if ($object->outubro == 1) {$meses_check[] = 10;} if ($object->novembro == 1) {$meses_check[] = 11;} if ($object->dezembro == 1) {$meses_check[] = 12;} //integra ao objeto instaciado do registro o nome controle do form //que irá receber como valor a variavel array criada $object->meses = $meses_check; // fill the form with the active record data $this->form->setData($object); // close the transaction TTransaction::close(); } else { $this->form->clear(); } } 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(); } }
?>
PD

Como você possui uma coluna para cada mês no banco de dados, acredito que essa é uma boa solução.
RS

Essa solução resolveu meu problema....ótima solução