Exibir Imgem no Grid Pessoal, criei no Adianti Studio um cadastro de categorias e neste tem a opção de incluir uma imagem. O Upload é feito normal, onde constatei a existência da imagem na pasta tmp do meu servidor e na tabela gravado o nome do arquivo como segue (van_sat203x54.png). Mas não está aparecendo a imagem no gri da listagem. Alguém ajuda por gentileza. Segue abaixo o código da Listagem de Categori...
PS
Exibir Imgem no Grid  
Pessoal, criei no Adianti Studio um cadastro de categorias e neste tem a opção de incluir uma imagem. O Upload é feito normal, onde constatei a existência da imagem na pasta tmp do meu servidor e na tabela gravado o nome do arquivo como segue (van_sat203x54.png). Mas não está aparecendo a imagem no gri da listagem. Alguém ajuda por gentileza.
Segue abaixo o código da Listagem de Categorias:

 
  1. <?php
  2. /**
  3. * PedCategoriasLista Listing
  4. * @author <your name here>
  5. */
  6. class PedCategoriasLista extends TPage
  7. {
  8. private $form; // form
  9. private $datagrid; // listing
  10. private $pageNavigation;
  11. private $formgrid;
  12. private $loaded;
  13. private $deleteButton;
  14. /**
  15. * Class constructor
  16. * Creates the page, the form and the listing
  17. */
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. // creates the form
  22. $this->form = new BootstrapFormBuilder('form_PedCategorias');
  23. $this->form->setFormTitle('PedCategorias');
  24. // create the form fields
  25. $id = new TEntry('id');
  26. $ds_categoria = new TEntry('ds_categoria');
  27. // add the fields
  28. $this->form->addFields( [ new TLabel('Código') ], [ $id ] );
  29. $this->form->addFields( [ new TLabel('Categoria') ], [ $ds_categoria ] );
  30. // set sizes
  31. $id->setSize('100%');
  32. $ds_categoria->setSize('100%');
  33. // keep the form filled during navigation with session data
  34. $this->form->setData( TSession::getValue('PedCategorias_filter_data') );
  35. // add the search form actions
  36. $btn = $this->form->addAction(_t('Find'), new TAction([$this, 'onSearch']), 'fa:search');
  37. $btn->class = 'btn btn-sm btn-primary';
  38. $this->form->addActionLink(_t('New'), new TAction(['PedCategoriasForm', 'onEdit']), 'fa:plus green');
  39. // creates a Datagrid
  40. $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  41. $this->datagrid->style = 'width: 100%';
  42. $this->datagrid->datatable = 'true';
  43. // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  44. // creates the datagrid columns
  45. $column_id = new TDataGridColumn('id', 'Código', 'right', 30);
  46. $column_ds_categoria = new TDataGridColumn('ds_categoria', 'Categoria', 'left');
  47. $column_img_catego = new TDataGridColumn('img_catego', 'Imagem', 'center');
  48. // add the columns to the DataGrid
  49. $this->datagrid->addColumn($column_id);
  50. $this->datagrid->addColumn($column_ds_categoria);
  51. $this->datagrid->addColumn($column_img_catego);
  52. // creates the datagrid column actions
  53. $column_id->setAction(new TAction([$this, 'onReload']), ['order' => 'id']);
  54. $column_ds_categoria->setAction(new TAction([$this, 'onReload']), ['order' => 'ds_categoria']);
  55. // define the transformer method over image
  56. $column_ds_categoria->setTransformer( function($value, $object, $row) {
  57. return strtoupper($value);
  58. });
  59. // define the transformer method over image
  60. $column_img_catego->setTransformer( function($value, $object, $row) {
  61. if (file_exists($value)) {
  62. return new TImage($value);
  63. }
  64. });
  65. // create EDIT action
  66. $action_edit = new TDataGridAction(['PedCategoriasForm', 'onEdit']);
  67. //$action_edit->setUseButton(TRUE);
  68. //$action_edit->setButtonClass('btn btn-default');
  69. $action_edit->setLabel(_t('Edit'));
  70. $action_edit->setImage('fa:pencil-square-o blue fa-lg');
  71. $action_edit->setField('id');
  72. $this->datagrid->addAction($action_edit);
  73. // create DELETE action
  74. $action_del = new TDataGridAction(array($this, 'onDelete'));
  75. //$action_del->setUseButton(TRUE);
  76. //$action_del->setButtonClass('btn btn-default');
  77. $action_del->setLabel(_t('Delete'));
  78. $action_del->setImage('fa:trash-o red fa-lg');
  79. $action_del->setField('id');
  80. $this->datagrid->addAction($action_del);
  81. // create the datagrid model
  82. $this->datagrid->createModel();
  83. // creates the page navigation
  84. $this->pageNavigation = new TPageNavigation;
  85. $this->pageNavigation->setAction(new TAction([$this, 'onReload']));
  86. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  87. // vertical box container
  88. $container = new TVBox;
  89. $container->style = 'width: 90%';
  90. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  91. $container->add($this->form);
  92. $container->add(TPanelGroup::pack('', $this->datagrid, $this->pageNavigation));
  93. parent::add($container);
  94. }
  95. /**
  96. * Inline record editing
  97. * @param $param Array containing:
  98. * key: object ID value
  99. * field name: object attribute to be updated
  100. * value: new attribute content
  101. */
  102. public function onInlineEdit($param)
  103. {
  104. try
  105. {
  106. // get the parameter $key
  107. $field = $param['field'];
  108. $key = $param['key'];
  109. $value = $param['value'];
  110. TTransaction::open('sispedidos'); // open a transaction with database
  111. $object = new PedCategorias($key); // instantiates the Active Record
  112. $object->{$field} = $value;
  113. $object->store(); // update the object in the database
  114. TTransaction::close(); // close the transaction
  115. $this->onReload($param); // reload the listing
  116. new TMessage('info', "Record Updated");
  117. }
  118. catch (Exception $e) // in case of exception
  119. {
  120. new TMessage('error', $e->getMessage()); // shows the exception error message
  121. TTransaction::rollback(); // undo all pending operations
  122. }
  123. }
  124. /**
  125. * Register the filter in the session
  126. */
  127. public function onSearch()
  128. {
  129. // get the search form data
  130. $data = $this->form->getData();
  131. // clear session filters
  132. TSession::setValue('PedCategoriasLista_filter_id', NULL);
  133. TSession::setValue('PedCategoriasLista_filter_ds_categoria', NULL);
  134. if (isset($data->id) AND ($data->id)) {
  135. $filter = new TFilter('id', '=', "$data->id"); // create the filter
  136. TSession::setValue('PedCategoriasLista_filter_id', $filter); // stores the filter in the session
  137. }
  138. if (isset($data->ds_categoria) AND ($data->ds_categoria)) {
  139. $filter = new TFilter('ds_categoria', 'like', "%{$data->ds_categoria}%"); // create the filter
  140. TSession::setValue('PedCategoriasLista_filter_ds_categoria', $filter); // stores the filter in the session
  141. }
  142. // fill the form with data again
  143. $this->form->setData($data);
  144. // keep the search data in the session
  145. TSession::setValue('PedCategorias_filter_data', $data);
  146. $param = array();
  147. $param['offset'] =0;
  148. $param['first_page']=1;
  149. $this->onReload($param);
  150. }
  151. /**
  152. * Load the datagrid with data
  153. */
  154. public function onReload($param = NULL)
  155. {
  156. try
  157. {
  158. // open a transaction with database 'sispedidos'
  159. TTransaction::open('sispedidos');
  160. // creates a repository for PedCategorias
  161. $repository = new TRepository('PedCategorias');
  162. $limit = 10;
  163. // creates a criteria
  164. $criteria = new TCriteria;
  165. // default order
  166. if (empty($param['order']))
  167. {
  168. $param['order'] = 'id';
  169. $param['direction'] = 'asc';
  170. }
  171. $criteria->setProperties($param); // order, offset
  172. $criteria->setProperty('limit', $limit);
  173. if (TSession::getValue('PedCategoriasLista_filter_id')) {
  174. $criteria->add(TSession::getValue('PedCategoriasLista_filter_id')); // add the session filter
  175. }
  176. if (TSession::getValue('PedCategoriasLista_filter_ds_categoria')) {
  177. $criteria->add(TSession::getValue('PedCategoriasLista_filter_ds_categoria')); // add the session filter
  178. }
  179. // load the objects according to criteria
  180. $objects = $repository->load($criteria, FALSE);
  181. if (is_callable($this->transformCallback))
  182. {
  183. call_user_func($this->transformCallback, $objects, $param);
  184. }
  185. $this->datagrid->clear();
  186. if ($objects)
  187. {
  188. // iterate the collection of active records
  189. foreach ($objects as $object)
  190. {
  191. // add the object inside the datagrid
  192. $this->datagrid->addItem($object);
  193. }
  194. }
  195. // reset the criteria for record count
  196. $criteria->resetProperties();
  197. $count= $repository->count($criteria);
  198. $this->pageNavigation->setCount($count); // count of records
  199. $this->pageNavigation->setProperties($param); // order, page
  200. $this->pageNavigation->setLimit($limit); // limit
  201. // close the transaction
  202. TTransaction::close();
  203. $this->loaded = true;
  204. }
  205. catch (Exception $e) // in case of exception
  206. {
  207. // shows the exception error message
  208. new TMessage('error', $e->getMessage());
  209. // undo all pending operations
  210. TTransaction::rollback();
  211. }
  212. }
  213. /**
  214. * Ask before deletion
  215. */
  216. public static function onDelete($param)
  217. {
  218. // define the delete action
  219. $action = new TAction([__CLASS__, 'Delete']);
  220. $action->setParameters($param); // pass the key parameter ahead
  221. // shows a dialog to the user
  222. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  223. }
  224. /**
  225. * Delete a record
  226. */
  227. public static function Delete($param)
  228. {
  229. try
  230. {
  231. $key=$param['key']; // get the parameter $key
  232. TTransaction::open('sispedidos'); // open a transaction with database
  233. $object = new PedCategorias($key, FALSE); // instantiates the Active Record
  234. $object->delete(); // deletes the object from the database
  235. TTransaction::close(); // close the transaction
  236. $pos_action = new TAction([__CLASS__, 'onReload']);
  237. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted'), $pos_action); // success message
  238. }
  239. catch (Exception $e) // in case of exception
  240. {
  241. new TMessage('error', $e->getMessage()); // shows the exception error message
  242. TTransaction::rollback(); // undo all pending operations
  243. }
  244. }
  245. /**
  246. * method show()
  247. * Shows the page
  248. */
  249. public function show()
  250. {
  251. // check if the datagrid is already loaded
  252. if (!$this->loaded AND (!isset($_GET['method']) OR !(in_array($_GET['method'], array('onReload', 'onSearch')))) )
  253. {
  254. if (func_num_args() > 0)
  255. {
  256. $this->onReload( func_get_arg(0) );
  257. }
  258. else
  259. {
  260. $this->onReload();
  261. }
  262. }
  263. parent::show();
  264. }
  265. }

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


LA

Paulo, altera esta linha:

 
  1. <?php
  2. //Esta
  3. // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  4. //Para
  5. $this->datagrid->enablePopover('Foto principal', '<img src="{photo_path}" width="350" height="250">');
  6. ?>
PS

Luis, fiz a alteração porém continua sem mostrar
PS

Alguém mais pode dar uma ajuda?
Precisando com urgência...
PS

Só lembrando a todos, eu estou usando o Adianti Studio pra gerar as classes, os forms e as listas. A listagem do código acima também foi gerada no Adianti Studio.
NR

No banco de dados você gravou somente o nome da imagem, desconsiderando a pasta?

Nesse caso tem que informar pasta + nome da imagem no TImage, senão o navegador vai buscar na raiz do projeto e não encontra a imagem.
PS

Nataniel, poderia mostrar no código acima onde e como colocar a pasta. Sou iniciante no Adianti e estou com dificuldades.
NR

Você deve informar o caminho no construtor da classe TImage:
 
  1. <?php
  2. $pasta = 'tmp/'; // supondo que a pasta seja tmp
  3. return new TImage($pasta . $value);
  4. ?>
PS

Nataniel, consegui aqui, muito obrigado a você e todos que se empenharam neste post. Mais uma vez a família Adianti me deixou surpreso.