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

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 (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. ?>