none
Sum column value with javascript RRS feed

  • Question

  • Hi guys ,

    i want to sum a column with javascript after a query.. This is my query:

     var queryTotalTime = "<View><Query><Where><IsNotNull><FieldRef Name='Estimated_x0020_Time_x0020__x002'/></IsNotNull></Where></Query></View>";
    
     var querySpentTime = "<View><Query><Where><IsNotNull><FieldRef Name='Real_x0020_spent_x0020_time' /></IsNotNull></Where></Query></View>";
    
    		    var siteUrl = window.location.protocol + "//" + window.location.host;
    		    var clientContext = SP.ClientContext.get_current();
    		    var oList = clientContext.get_web().get_lists().getByTitle('Task');
    
    		    var MyTotalQuery = new SP.CamlQuery();
    		    MyTotalQuery.set_viewXml(queryTotalTime);
    		    myTotalTimeItems = oList.getItems(MyTotalQuery);
    
    

    I have to iterate myTotalTimesItems?how?

    Thank you!

    Monday, October 13, 2014 8:33 AM

Answers

  • Updated code for onItems JS function. If any item won't have any value for the field, it will be skipped.

    function onItems(sender, e){
      var iterator=myTotalTimeItems.getEnumerator();
      var sum=0;
      while(iterator.moveNext()){
             var item=iterator.get_current();//retrieves current item from the collection.
    		 var num=item.get_item("Estimated_x0020_Time_x0020__x002");
    		 if(isNaN(num)) continue;
    		 else
    		    sum+=Number(num);
      }
      alert("Total estimated time:"+sum);
    }


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    • Marked as answer by gohberto Monday, October 13, 2014 1:41 PM
    Monday, October 13, 2014 1:14 PM

All replies

  • Hi gohberto,

    You must use a for

    var total
    var i;
    var a = ["a", "b", "c"];
    for (i = 0; i < myTotalTimeItems.length; i++) {
        //action to do;
        total = total + myTotalTimeItems[i].proptoSUM
    }
    
    //proptoSUM is the property you want add


    If a post answers your question, please click Mark As Answer on that post and Vote as Helpful

    Monday, October 13, 2014 8:51 AM
  • Try this code.... To iterate through the client object collection, you have invoke the getEnumerator method on collection and process each item in collection. Also due to async nature of CSOM, the processing needs a separate function where loaded items can be processed.

    var myTotalTimeItems;
    function onFail(sender, e){
       alert(e.message);
    }
    function onItems(sender, e){
      var iterator=myTotalTimeItems.getEnumerator();
      while(iterator.moveNext()){
             var item=iterator.get_current();//retrieves current item from the collection.
    		 /*...process the item...*/
      }
    }
    function initialize(){
      var queryTotalTime = "<View><Query><Where><IsNotNull><FieldRef Name='Estimated_x0020_Time_x0020__x002'/></IsNotNull></Where></Query></View>";
    
     var querySpentTime = "<View><Query><Where><IsNotNull><FieldRef Name='Real_x0020_spent_x0020_time' /></IsNotNull></Where></Query></View>";
    
    		    var siteUrl = window.location.protocol + "//" + window.location.host;
    		    var clientContext = SP.ClientContext.get_current();
    		    var oList = clientContext.get_web().get_lists().getByTitle('Task');
    
    		    var MyTotalQuery = new SP.CamlQuery();
    		    MyTotalQuery.set_viewXml(queryTotalTime);
    		    myTotalTimeItems = oList.getItems(MyTotalQuery);
                clientContext.load(myTottalTimeItems);
    			clientContext.executeQueryAsync(Function.createDelegate(this, onItems), Function.createDelegate(this, onFail));
    }


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile


    • Edited by Pradip T Monday, October 13, 2014 10:05 AM
    Monday, October 13, 2014 10:04 AM
  • Hi Pradip,

    i need to get the value of the sum of the items collection in a variable what is the best way?

    Thank you! 

    Monday, October 13, 2014 12:12 PM
  • Updated code for onItems JS function. If any item won't have any value for the field, it will be skipped.

    function onItems(sender, e){
      var iterator=myTotalTimeItems.getEnumerator();
      var sum=0;
      while(iterator.moveNext()){
             var item=iterator.get_current();//retrieves current item from the collection.
    		 var num=item.get_item("Estimated_x0020_Time_x0020__x002");
    		 if(isNaN(num)) continue;
    		 else
    		    sum+=Number(num);
      }
      alert("Total estimated time:"+sum);
    }


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    • Marked as answer by gohberto Monday, October 13, 2014 1:41 PM
    Monday, October 13, 2014 1:14 PM
  • Thank you!
    Monday, October 13, 2014 1:41 PM