datagrid->getRowIndex Boa noite galera! tenho uma datagrid onde implementei a função 'onShowDetail()', esse é o método que exibe detalhes das linhas da datagrid e usa a função $this->datagrid->getRowIndex() para recuperar a posição da linha na tabela... pois bem vamos ao problema, o método está funcionando corretamente somente quando ordeno os dados de forma crescente "asc", quando mudo para "desc" que é o...
BI
datagrid->getRowIndex  
Boa noite galera!
tenho uma datagrid onde implementei a função 'onShowDetail()', esse é o método que exibe detalhes das linhas da datagrid e usa a função $this->datagrid->getRowIndex() para recuperar a posição da linha na tabela...
pois bem vamos ao problema, o método está funcionando corretamente somente quando ordeno os dados de forma crescente "asc", quando mudo para "desc" que é o que me interessa e clico para exibir os detalhes, a função $this->datagrid->getRowIndex() retorna "null" e automaticamente a ordem dos dados é alterada para "asc". alguem já passou por isso? pode me ajudar? estou usando o template III.

 
  1. <?php
  2. class Ocorrencias extends TPage
  3. {
  4. private $form; // form
  5. private $datagrid; // listing
  6. private $pageNavigation;
  7. private $formgrid;
  8. private $loaded;
  9. private $deleteButton;
  10. /**
  11. * Class constructor
  12. * Creates the page, the form and the listing
  13. */
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. // creates the form
  18. $this->form = new TQuickForm('form_search_ViewOcorrenciasCliente');
  19. $this->form->class = 'tform'; // change CSS class
  20. $this->form = new BootstrapFormWrapper($this->form);
  21. $this->form->style = 'display: table;width:100%'; // change style
  22. $this->form->setFormTitle('ViewOcorrenciasCliente');
  23. // create the form fields
  24. $id = new TEntry('id');
  25. $data_coig = new TDate('data_coig');
  26. $empreendimento = new TCombo('empreendimento');
  27. $sigla_apl = new TCombo('sigla_apl');
  28. $evento = new TDBCombo('evento','sqlserver','EventoGenerico','evento','evento');
  29. $obs = new TEntry('obs');
  30. //carrega os empreendimentos do cliente
  31. TTransaction::open('sqlserver');
  32. $repository = new TRepository('ViewEmpreendimentocliente');
  33. $criteria = new TCriteria;
  34. //$log_id = TSession::getValue('login_id');
  35. $criteria->add(new TFilter('systemuser_id', '=', TSession::getValue('login_id')));
  36. $collection = $repository->load($criteria);
  37. $items = array();
  38. foreach ($collection as $object)
  39. {
  40. $items[$object->empreendimento_id] = $object->sigla;
  41. }
  42. $empreendimento->addItems($items);
  43. TTransaction::close();
  44. $id->setMask('999999999999');
  45. // set change action for empreendimento
  46. $change_action = new TAction(array($this, 'onChange'));
  47. $empreendimento->setChangeAction($change_action);
  48. // add the fields
  49. $this->form->addQuickField('Cód', $id, '30%' );
  50. $this->form->addQuickField('Data', $data_coig, '47%' );
  51. $this->form->addQuickField('Empreendimento', $empreendimento, '50%' );
  52. $this->form->addQuickField('Aplicação', $sigla_apl, '50%' );
  53. $this->form->addQuickField('Evento', $evento, '50%' );
  54. $this->form->addQuickField('Observação', $obs, '50%' );
  55. // keep the form filled during navigation with session data
  56. $this->form->setData( TSession::getValue('ViewOcorrenciasCliente_filter_data') );
  57. // add the search form actions
  58. $this->form->addQuickAction(_t('Find'), new TAction(array($this, 'onSearch')), 'fa:search');
  59. //$this->form->addQuickAction(_t('New'), new TAction(array('', 'onEdit')), 'bs:plus-sign green');
  60. // creates a Datagrid
  61. $this->datagrid = new TDataGrid;
  62. //$this->datagrid = new BootstrapDatagridWrapper($this->datagrid);
  63. $this->datagrid->style = 'width: 100%';
  64. $this->datagrid->setHeight(320);
  65. //$this->datagrid->datatable = 'true';
  66. //$this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
  67. // creates the datagrid columns
  68. $column_id = new TDataGridColumn('id', 'Cód', 'center');
  69. $column_data_coig = new TDataGridColumn('data_coig', 'Data', 'center');
  70. $column_hora = new TDataGridColumn('hora', 'Hora', 'center');
  71. $column_empreendimento = new TDataGridColumn('empreendimento', 'Empreendimento', 'left');
  72. $column_sigla_apl = new TDataGridColumn('sigla_apl', 'Aplicação', 'center');
  73. $column_evento = new TDataGridColumn('evento', 'Evento / Trip', 'left');
  74. $column_obs = new TDataGridColumn('obs', 'Observação', 'left');
  75. $column_imagem = new TDataGridColumn('imagem', 'Imagem', 'left');
  76. // add the columns to the DataGrid
  77. $this->datagrid->addColumn($column_id);
  78. $this->datagrid->addColumn($column_data_coig);
  79. $this->datagrid->addColumn($column_hora);
  80. $this->datagrid->addColumn($column_empreendimento);
  81. $this->datagrid->addColumn($column_sigla_apl);
  82. $this->datagrid->addColumn($column_evento);
  83. $this->datagrid->addColumn($column_obs);
  84. $this->datagrid->addColumn($column_imagem);
  85. $column_id->setTransformer(array($this, 'formatRelevancia'));
  86. // creates the datagrid column actions
  87. $order_id = new TAction(array($this, 'onReload'));
  88. $order_id->setParameter('order', 'id');
  89. $column_id->setAction($order_id);
  90. $order_data_coig = new TAction(array($this, 'onReload'));
  91. $order_data_coig->setParameter('order', 'data_coig');
  92. $column_data_coig->setAction($order_data_coig);
  93. $order_hora = new TAction(array($this, 'onReload'));
  94. $order_hora->setParameter('order', 'hora');
  95. $column_hora->setAction($order_hora);
  96. // creates two datagrid actions
  97. $action = new TDataGridAction(array($this, 'onShowDetail'));
  98. $action->setLabel('Detalhes');
  99. $action->setImage('ico_view.png');
  100. $action->setField('id');
  101. // add the actions to the datagrid
  102. $this->datagrid->addAction($action);
  103. // define the transformer method over image
  104. $column_data_coig->setTransformer( function($value, $object, $row) {
  105. $date = new DateTime($value);
  106. return $date->format('d/m/Y');
  107. });
  108. // create EDIT action
  109. //$action_edit = new TDataGridAction(array('', 'onEdit'));
  110. //$action_edit->setUseButton(TRUE);
  111. //$action_edit->setButtonClass('btn btn-default');
  112. //$action_edit->setLabel(_t('Edit'));
  113. //$action_edit->setImage('fa:pencil-square-o blue fa-lg');
  114. //$action_edit->setField('id');
  115. //$this->datagrid->addAction($action_edit);
  116. // create the datagrid model
  117. $this->datagrid->createModel();
  118. // creates the page navigation
  119. $this->pageNavigation = new TPageNavigation;
  120. $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
  121. $this->pageNavigation->setWidth($this->datagrid->getWidth());
  122. // vertical box container
  123. $container = new TVBox;
  124. $container->style = 'width: 90%';
  125. $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  126. $container->add(TPanelGroup::pack('', $this->form));
  127. $container->add($this->datagrid);
  128. $container->add($this->pageNavigation);
  129. parent::add($container);
  130. }
  131. //transformar a linha "colorir as mais relevantes"
  132. public function formatRelevancia($id, $object, $row)
  133. {
  134. if (PHP_SAPI !== 'cli') // just in the web version
  135. {
  136. if ($object->relevancia_id == 2)
  137. {
  138. $row->style = "color: blue";//relevancia media
  139. //return $relevancia_id;
  140. }
  141. elseif($object->relevancia_id == 3)
  142. {
  143. $row->style = "color: red";//relevancia alta
  144. //return $relevancia_id;
  145. }
  146. else
  147. {
  148. $row->style = "color: black";//relevancia normal 'background'
  149. }
  150. return $id;
  151. }
  152. }
  153. /**
  154. DETALHES
  155. */
  156. public function onShowDetail( $param )
  157. {
  158. // get row position
  159. $pos = $this->datagrid->getRowIndex('id', $param['key']);
  160. var_dump($pos);
  161. //var_dump($param);
  162. if ($pos >= 0){
  163. TTransaction::open('sqlserver');
  164. $objeto = new ViewOcorrenciasGerais($param['key']);
  165. $user_post = new SystemUser($objeto->systemuser_id);
  166. $user_edit = new SystemUser($objeto->user_edicao);
  167. // get row by position
  168. $current_row = $this->datagrid->getRow($pos);
  169. $current_row->style = "background-color: #43CD80; color:white; text-shadow:none";
  170. // create a new row
  171. if ($objeto->validador == 1){
  172. $row3 = new TTableRow;
  173. $row3->style = "background-color: #E0DEF8; color: black";
  174. $cell = $row3->addCell("<b><p style='color: red'>Ocorrência Validada!</p></b> ");
  175. $cell->colspan = 9;
  176. $this->datagrid->insert($pos +1, $row3);
  177. }
  178. // create a new row
  179. if ($objeto->user_edicao){
  180. $row2 = new TTableRow;
  181. $row2->style = "background-color: #E0DEF8; color: black";
  182. $cell = $row2->addCell("<b>Atualizado por:</b> ". ucfirst($objeto->editor).", <b>na data:</b> ".date('d/m/y H:i:s', strtotime($objeto->data_edicao)));
  183. $cell->colspan = 9;
  184. $this->datagrid->insert($pos +1, $row2);
  185. }
  186. $row1 = new TTableRow;
  187. $row1->style = "background-color: #E0DEF8; color: black";
  188. $cell = $row1->addCell("<b>Postado por:</b> ". ucfirst($objeto->user_post). ", <b>na data:</b> ".date('d/m/y H:i:s', strtotime($objeto->agora)));
  189. $cell->colspan = 9;
  190. $this->datagrid->insert($pos +1, $row1);
  191. if ($objeto->nivel_montante and $objeto->potencia){
  192. $row = new TTableRow;
  193. $row->style = "background-color: #E0DEF8; color: black";
  194. $cell = $row->addCell("<b>Nível Montante:</b> $objeto->nivel_montante m. <b>- Potência ativa:</b> $objeto->potencia kW.");
  195. $cell->colspan = 9;
  196. $this->datagrid->insert($pos +1, $row);
  197. } elseif ($objeto->nivel_montante and empty ($objeto->potencia)) {
  198. $row = new TTableRow;
  199. $row->style = "background-color: #E0DEF8; color: black";
  200. $cell = $row->addCell("<b>Nível Montante:</b> $objeto->nivel_montante m.");
  201. $cell->colspan = 9;
  202. $this->datagrid->insert($pos +1, $row);
  203. } elseif (empty($objeto->nivel_montante) and $objeto->potencia) {
  204. $row = new TTableRow;
  205. $row->style = "background-color: #E0DEF8; color: black";
  206. $cell = $row->addCell("<b>Potência ativa:</b> $objeto->potencia kW.");
  207. $cell->colspan = 9;
  208. $this->datagrid->insert($pos +1, $row);
  209. }
  210. if (!empty($objeto->evento_real_id)) {
  211. $real = new EventoReal($objeto->evento_real_id);
  212. $row = new TTableRow;
  213. $row->style = "background-color: #E0DEF8; color: black";
  214. $cell = $row->addCell("<b>Evendo Real: </b>$real->evento ");
  215. $cell->colspan = 9;
  216. $this->datagrid->insert($pos +1, $row);
  217. }
  218. TTransaction::close();
  219. }
  220. }
  221. /**
  222. * Register the filter in the session
  223. */
  224. public function onSearch()
  225. {
  226. // get the search form data
  227. $data = $this->form->getData();
  228. // clear session filters
  229. TSession::setValue('Ocorrencias_filter_id', NULL);
  230. TSession::setValue('Ocorrencias_filter_data_coig', NULL);
  231. TSession::setValue('Ocorrencias_filter_empreendimento', NULL);
  232. TSession::setValue('Ocorrencias_filter_sigla_apl', NULL);
  233. TSession::setValue('Ocorrencias_filter_evento', NULL);
  234. TSession::setValue('Ocorrencias_filter_obs', NULL);
  235. if (isset($data->id) AND ($data->id)) {
  236. $filter = new TFilter('id', '=', "$data->id"); // create the filter
  237. TSession::setValue('Ocorrencias_filter_id', $filter); // stores the filter in the session
  238. }
  239. if (isset($data->data_coig) AND ($data->data_coig)) {
  240. $filter = new TFilter('data_coig', '=', "$data->data_coig"); // create the filter
  241. TSession::setValue('Ocorrencias_filter_data_coig', $filter); // stores the filter in the session
  242. }
  243. if (isset($data->empreendimento) AND ($data->empreendimento)) {
  244. $filter = new TFilter('empreendimento_id', '=', $data->empreendimento); // create the filter
  245. TSession::setValue('Ocorrencias_filter_empreendimento', $filter); // stores the filter in the session
  246. }
  247. if (isset($data->sigla_apl) AND ($data->sigla_apl)) {
  248. $filter = new TFilter('aplicacao_id', '=', $data->sigla_apl); // create the filter
  249. TSession::setValue('Ocorrencias_filter_sigla_apl', $filter); // stores the filter in the session
  250. }
  251. if (isset($data->evento) AND ($data->evento)) {
  252. $evento = trim($data->evento);
  253. $filter = new TFilter('evento', 'like', "%{$evento}%" ); // create the filter
  254. TSession::setValue('Ocorrencias_filter_evento', $filter); // stores the filter in the session
  255. }
  256. if (isset($data->obs) AND ($data->obs)) {
  257. $filter = new TFilter('obs', 'like', "%{$data->obs}%"); // create the filter
  258. TSession::setValue('Ocorrencias_filter_obs', $filter); // stores the filter in the session
  259. }
  260. // fill the form with data again
  261. $this->form->setData($data);
  262. TForm::sendData('form_search_ViewOcorrenciasCliente', $data);
  263. // keep the search data in the session
  264. TSession::setValue('ViewOcorrenciasCliente_filter_data', $data);
  265. $param=array();
  266. $param['offset'] =0;
  267. $param['first_page']=1;
  268. $this->onReload($param);
  269. }
  270. /**
  271. * Load the datagrid with data
  272. */
  273. public function onReload($param = NULL)
  274. {
  275. try
  276. {
  277. // open a transaction with database 'sqlserver'
  278. TTransaction::open('sqlserver');
  279. // creates a repository for ViewOcorrenciasCliente
  280. $repository = new TRepository('ViewOcorrenciasCliente');
  281. $limit = 30;
  282. // creates a criteria
  283. $criteria = new TCriteria;
  284. // default order
  285. if (empty($param['order']))
  286. {
  287. $param['order'] = 'id';
  288. $param['direction'] = 'desc';
  289. }
  290. $criteria->setProperties($param); // order, offset
  291. $criteria->setProperty('limit', $limit);
  292. if (TSession::getValue('Ocorrencias_filter_id')) {
  293. $criteria->add(TSession::getValue('Ocorrencias_filter_id')); // add the session filter
  294. }
  295. if (TSession::getValue('Ocorrencias_filter_data_coig')) {
  296. $criteria->add(TSession::getValue('Ocorrencias_filter_data_coig')); // add the session filter
  297. }
  298. if (TSession::getValue('Ocorrencias_filter_empreendimento')) {
  299. $criteria->add(TSession::getValue('Ocorrencias_filter_empreendimento')); // add the session filter
  300. }
  301. if (TSession::getValue('Ocorrencias_filter_sigla_apl')) {
  302. $criteria->add(TSession::getValue('Ocorrencias_filter_sigla_apl')); // add the session filter
  303. }
  304. if (TSession::getValue('Ocorrencias_filter_evento')) {
  305. $criteria->add(TSession::getValue('Ocorrencias_filter_evento')); // add the session filter
  306. }
  307. if (TSession::getValue('Ocorrencias_filter_obs')) {
  308. $criteria->add(TSession::getValue('Ocorrencias_filter_obs')); // add the session filter
  309. }
  310. $log_id = TSession::getValue('login_id');
  311. $criteria->add( new TFilter( 'id_cliente', '=', $log_id ));
  312. // load the objects according to criteria
  313. $objects = $repository->load($criteria, FALSE);
  314. if (is_callable($this->transformCallback))
  315. {
  316. call_user_func($this->transformCallback, $objects, $param);
  317. }
  318. $this->datagrid->clear();
  319. $this->datagrid->disableDefaultClick();
  320. if ($objects)
  321. {
  322. // iterate the collection of active records
  323. foreach ($objects as $object)
  324. {
  325. //$object->data_coig = date("d/m/y", strtotime($object->data_coig));
  326. $object->hora = date("H:i:s", strtotime($object->hora));
  327. if ($object->imagem){
  328. //coloca o botão com a classe modal para abrir uma imagem
  329. $teste = "";
  330. $teste = $object->imagem;
  331. $object->imagem = "<div class='container' style='width: 20px'>
  332. <div class='row text-center' >
  333. <a href='#' class='btn btn-lg btn-success' data-toggle='modal' data-target='#ocorrencia-$object->id' style='background: #ccc; border: green 1px solid; color: black; font-size: 12px;'><img style='width: 35px; height: 30px;' src='$teste'></a>
  334. </div>
  335. </div>
  336. <div class='modal fade' id='ocorrencia-$object->id' tabindex='-1' role='dialog' aria-labelledby='basicModal' aria-hidden='true'>
  337. <div class='modal-dialog' style='width: 1000px; height: 800px;'>
  338. <div class='modal-content'>
  339. <div class='modal-header'>
  340. <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
  341. </div>
  342. <div class='modal-body' >
  343. <img src='$teste'>
  344. </div>
  345. </div>
  346. </div>
  347. </div>";
  348. }
  349. // add the object inside the datagrid
  350. $this->datagrid->addItem($object);
  351. }
  352. }
  353. // reset the criteria for record count
  354. $criteria->resetProperties();
  355. $count= $repository->count($criteria);
  356. $this->pageNavigation->setCount($count); // count of records
  357. $this->pageNavigation->setProperties($param); // order, page
  358. $this->pageNavigation->setLimit($limit); // limit
  359. // close the transaction
  360. TTransaction::close();
  361. $this->loaded = true;
  362. }
  363. catch (Exception $e) // in case of exception
  364. {
  365. // shows the exception error message
  366. new TMessage('error', $e->getMessage());
  367. // undo all pending operations
  368. TTransaction::rollback();
  369. }
  370. }
  371. /**
  372. * Ask before deletion
  373. */
  374. public function onDelete($param)
  375. {
  376. // define the delete action
  377. $action = new TAction(array($this, 'Delete'));
  378. $action->setParameters($param); // pass the key parameter ahead
  379. // shows a dialog to the user
  380. new TQuestion(AdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  381. }
  382. /**
  383. * Delete a record
  384. */
  385. public function Delete($param)
  386. {
  387. try
  388. {
  389. $key=$param['key']; // get the parameter $key
  390. TTransaction::open('sqlserver'); // open a transaction with database
  391. $object = new ViewOcorrenciasCliente($key, FALSE); // instantiates the Active Record
  392. $object->delete(); // deletes the object from the database
  393. TTransaction::close(); // close the transaction
  394. $this->onReload( $param ); // reload the listing
  395. new TMessage('info', AdiantiCoreTranslator::translate('Record deleted')); // success message
  396. }
  397. catch (Exception $e) // in case of exception
  398. {
  399. new TMessage('error', $e->getMessage()); // shows the exception error message
  400. TTransaction::rollback(); // undo all pending operations
  401. }
  402. }
  403. public static function onChange($param)
  404. {
  405. if (!empty($param['empreendimento'])){
  406. TTransaction::open('sqlserver');
  407. $criteria = new TCriteria;
  408. $criteria->add(new TFilter('empreendimento_id', '=', $param['empreendimento']));
  409. //$criteria->add(new TFilter('aplicacao', 'like', '%gerador%'));
  410. $repository = new TRepository('ViewEmpreendimentos');
  411. $aplicacaos = $repository->load($criteria);
  412. $options = array();
  413. $options[] = '' ;
  414. foreach ($aplicacaos as $aplicacao){
  415. $options[$aplicacao->id] = $aplicacao->sigla;
  416. }
  417. //TForm::sendData('form_Vertedouro', $objeto);
  418. TCombo::reload('form_search_ViewOcorrenciasCliente', 'sigla_apl', $options);
  419. }
  420. }
  421. /**
  422. * method show()
  423. * Shows the page
  424. */
  425. public function show()
  426. {
  427. // check if the datagrid is already loaded
  428. if (!$this->loaded AND (!isset($_GET['method']) OR !(in_array($_GET['method'], array('onReload', 'onSearch')))) )
  429. {
  430. if (func_num_args() > 0)
  431. {
  432. $this->onReload( func_get_arg(0) );
  433. }
  434. else
  435. {
  436. $this->onReload();
  437. }
  438. }
  439. parent::show();
  440. }
  441. }
  442. ?>

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


AE

Buenas.
Conseguiste resolver?
Estou na mesma situação com a versão 5.7 do framework.