grid Listagem de registro não exibe imagem Não estou conseguindo mostrar uma imagem em um grid, alguém pode ajudar? Com o adianti studio, no menu "Nova pagina/Nova listagem de registros" gerei o formulário, porém ele não mostra a imagem correspondente que está salva na raiz do projeto na pasta " /tmp" ...
MC
grid Listagem de registro não exibe imagem  
Não estou conseguindo mostrar uma imagem em um grid, alguém pode ajudar?
Com o adianti studio, no menu "Nova pagina/Nova listagem de registros" gerei o formulário, porém ele não mostra a imagem correspondente que está salva na raiz do projeto na pasta " /tmp"
 
  1. <?php
  2. /**
  3. * Imagem_marcadorList Listing
  4. * @author <your name here>
  5. */
  6. class Imagem_marcadorList extends TStandardList
  7. {
  8. protected $form; // registration form
  9. protected $datagrid; // listing
  10. protected $pageNavigation;
  11. protected $formgrid;
  12. protected $deleteButton;
  13. protected $transformCallback;
  14. /**
  15. * Page constructor
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. parent::setDatabase('bancopatrulharural'); // defines the database
  21. parent::setActiveRecord('Imagem_marcador'); // defines the active record
  22. parent::setDefaultOrder('id', 'asc'); // defines the default order
  23. // parent::setCriteria($criteria) // define a standard filter
  24. // creates the form
  25. $this->form = new TQuickForm('form_search_Imagem_marcador');
  26. $this->form->class = 'tform'; // change CSS class
  27. $this->form->style = 'display: table;width:100%'; // change style
  28. $this->form->setFormTitle('Imagem_marcador');
  29. // keep the form filled during navigation with session data
  30. // $this->form->setData( TSession::getValue('Imagem_marcador_filter_data') );
  31. // add the search form actions
  32. $this->form->addQuickAction(_t('Find'), new TAction(array($this, 'onSearch')), 'fa:search');
  33. $this->form->addQuickAction(_t('New'), new TAction(array('Imagem_marcadorForm', 'onEdit')), 'bs:plus-sign green');
  34. // creates a DataGrid
  35. $this->datagrid = new TDataGrid;
  36. $this->datagrid->style = 'width: 100%';
  37. $this->datagrid->datatable = 'true';
  38. // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  39. // creates the datagrid columns
  40. $column_id = new TDataGridColumn('id', 'Id', 'right');
  41. $column_nome = new TDataGridColumn('nome', 'Nome', 'left');
  42. $column_imagem = new TDataGridColumn('imagem', 'Imagem', 'left', 160);
  43. //$column_imagem->setTransformer( array($this, 'showImage') );
  44. // add the columns to the DataGrid
  45. $this->datagrid->addColumn($column_id);
  46. $this->datagrid->addColumn($column_nome);
  47. $this->datagrid->addColumn($column_imagem);
  48. // define the transformer method over image
  49. $column_imagem->setTransformer( function($value, $object, $row) {
  50. if (file_exists($value)) {
  51. return new TImage($value);
  52. }
  53. });
  54. // create EDIT action
  55. $action_edit = new TDataGridAction(array('Imagem_marcadorForm', 'onEdit'));
  56. $action_edit->setUseButton(TRUE);
  57. $action_edit->setButtonClass('btn btn-default');
  58. $action_edit->setLabel(_t('Edit'));
  59. $action_edit->setImage('fa:pencil-square-o blue fa-lg');
  60. $action_edit->setField('id');
  61. $this->datagrid->addAction($action_edit);
  62. // create DELETE action
  63. $action_del = new TDataGridAction(array($this, 'onDelete'));
  64. $action_del->setUseButton(TRUE);
  65. $action_del->setButtonClass('btn btn-default');
  66. $action_del->setLabel(_t('Delete'));
  67. $action_del->setImage('fa:trash-o red fa-lg');
  68. $action_del->setField('id');
  69. $this->datagrid->addAction($action_del);
  70. // create the datagrid model
  71. $this->datagrid->createModel();
  72. // create the page navigation
  73. $this->pageNavigation = new TPageNavigation;
  74. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  75. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  76. // vertical box container
  77. $container = new TVBox;
  78. $container->style = 'width: 90%';
  79. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  80. $container->add($this->form);
  81. $container->add($this->datagrid);
  82. $container->add($this->pageNavigation);
  83. parent::add($container);
  84. }
  85. }
  86. </code>

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


NR

Qual é o caminho do arquivo que está salvo no banco de dados?

Obs: cuidado com a "/". Chamando "/tmp" você não está acessando a pasta tmp da raiz do seu projeto e sim passando o caminho absoluto da pasta "/tmp" do sistema.
MC

Resolvido!
"Qual é o caminho do arquivo que está salvo no banco de dados? " -> o proprio frame no campo TFile salva automaticamente todos os arquivos na pasta tmp por "defautl", mesmo antes de salvar o registro, então após ver alguns exemplos no fórum, adaptei "meio gambiarra", e está funcionando.


 
  1. <?php
  2. /**
  3. * Imagem_marcadorForm Form
  4. * @author <your name here>
  5. */
  6. class Imagem_marcadorForm extends TPage
  7. {
  8. protected $form; // form
  9. /**
  10. * Form constructor
  11. * @param $param Request
  12. */
  13. public function __construct( $param )
  14. {
  15. parent::__construct();
  16. // creates the form
  17. $this->form = new TQuickForm('form_Imagem_marcador');
  18. $this->form->class = 'tform'; // change CSS class
  19. $this->form = new BootstrapFormWrapper($this->form);
  20. $this->form->style = 'display: table;width:100%'; // change style
  21. // define the form title
  22. $this->form->setFormTitle('Imagem_marcador');
  23. // create the form fields
  24. $id = new TEntry('id');
  25. $nome = new TEntry('nome');
  26. $imagem = new TFile('imagem');
  27. // add the fields
  28. $this->form->addQuickField('Id', $id, 100 );
  29. $this->form->addQuickField('Nome', $nome, 200 , new TRequiredValidator);
  30. $this->form->addQuickField('Imagem', $imagem, 200 , new TRequiredValidator);
  31. if (!empty($id))
  32. {
  33. $id->setEditable(FALSE);
  34. }
  35. /** samples
  36. $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  37. $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  38. $fieldX->setSize( 100, 40 ); // set size
  39. **/
  40. // create the form actions
  41. $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
  42. $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
  43. // vertical box container
  44. $container = new TVBox;
  45. $container->style = 'width: 90%';
  46. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  47. $container->add(TPanelGroup::pack('Title', $this->form));
  48. parent::add($container);
  49. }
  50. /**
  51. * Save form data
  52. * @param $param Request
  53. */
  54. public function onSave( $param )
  55. {
  56. try
  57. {
  58. TTransaction::open('bancopatrulharural'); // open a transaction
  59. /**
  60. // Enable Debug logger for SQL operations inside the transaction
  61. TTransaction::setLogger(new TLoggerSTD); // standard output
  62. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  63. **/
  64. $this->form->validate(); // validate form data
  65. $object = new Imagem_marcador; // create an empty object
  66. $data = $this->form->getData(); // get form data as array
  67. $object->fromArray( (array) $data); // load the object with data
  68. var_dump($object);
  69. // adaptado por fabiano dos santos no forúm https://www.adianti.com.br/forum/pt/view_599?exemplo-de-upload-e-download-de-arquivos
  70. if ($object-> imagem)
  71. {
  72. $target_folder = 'tmp/' . $object-> id;
  73. $target_file = $target_folder . '/' .$object-> imagem;
  74. @mkdir($target_folder);
  75. rename('tmp/'.$object-> imagem, $target_file); }
  76. $object->imagem = "tmp/".$object->id."/".$object->imagem; // aqui adicionei novo valor a variavel a fim de salvar corretamente o caminho no banco de dados
  77. //object->imagem = "tmp/".$object->imagem;
  78. $object->store(); // save the object
  79. // get the generated id
  80. $data->id = $object->id;
  81. $this->form->setData($data); // fill form data
  82. TTransaction::close(); // close the transaction
  83. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  84. }
  85. catch (Exception $e) // in case of exception
  86. {
  87. new TMessage('error', $e->getMessage()); // shows the exception error message
  88. $this->form->setData( $this->form->getData() ); // keep form data
  89. TTransaction::rollback(); // undo all pending operations
  90. }
  91. }
  92. /**
  93. * Clear form data
  94. * @param $param Request
  95. */
  96. public function onClear( $param )
  97. {
  98. $this->form->clear(TRUE);
  99. }
  100. /**
  101. * Load object to form data
  102. * @param $param Request
  103. */
  104. public function onEdit( $param )
  105. {
  106. try
  107. {
  108. if (isset($param['key']))
  109. {
  110. $key = $param['key']; // get the parameter $key
  111. TTransaction::open('bancopatrulharural'); // open a transaction
  112. $object = new Imagem_marcador($key); // instantiates the Active Record
  113. $this->form->setData($object); // fill the form
  114. TTransaction::close(); // close the transaction
  115. }
  116. else
  117. {
  118. $this->form->clear(TRUE);
  119. }
  120. }
  121. catch (Exception $e) // in case of exception
  122. {
  123. new TMessage('error', $e->getMessage()); // shows the exception error message
  124. TTransaction::rollback(); // undo all pending operations
  125. }
  126. }
  127. }
  128. </code>


Obrigado.</your>