0

Usually the sum value for a list occurs at the top as in the image below. enter image description here

Is there any way we can display it in some textbox or label?

4 Answers 4

2
  1. Using jQuery you can calculate the sum and display in required label. You can edit the page and add script using script editor webpart.
  2. using JSOM or SPServices also to get sum value.
2

Note:

in June 2017, Microsoft disabled the use of JavaScript in a Calculated Column

That means given answers may not apply for newer SharePoint versions

For long explanation and work arounds see:
June 13th 2017 Microsoft blocked handling HTML markup in SharePoint calculated fields - how to get the same functionality back


**Original answer:**

In 2013 its best to apply CSR with a JSLink, but this method has some drawbacks like having to set the JSLink for every View, and not being able to (easily) have multiple Views on one page.

In SP2010 I used these steps to Sum a Calculated Column (which can not be done with a View Total)

It uses Javascript in a Calculated Column to do the math while the page is loading.

The complete Formula is:
(remember to set the datatype to Number)

="<div style=""color:"
 & IF(    [Rate]<[Buy]   ,"red","green")
 & """>"
 & DOLLAR(   [Qty]*[Rate]   )
 & "</div>"
 & "<div class=""vmSums"" style=""display:none;font-weight:bold;border-top:1px solid black;""></div>"
 & "<img src=""/_layouts/images/blank.gif"" onload=""{"
      &"var TBODY=this;while(TBODY.tagName!='TBODY'){TBODY=TBODY.parentNode}" 
     & ",dataSum='data-vmSum',dataCnt='data-vmCount',total=value="
 &    [Qty]*[Rate]   
     & ",count=0;"
     & "if(TBODY.getAttribute(dataSum)){"
         & "total=parseFloat(TBODY.getAttribute(dataSum))+value;"
         & "count=~~TBODY.getAttribute(dataCnt)+1}"
     & "TBODY.setAttribute(dataSum,Number(Math.round(total+'e2')+'e-2').toFixed(2));"
     & "TBODY.setAttribute(dataCnt,count);"
     & "window.clearTimeout(window.vmSumFunc);"
     & "window.vmSumFunc=window.setTimeout((function(){"
         & "var lastrow=~~TBODY.getAttribute(dataCnt),"
         & "el=document.getElementsByClassName('vmSums').item(lastrow);"
         & "el.textContent='$'+parseFloat(TBODY.getAttribute(dataSum)).toFixed(2);"
         & "el.style.display='block';"
     & "}), 100)"
 & "}"">"

Some things to note:

  • Calculated Columns set to datatype:Number can output HTML (since SP2010)
  • Using the SCRIPT tag was removed by Microsoft in summer 2013
  • but loading a blank IMG can still trigger Javascript in the onload function
  • This is all executed while the page is loading (so SP.js is not available and everything down the HTML page is not available yet)
  • Javascript Timeout (canceled by every next IMG onload) trick is used to execute the final Display of the Total once. Reference another DOM element with 'el' and you can display the total anywhere you want (note: this Javascript is loading Inline.. so your DOM element must exist)

###CalcMaster Bookmarklet to edit Formulas It is a PITA to debug Calculated Columns. Because you don't get feedback until you save a Formula and you end up having to click multiple times to get back to your Formula.

I have written a small 'CalcMaster' bookmarklet which hooks into the formula-editor and does a save of the Formula on every keypress; giving immediate feedback.
Recently published a first version on GitHub:

https://github.com/Danny-Engelman/CalcMaster

ICC

1

You can use JSLink in View form and create textbox dynamically and display sum value in that textbox using JQuery or you can dynamically calculate value using JQuery and display in textbox created by Jquery and place it anywhere you want.

1

You can use the below small code..

<script src="https://code.jquery.com/jquery-3.2.1.min.js"> </script>

<script type="text/javascript">

$(document).ready(function(){

var sumValue=0;
var listUrl = _spPageContextInfo.webServerRelativeUrl;

     $.ajax({  
        url: listUrl+"/_api/web/lists/getbytitle('TeamAutoPopulate')/items?$Select=Total", //  site url goes here  
       type: "GET",  
        headers: {  
            "Accept": "application/json;odata=verbose"  
       },  
       success: function(data, textStatus, xhr) {  
           $.each(data.d.results, function(index, item) {  
             // alert("The items in list are : " + item.Total);
             sumValue=sumValue + parseFloat(item.Total);    

           })  
           // alert("The Sum value is : " +sumValue);
            $("#aggrWPQ2 .ms-vb2").next().next().next().next().next().next().next().next().text("Sum= "+sumValue).css({ 'font-weight': 'bold' });
      },  
        error: function r(xhr, textStatus, errorThrown) {  
            alert("error:" + JSON.stringify(xhr));  
       }  
    }); 



});

</script>

enter image description here

1
  • I have used this in Sharepoint online 2013 list
    – Rajkumar
    May 12, 2017 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.