EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: Marco on November 27, 2014, 03:22:04 PM



Title: Datagrid column sum
Post by: Marco on November 27, 2014, 03:22:04 PM
Hi !
j need some help
Is it possible to get the sum of a data grid column

j need to put the result in another field.

Thanks


Title: Re: Datagrid column sum
Post by: jarry on November 27, 2014, 06:34:39 PM
Please use the 'formatter' function to set the calculate field.
Code:
$('#dg').datagrid({
columns:[[
{field:'sum',title:'Sum',width:100,
formatter:function(value,row){
return row.v1+row.v2+row.v3;
}
}
]]
})


Title: Re: Datagrid column sum
Post by: Marco on November 28, 2014, 03:40:45 AM
Thanks for your replay

but j  wa searching for

something like this img.



Title: Re: Datagrid column sum
Post by: yamilbracho on November 28, 2014, 10:20:39 AM
It could be something like :
Code:
$('#dg').datagrid({
onLoadSuccess:function(data) {
  var sum = 0;
  for (i = 0; i < data.length; i++) {
     sum+=data[i].YourField;
  }
  $('#Imponible').text(sum);
}
})


Title: Re: Datagrid column sum
Post by: Marco on November 28, 2014, 11:49:00 AM
Hi !
Thanks for your interest

J try your code but it does not work maybe j do something wrong


here's my code


<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="somma()">Somma</a>

<div class="fitem" >
 <label>Imponibile:</label>
 <input id="Imponibile" name="Imponibile" class="easyui-textbox"  style="width:80px;height:20px" >



function somma ()
{
$('#tt_dt').datagrid('load')
$('#tt_dt').datagrid({
   onLoadSuccess:function(data) {
     var sum = 0;
     for (i = 0; i < data.length; i++) {
        sum+=data.Totale_riga;
     }
     $('#Imponible').text(sum);
   }
})

}
//-------------------------------------------------------------


Title: Re: Datagrid column sum
Post by: yamilbracho on November 29, 2014, 12:11:56 PM
Change function summa() to :
Code:
function somma () {
  var data = $('#tt_dt').datagrid('getData');
  var sum = 0;
 
  for (i = 0; i < data.length; i++) {
        sum+=data.Totale_riga;
  }

  // just to show if the sum is OK
 alert('sum=' + sum);
 $('#Imponible').textbox('setText', sum);
}


Title: Re: Datagrid column sum
Post by: Marco on November 29, 2014, 01:12:32 PM
It seems that the sum is not ok..


Title: Re: Datagrid column sum
Post by: yamilbracho on November 29, 2014, 02:13:15 PM
My mistake, try this :

Code:
function somma () {
  var data = $('#tt_dt').datagrid('getData');
  var rows = data.rows;
  var sum = 0;
 
  for (i=0; i < rows.length; i++) {
        sum+=rows[i].Totale_riga;
  }

  // just to show if the sum is OK
 alert('sum=' + sum);
 $('#Imponible').textbox('setText', sum);
}


Title: Re: Datagrid column sum
Post by: Marco on November 29, 2014, 02:30:02 PM
It does the text sum not .
it does not the true mathematical sum.


Title: Re: Datagrid column sum
Post by: yamilbracho on November 29, 2014, 07:03:31 PM
Well, you only need to convert a string to number because it returns the concatenation of the column values...
Change the line in the loop where you want to add the column values to :

Code:
sum+=parseFloat(rows[i].Totale_riga);



Title: Re: Datagrid column sum
Post by: Marco on November 30, 2014, 03:46:27 AM
Ok it works fine !!!

Thanks a lot