EasyUI Forum
September 13, 2025, 04:31:16 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Datagrid column sum  (Read 26311 times)
Marco
Newbie
*
Posts: 12


View Profile Email
« 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
Logged
jarry
Administrator
Hero Member
*****
Posts: 2298


View Profile Email
« Reply #1 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;
}
}
]]
})
Logged
Marco
Newbie
*
Posts: 12


View Profile Email
« Reply #2 on: November 28, 2014, 03:40:45 AM »

Thanks for your replay

but j  wa searching for

something like this img.

Logged
yamilbracho
Jr. Member
**
Posts: 64


View Profile Email
« Reply #3 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);
}
})
Logged
Marco
Newbie
*
Posts: 12


View Profile Email
« Reply #4 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);
   }
})

}
//-------------------------------------------------------------
Logged
yamilbracho
Jr. Member
**
Posts: 64


View Profile Email
« Reply #5 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);
}
Logged
Marco
Newbie
*
Posts: 12


View Profile Email
« Reply #6 on: November 29, 2014, 01:12:32 PM »

It seems that the sum is not ok..
Logged
yamilbracho
Jr. Member
**
Posts: 64


View Profile Email
« Reply #7 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);
}
Logged
Marco
Newbie
*
Posts: 12


View Profile Email
« Reply #8 on: November 29, 2014, 02:30:02 PM »

It does the text sum not .
it does not the true mathematical sum.
Logged
yamilbracho
Jr. Member
**
Posts: 64


View Profile Email
« Reply #9 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);

Logged
Marco
Newbie
*
Posts: 12


View Profile Email
« Reply #10 on: November 30, 2014, 03:46:27 AM »

Ok it works fine !!!

Thanks a lot
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!