?
Solved

jquery increment

Posted on 2014-02-24
2
Medium Priority
?
448 Views
Last Modified: 2014-05-05
Hi All,

I have two lists called "Analyst" and "Log".
I am trying to find out how many times each analyst was assigned an assignment and represent the data in a google chart table like so "https://developers.google.com/chart/interactive/docs/gallery/table".
My actual production list contains 2000+ items.

I want to use jquery to accomplish the counting but I'm sort of stuck.

Here's what I have so far:

var anArray = new Array();
var anIndex=[];


//push names of analyst into an array to use later
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Analyst",
	CAMLViewFields: "<ViewFields><FieldRef Name='Title'></FieldRef></ViewFields>",
	CAMLQuery: "<Query><OrderBy><FieldRef Name='Title' /></OrderBy></Query>",
    completefunc: function (xData, Status) {
	//alert(xData.responseXML.xml);
      $(xData.responseXML).SPFilterNode("z:row").each(function() { 
			anArray.push($(this).attr("ows_Title"));			
      });
     }
	 //initialize "anIndex" array to zero
	 
  });

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Log",
	CAMLViewFields: "<ViewFields><FieldRef Name='Days'></FieldRef><FieldRef Name='Analyst'></FieldRef></ViewFields>",
	CAMLQuery: "<Query><OrderBy><FieldRef Name='Days' /></OrderBy></Query>",
    completefunc: function (xData, Status) {
	//alert(xData.responseXML.xml);
      $(xData.responseXML).SPFilterNode("z:row").each(function() { 		
		
//As you loop through the array, if 'Analyst' name equals what's in the array, then increment
		for (var x=0; x<anArray.length; x++)
		{
			if ($(this).attr("ows_Analyst") == anArray[x])
			{
				anArray[x] += 1;
			}
		}		
		
      });
     }
  });

Open in new window


Does what I have so far look right?  For some reason, it seems like it won't work because, anArray[x] is never initialized to zero.

Any ideas/suggestions/comments would be great.

My analyst list:
http://isaac.issharepoint.com/Lists/Analyst/AllItems.aspx

My log list:
http://isaac.issharepoint.com/Lists/Log/AllItems.aspx

Thanks!
0
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 2000 total points
ID: 39884803
Hi,
sorry but this does not really look OK as you use a simple one dimensional array with just one value.
And in the later processing you try to replace the content (which is initially the analyst name) by trying to increment it.

You can either use an array of Arrays or an array of objects (which I prefer):

var anArray = new Array();

//push names of analyst into an array to use later
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Analyst",
	CAMLViewFields: "<ViewFields><FieldRef Name='Title'></FieldRef></ViewFields>",
	CAMLQuery: "<Query><OrderBy><FieldRef Name='Title' /></OrderBy></Query>",
    completefunc: function (xData, Status) {
	//alert(xData.responseXML.xml);
      $(xData.responseXML).SPFilterNode("z:row").each(function() { 
			anArray.push({
				Analyst: 	$(this).attr("ows_Title"),
				Count:		0
				});			
      });
     }
  });

$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Log",
	CAMLViewFields: "<ViewFields><FieldRef Name='Days'></FieldRef><FieldRef Name='Analyst'></FieldRef></ViewFields>",
	CAMLQuery: "<Query><OrderBy><FieldRef Name='Days' /></OrderBy></Query>",
    completefunc: function (xData, Status) {
	//alert(xData.responseXML.xml);
      $(xData.responseXML).SPFilterNode("z:row").each(function() { 		
		
//As you loop through the array, if 'Analyst' name equals what's in the array, then increment
		for (var x=0; x<anArray.length; x++)
		{
			if ($(this).attr("ows_Analyst") == anArray[x].Analyst)
			{
				anArray[x].Count += 1;
			}
		}		
      });
     }
  });

Open in new window


Sorry I have currently no dev environment at Hand, therefore the above code has to be tested (its written "blind").

HTH
Rainer
0
 
LVL 5

Author Comment

by:Isaac
ID: 39887976
Here's what I have but does not work.

http://isaac.issharepoint.com/Shared%20Documents/pieChart.aspx

Here's the code:
<!-- Step 1 - Load libraries -->
<script type="text/javascript" src="http://isaac.issharepoint.com/Script/jquery-1.8.2.min.js" language="javascript"></script>
<script type="text/javascript" src="http://isaac.issharepoint.com/Script/jquery.SPServices-0.7.2.min.js" language="javascript"></script>
<script type="text/javascript" src="http://isaac.issharepoint.com/Script/utilityFunctions.js" language="javascript"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>


<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("buildChartFunc");
//The Google Visualization library implements several classes and methods that you'll need to manipulate all charts
google.load("visualization", "1", {packages:["corechart"]});

//$(document).ready(function() { 
function buildChartFunc()
{

var Over30=0;
var Under30=0;

var anArray = new Array();

   // Initialize data object to hold chart data
    var dataAn = new google.visualization.DataTable();
    dataAn.addColumn('string', 'Month');
    dataAn.addColumn('string', 'Analyst');
    dataAn.addColumn('number', 'Correspondence Closed Over 30');
	
<!-- Step 2 - Prepare your data -->	
//push names of analyst into an array to use later
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Analyst",
	CAMLViewFields: "<ViewFields><FieldRef Name='Title'></FieldRef></ViewFields>",
	CAMLQuery: "<Query><OrderBy><FieldRef Name='Title' /></OrderBy></Query>",
    completefunc: function (xData, Status) {
	//alert(xData.responseXML.xml);
      $(xData.responseXML).SPFilterNode("z:row").each(function() { 
			anArray.push({
				Analyst: $(this).attr("ows_Title"),
				Count:	 0
				});			
      });
     }
  }); 
  
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Log",
	CAMLViewFields: "<ViewFields><FieldRef Name='Days'></FieldRef><FieldRef Name='Analyst'></FieldRef></ViewFields>",
	CAMLQuery: "<Query><OrderBy><FieldRef Name='Days' /></OrderBy></Query>",
    completefunc: function (xData, Status) {
	//alert(xData.responseXML.xml);
      $(xData.responseXML).SPFilterNode("z:row").each(function() { 		
//As you loop through the array, if 'Analyst' name equals what's in the array, then increment
		for (var x=0; x<anArray.length; x++)
		{
			if ($(this).attr("ows_Analyst") == anArray[x].Analyst)
			{
				anArray[x].Count += 1;
			}
		}				
      });
     }
  });
  
   $.each(anArray, function (index, value) {																													
        dataAn.addRow([anArray[index].Analyst,anArray[index].Count]);
    }); 

	  
	 // Create and draw the visualization.
        new google.visualization.ColumnChart(document.getElementById('visualization')).
            draw(dataAn,
                 {title:"Conformance Log",
                  width:900, height:400,
                  hAxis: {title: "Analyst"}}
            );	  

}
</script>
<table>
	<tr><td><div id="visualization" style="width: 650px; height: 400px;"></div></td></tr>
</table>

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question