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 !
- <?php
- /**
- * MdlUserList Listing
- * @author <Flavio da Rocha Maidl>
- */
- class MdlUserList extends TPage
- {
- private $form; // registration form
- private $datagrid; // listing
- private $pageNavigation;
- private $loaded;
- /**
- * Class constructor
- * Creates the page, the form and the listing
- */
- public function __construct()
- {
- parent::__construct();
- // creates the form
- $this->form = new TForm('form_search_MdlUser');
- // creates a table
- $table = new TTable;
- // add the table inside the form
- $this->form->add($table);
- // create the form fields
- $filter = new TEntry('firstname');
- $filter->setValue(TSession::getValue('MdlUser_firstname'));
- // add a row for the filter field
- $row=$table->addRowSet( new TLabel('firstname:'), $filter);
- // create two action buttons to the form
- $find_button = new TButton('find');
- $new_button = new TButton('new');
- $find_button->setAction(new TAction(array($this, 'onSearch')), _t('Find'));
- $new_button->setAction(new TAction(array('MdlUserForm', 'onEdit')), _t('New'));
- $find_button->setImage('ico_find.png');
- $new_button->setImage('ico_new.png');
- // add a row for the form actions
- $row=$table->addRowSet( $find_button, $new_button );
- // define wich are the form fields
- $this->form->setFields(array($filter, $find_button, $new_button));
- // creates a DataGrid
- $this->datagrid = new TDataGrid;
- $this->datagrid->setHeight(320);
- // creates the datagrid columns
- 939 = new TDataGridColumn('id', 'Código', 'right', 100);
- $username = new TDataGridColumn('username', 'Usuário', 'left', 200);
- $firstname = new TDataGridColumn('firstname', 'Nome', 'left', 200);
- $lastname = new TDataGridColumn('lastname', 'Sobrenome', 'left', 200);
- $city = new TDataGridColumn('city', 'Cidade', 'left', 200);
- $deleted = new TDataGridColumn('deleted', 'Excluido', 'left', 60);
- $deleted->setTransformer(array($this, 'formatDeleted'));
- // add the columns to the DataGrid
- $this->datagrid->addColumn(939);
- $this->datagrid->addColumn($username);
- $this->datagrid->addColumn($firstname);
- $this->datagrid->addColumn($lastname);
- $this->datagrid->addColumn($city);
- $this->datagrid->addColumn($deleted);
- // creates two datagrid actions
- $action1 = new TDataGridAction(array('MdlUserForm', 'onEdit'));
- $action1->setLabel(_t('Edit'));
- $action1->setImage('ico_edit.png');
- $action1->setField('id');
- $action2 = new TDataGridAction(array($this, 'onDelete'));
- $action2->setLabel(_t('Delete'));
- $action2->setImage('ico_delete.png');
- $action2->setField('id');
- // add the actions to the datagrid
- $this->datagrid->addAction($action1);
- $this->datagrid->addAction($action2);
- // create the datagrid model
- $this->datagrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // create the page container
- $container = new TVBox;
- $container->add($this->form);
- $container->add($this->datagrid);
- $container->add($this->pageNavigation);
- parent::add($container);
- }
- public function formatDeleted($apagado, $object)
- {
- if($object->deleted = "1")
- {
- $apa = 'SIM';
- }
- if($object->deleted = "0")
- {
- $apa = 'NÃO';
- }
- return $apa;
- }
- /**
- * method onInlineEdit()
- * Inline record editing
- * @param $param Array containing:
- * key: object ID value
- * field name: object attribute to be updated
- * value: new attribute content
- */
- function onInlineEdit($param)
- {
- try
- {
- // get the parameter $key
- $field = $param['field'];
- $key = $param['key'];
- $value = $param['value'];
- // open a transaction with database 'cadAluno'
- TTransaction::open('cadAluno');
- // instantiates object MdlUser
- $object = new MdlUser($key);
- // update the object in the database
- $object->{$field} = $value;
- $object->store();
- // close the transaction
- TTransaction::close();
- $this->onReload($param); // reload the listing
- new TMessage('info', "Record Updated");
- }
- 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();
- }
- }
- /**
- * method onSearch()
- * Register the filter in the session when the user performs a search
- */
- function onSearch()
- {
- // get the search form data
- $data = $this->form->getData();
- TSession::setValue('MdlUser_filter', NULL);
- TSession::setValue('MdlUser_firstname', '');
- // check if the user has filled the form
- if (isset($data->firstname) AND ($data->firstname))
- {
- // creates a filter using what the user has typed
- $filter = new TFilter('firstname', 'like', "%{$data->firstname}%");
- // stores the filter in the session
- TSession::setValue('MdlUser_filter', $filter);
- TSession::setValue('MdlUser_firstname', $data->firstname);
- }
- else
- {
- TSession::setValue('MdlUser_filter', NULL);
- TSession::setValue('MdlUser_firstname', '');
- }
- // fill the form with data again
- $this->form->setData($data);
- $param=array();
- $param['offset'] =0;
- $param['first_page']=1;
- $this->onReload($param);
- }
- /**
- * method onReload()
- * Load the datagrid with the database objects
- */
- function onReload($param = NULL)
- {
- try
- {
- // open a transaction with database 'cadAluno'
- TTransaction::open('cadAluno');
- // creates a repository for MdlUser
- $repository = new TRepository('MdlUser');
- $limit = 10;
- // creates a criteria
- $criteria = new TCriteria;
- // default order
- if (empty($param['order']))
- {
- $param['order'] = 'id';
- $param['direction'] = 'asc';
- }
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('limit', $limit);
- if (TSession::getValue('MdlUser_filter'))
- {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('MdlUser_filter'));
- }
- // load the objects according to criteria
- $objects = $repository->load($criteria, FALSE);
- $this->datagrid->clear();
- if ($objects)
- {
- // iterate the collection of active records
- foreach ($objects as $object)
- {
- // add the object inside the datagrid
- $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', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onDelete()
- * executed whenever the user clicks at the delete button
- * Ask if the user really wants to delete the record
- */
- function onDelete($param)
- {
- // define the delete action
- $action = new TAction(array($this, 'Delete'));
- $action->setParameters($param); // pass the key parameter ahead
- // shows a dialog to the user
- new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
- }
- /**
- * method Delete()
- * Delete a record
- */
- function Delete($param)
- {
- try
- {
- // get the parameter $key
- $key=$param['key'];
- // open a transaction with database 'cadAluno'
- TTransaction::open('cadAluno');
- // instantiates object MdlUser
- $object = new MdlUser($key);
- // deletes the object from the database
- $object->delete();
- // close the transaction
- TTransaction::close();
- // reload the listing
- $this->onReload( $param );
- // shows the success message
- new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted'));
- }
- 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();
- }
- }
- /**
- * method show()
- * Shows the page
- */
- function show()
- {
- // check if the datagrid is already loaded
- if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
- {
- $this->onReload( func_get_arg(0) );
- }
- parent::show();
- }
- }
- ?>
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...:
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:
Além disso, você pode verificar diretamente a variável $apagado (primeiro parâmetro).
Abraço,
Pablo
Ops, não terminei o serviço:
Ou ainda: