Lançado Adianti Framework 7.6!
Clique aqui para saber mais
problema no setTransform Tenho um campo em minha tabela que armazena o numero 0 ou o numero 1. Quando exibo a datagrid quero que apareça SIM no lugar do 1 e NÃO no lugar do 0. Utilizei a funcao setTransofrm mas nao funcionou... vou colar o codigo todo aqui .. obrigado ! ...
FM
problema no setTransform  
Fechado
Tenho um campo em minha tabela que armazena o numero 0 ou o numero 1. Quando exibo a datagrid quero que apareça SIM no lugar do 1 e NÃO no lugar do 0. Utilizei a funcao setTransofrm mas nao funcionou... vou colar o codigo todo aqui .. obrigado !

  1. <?php
  2. /**
  3.  * MdlUserList Listing
  4.  * @author  <Flavio da Rocha Maidl>
  5.  */
  6. class MdlUserList extends TPage
  7. {
  8.     private $form;     // registration form
  9.     private $datagrid// listing
  10.     private $pageNavigation;
  11.     private $loaded;
  12.     
  13.     /**
  14.      * Class constructor
  15.      * Creates the page, the form and the listing
  16.      */
  17.     public function __construct()
  18.     {
  19.         parent::__construct();
  20.         
  21.         // creates the form
  22.         $this->form = new TForm('form_search_MdlUser');
  23.         
  24.         // creates a table
  25.         $table = new TTable;
  26.         
  27.         // add the table inside the form
  28.         $this->form->add($table);
  29.         
  30.         // create the form fields
  31.         $filter = new TEntry('firstname');
  32.         $filter->setValue(TSession::getValue('MdlUser_firstname'));
  33.         
  34.         // add a row for the filter field
  35.         $row=$table->addRowSet( new TLabel('firstname:'), $filter);
  36.         
  37.         // create two action buttons to the form
  38.         $find_button = new TButton('find');
  39.         $new_button  = new TButton('new');
  40.         $find_button->setAction(new TAction(array($this'onSearch')), _t('Find'));
  41.         $new_button->setAction(new TAction(array('MdlUserForm''onEdit')), _t('New'));
  42.         $find_button->setImage('ico_find.png');
  43.         $new_button->setImage('ico_new.png');
  44.         
  45.         
  46.         // add a row for the form actions
  47.         $row=$table->addRowSet$find_button$new_button );
  48.         
  49.         // define wich are the form fields
  50.         $this->form->setFields(array($filter$find_button$new_button));
  51.         
  52.         // creates a DataGrid
  53.         $this->datagrid = new TDataGrid;
  54.         $this->datagrid->setHeight(320);
  55.         
  56.         // creates the datagrid columns
  57.         939   = new TDataGridColumn('id''Código''right'100);
  58.         $username   = new TDataGridColumn('username''Usuário''left'200);
  59.         $firstname   = new TDataGridColumn('firstname''Nome''left'200);
  60.         $lastname   = new TDataGridColumn('lastname''Sobrenome''left'200);
  61.         $city   = new TDataGridColumn('city''Cidade''left'200);
  62.         $deleted   = new TDataGridColumn('deleted''Excluido''left'60);
  63.         
  64.         $deleted->setTransformer(array($this'formatDeleted'));
  65.         // add the columns to the DataGrid
  66.         $this->datagrid->addColumn(939);
  67.         $this->datagrid->addColumn($username);
  68.         $this->datagrid->addColumn($firstname);
  69.         $this->datagrid->addColumn($lastname);
  70.         $this->datagrid->addColumn($city);
  71.         $this->datagrid->addColumn($deleted);
  72.         
  73.         // creates two datagrid actions
  74.         $action1 = new TDataGridAction(array('MdlUserForm''onEdit'));
  75.         $action1->setLabel(_t('Edit'));
  76.         $action1->setImage('ico_edit.png');
  77.         $action1->setField('id');
  78.         
  79.         $action2 = new TDataGridAction(array($this'onDelete'));
  80.         $action2->setLabel(_t('Delete'));
  81.         $action2->setImage('ico_delete.png');
  82.         $action2->setField('id');
  83.         
  84.         // add the actions to the datagrid
  85.         $this->datagrid->addAction($action1);
  86.         $this->datagrid->addAction($action2);
  87.         
  88.         // create the datagrid model
  89.         $this->datagrid->createModel();
  90.         
  91.         // creates the page navigation
  92.         $this->pageNavigation = new TPageNavigation;
  93.         $this->pageNavigation->setAction(new TAction(array($this'onReload')));
  94.         $this->pageNavigation->setWidth($this->datagrid->getWidth());
  95.         
  96.         // create the page container
  97.         $container = new TVBox;
  98.         $container->add($this->form);
  99.         $container->add($this->datagrid);
  100.         $container->add($this->pageNavigation);
  101.         
  102.         parent::add($container);
  103.     }
  104.   
  105.     public function formatDeleted($apagado$object)
  106.     {
  107.        
  108.         if($object->deleted "1")
  109.         {
  110.             $apa 'SIM';    
  111.         }
  112.         if($object->deleted "0")
  113.         {
  114.             $apa 'NÃO';
  115.         }
  116.         return $apa;
  117.     }
  118.     
  119.     /**
  120.      * method onInlineEdit()
  121.      * Inline record editing
  122.      * @param $param Array containing:
  123.      *              key: object ID value
  124.      *              field name: object attribute to be updated
  125.      *              value: new attribute content 
  126.      */
  127.     function onInlineEdit($param)
  128.     {
  129.         try
  130.         {
  131.             // get the parameter $key
  132.             $field $param['field'];
  133.             $key   $param['key'];
  134.             $value $param['value'];
  135.             
  136.             // open a transaction with database 'cadAluno'
  137.             TTransaction::open('cadAluno');
  138.             
  139.             // instantiates object MdlUser
  140.             $object = new MdlUser($key);
  141.             
  142.             // update the object in the database
  143.             $object->{$field} = $value;
  144.             $object->store();
  145.             
  146.             // close the transaction
  147.             TTransaction::close();
  148.             
  149.             
  150.             $this->onReload($param); // reload the listing
  151.             new TMessage('info'"Record Updated");
  152.         }
  153.         catch (Exception $e// in case of exception
  154.         {
  155.             // shows the exception error message
  156.             new TMessage('error''<b>Error</b> ' $e->getMessage());
  157.             // undo all pending operations
  158.             TTransaction::rollback();
  159.         }
  160.     }
  161.     
  162.     /**
  163.      * method onSearch()
  164.      * Register the filter in the session when the user performs a search
  165.      */
  166.     function onSearch()
  167.     {
  168.         // get the search form data
  169.         $data $this->form->getData();
  170.         
  171.         TSession::setValue('MdlUser_filter',   NULL);
  172.         TSession::setValue('MdlUser_firstname''');
  173.         
  174.         // check if the user has filled the form
  175.         if (isset($data->firstname) AND ($data->firstname))
  176.         {
  177.             // creates a filter using what the user has typed
  178.             $filter = new TFilter('firstname''like'"%{$data->firstname}%");
  179.             
  180.             // stores the filter in the session
  181.             TSession::setValue('MdlUser_filter',   $filter);
  182.             TSession::setValue('MdlUser_firstname'$data->firstname);
  183.         }
  184.         else
  185.         {
  186.             TSession::setValue('MdlUser_filter',   NULL);
  187.             TSession::setValue('MdlUser_firstname''');
  188.         }
  189.         
  190.         // fill the form with data again
  191.         $this->form->setData($data);
  192.         
  193.         $param=array();
  194.         $param['offset']    =0;
  195.         $param['first_page']=1;
  196.         $this->onReload($param);
  197.     }
  198.     
  199.     /**
  200.      * method onReload()
  201.      * Load the datagrid with the database objects
  202.      */
  203.     function onReload($param NULL)
  204.     {
  205.         try
  206.         {
  207.             // open a transaction with database 'cadAluno'
  208.             TTransaction::open('cadAluno');
  209.             
  210.             // creates a repository for MdlUser
  211.             $repository = new TRepository('MdlUser');
  212.             $limit 10;
  213.             // creates a criteria
  214.             $criteria = new TCriteria;
  215.             
  216.             // default order
  217.             if (empty($param['order']))
  218.             {
  219.                 $param['order'] = 'id';
  220.                 $param['direction'] = 'asc';
  221.             }
  222.             $criteria->setProperties($param); // order, offset
  223.             $criteria->setProperty('limit'$limit);
  224.             
  225.             if (TSession::getValue('MdlUser_filter'))
  226.             {
  227.                 // add the filter stored in the session to the criteria
  228.                 $criteria->add(TSession::getValue('MdlUser_filter'));
  229.             }
  230.             
  231.             // load the objects according to criteria
  232.             $objects $repository->load($criteriaFALSE);
  233.             
  234.             $this->datagrid->clear();
  235.             if ($objects)
  236.             {
  237.                 // iterate the collection of active records
  238.                 foreach ($objects as $object)
  239.                 {
  240.                     // add the object inside the datagrid
  241.                     $this->datagrid->addItem($object);
  242.                 }
  243.             }
  244.             
  245.             // reset the criteria for record count
  246.             $criteria->resetProperties();
  247.             $count$repository->count($criteria);
  248.             
  249.             $this->pageNavigation->setCount($count); // count of records
  250.             $this->pageNavigation->setProperties($param); // order, page
  251.             $this->pageNavigation->setLimit($limit); // limit
  252.             
  253.             // close the transaction
  254.             TTransaction::close();
  255.             $this->loaded true;
  256.         }
  257.         catch (Exception $e// in case of exception
  258.         {
  259.             // shows the exception error message
  260.             new TMessage('error''<b>Error</b> ' $e->getMessage());
  261.             
  262.             // undo all pending operations
  263.             TTransaction::rollback();
  264.         }
  265.     }
  266.     
  267.     /**
  268.      * method onDelete()
  269.      * executed whenever the user clicks at the delete button
  270.      * Ask if the user really wants to delete the record
  271.      */
  272.     function onDelete($param)
  273.     {
  274.         // define the delete action
  275.         $action = new TAction(array($this'Delete'));
  276.         $action->setParameters($param); // pass the key parameter ahead
  277.         
  278.         // shows a dialog to the user
  279.         new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  280.     }
  281.     
  282.     /**
  283.      * method Delete()
  284.      * Delete a record
  285.      */
  286.     function Delete($param)
  287.     {
  288.         try
  289.         {
  290.             // get the parameter $key
  291.             $key=$param['key'];
  292.             // open a transaction with database 'cadAluno'
  293.             TTransaction::open('cadAluno');
  294.             
  295.             // instantiates object MdlUser
  296.             $object = new MdlUser($key);
  297.             
  298.             // deletes the object from the database
  299.             $object->delete();
  300.             
  301.             // close the transaction
  302.             TTransaction::close();
  303.             
  304.             // reload the listing
  305.             $this->onReload$param );
  306.             // shows the success message
  307.             new TMessage('info'TAdiantiCoreTranslator::translate('Record deleted'));
  308.         }
  309.         catch (Exception $e// in case of exception
  310.         {
  311.             // shows the exception error message
  312.             new TMessage('error''<b>Error</b> ' $e->getMessage());
  313.             
  314.             // undo all pending operations
  315.             TTransaction::rollback();
  316.         }
  317.     }
  318.     
  319.     /**
  320.      * method show()
  321.      * Shows the page
  322.      */
  323.     function show()
  324.     {
  325.         // check if the datagrid is already loaded
  326.         if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
  327.         {
  328.             $this->onReloadfunc_get_arg(0) );
  329.         }
  330.         parent::show();
  331.     }
  332. }
  333. ?>

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


WG

Flavio,

Se você observar no código da classe TDatagrid (lib/adianti/widget/web/datagrid/TDatagrid.class.php), na linha 314 tem a chamado da função callback para o getTransformer() e ela tem o parâmetro $row que você não adicionou ao final da sua função callback.

Não sei se tá relacionado, mas pode ser isso. Mesmo não usando o parâmetro add ele aí na sua função pra ver se dá certo...:

  1. <?php
  2. public function formatDeleted($apagado$object$row)
  3. {
  4.    // ...
  5. }
  6. ?>
PD

Flavio,

No PHP usa-se dois "==" para comparação lógica. Senão é atribuição, retornando sempre TRUE neste caso. Corrigi para você, só não testei:
  1. <?php
  2.         if($object->deleted == '1')
  3.         {
  4.             return 'SIM';    
  5.         }
  6.         if($object->deleted '0')
  7.         {
  8.             return 'NÃO';
  9.         }
  10. ?>


Além disso, você pode verificar diretamente a variável $apagado (primeiro parâmetro).

Abraço,
Pablo
PD

Ops, não terminei o serviço:

  1. <?php
  2.         if($object->deleted == '1')
  3.         {
  4.             return 'SIM';    
  5.         }
  6.         else
  7.         {
  8.             return 'NÃO';
  9.         }
  10. ?>


Ou ainda:
  1. <?php
  2. return ($apagado == '1') ? 'Sim' 'Não';
  3. ?>