Lançado Adianti Framework 7.6!
Clique aqui para saber mais
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.

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