Datagrid editavel + calculo Como faço para calcular dinamicamente na coluna novo Saldo o valor de Saldo + Acerto ? Segue imagem em anexo...
ML
Datagrid editavel + calculo  
Como faço para calcular dinamicamente na coluna novo Saldo o valor de Saldo + Acerto ?

Segue imagem em anexo

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


MG

Matheus

Na versão 4, foi um introduzido uma forma de exibir totalizadores no data grid.

O exemplo abaixo está no tutor, use ele como base:

<php
 
  1. <?php
  2. /**
  3. * DatagridMathView
  4. *
  5. * @version 1.0
  6. * @package samples
  7. * @subpackage tutor
  8. * @author Pablo Dall'Oglio
  9. * @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class DatagridMathView extends TPage
  13. {
  14. private $datagrid;
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. // creates one datagrid
  19. $this->datagrid = new TDataGrid;
  20. $this->datagrid->style = 'width: 100%';
  21. // create the datagrid columns
  22. $code = new TDataGridColumn('code', 'Code', 'right');
  23. $description = new TDataGridColumn('description', 'Description', 'left');
  24. $amount = new TDataGridColumn('amount', 'Amount', 'center');
  25. $price = new TDataGridColumn('price', 'Price', 'right');
  26. $discount = new TDataGridColumn('discount', 'Discount', 'right');
  27. $subtotal = new TDataGridColumn('= {amount} * ( {price} - {discount} )', 'Subtotal', 'right');
  28. // add the columns to the datagrid
  29. $this->datagrid->addColumn($code);
  30. $this->datagrid->addColumn($description);
  31. $this->datagrid->addColumn($amount);
  32. $this->datagrid->addColumn($price);
  33. $this->datagrid->addColumn($discount);
  34. $this->datagrid->addColumn($subtotal);
  35. // define format function
  36. $format_value = function($value) {
  37. if (is_numeric($value)) {
  38. return 'R$ '.number_format($value, 2, ',', '.');
  39. }
  40. return $value;
  41. };
  42. $price->setTransformer( $format_value );
  43. $discount->setTransformer( $format_value );
  44. $subtotal->setTransformer( $format_value );
  45. // define totals
  46. $subtotal->setTotalFunction( function($values) {
  47. return array_sum((array) $values);
  48. });
  49. // creates the datagrid model
  50. $this->datagrid->createModel();
  51. // wrap the page content using vertical box
  52. $vbox = new TVBox;
  53. $vbox->style = 'width: 100%';
  54. $vbox->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  55. $vbox->add($this->datagrid);
  56. parent::add($vbox);
  57. }
  58. /**
  59. * Load the data into the datagrid
  60. */
  61. function onReload()
  62. {
  63. $this->datagrid->clear();
  64. // add an regular object to the datagrid
  65. $item = new StdClass;
  66. $item->code = '1';
  67. $item->description = 'Chocolate';
  68. $item->amount = 10;
  69. $item->price = 1;
  70. $item->discount = 0.05;
  71. $this->datagrid->addItem($item);
  72. // add an regular object to the datagrid
  73. $item = new StdClass;
  74. $item->code = '2';
  75. $item->description = 'Milk';
  76. $item->amount = 20;
  77. $item->price = 2;
  78. $item->discount = 0.5;
  79. $this->datagrid->addItem($item);
  80. // add an regular object to the datagrid
  81. $item = new StdClass;
  82. $item->code = '3';
  83. $item->description = 'Beer';
  84. $item->amount = 10;
  85. $item->price = 4;
  86. $item->discount = 1;
  87. $this->datagrid->addItem($item);
  88. // add an regular object to the datagrid
  89. $item = new StdClass;
  90. $item->code = '4';
  91. $item->description = 'Cofee';
  92. $item->amount = 20;
  93. $item->price = 2.5;
  94. $item->discount = 0.5;
  95. $this->datagrid->addItem($item);
  96. }
  97. /**
  98. * shows the page
  99. */
  100. function show()
  101. {
  102. $this->onReload();
  103. parent::show();
  104. }
  105. }
  106. ?>
ML

Não conseguir utilizar esse modelo porque o calculo envolve um campo do tipo TEntry.
MG

Veja, este é um exemplo.

Você pode criar um método que retorne o valor que deseja e atribua no $total->setTotalFunction().


MG

Outra abordagem, você criar 2 TEntry readonly, cria os métodos que retorne a soma dos valores das colunas que deseja e a atualiza a cada inclusão, alteração ou exclusão de um registro da data grid.
ML

usando o TotalFunction ficará um somatorio geral não ?

eu preciso de um somatorio para cada linha da datagrid, talvez usando javascript seja o caminho usando exitAction do campo TEntry, acha que pode ser feito assim Marcelo?

Obrigado pela ajuda.
MG

Pela imagem que postou, vc tem um data grid e no final de 2 colunas, um total.
Como disse, uma abordagem seria um método mesmo, pegando sua grid lendo ela e fazendo a soma.
Esta soma vc atribuiria aos campos que seriam os totais.
Não precisa necessariamente usar javascript, métodos poderiam ajudá-lo nisso. Javascript também vai funcionar.

FC

Posso dar uma sugestão ?

Porque não usa o editinline do proprio data grid sem colocar o TEntry assim dentro do onInlineEdit vc faz o calculo e depois chama o onReload pronto sua rotina está feita.