Array of arrays help required - Javascript

  • Thread starter Thread starter Deleted member 66701
  • Start date Start date

Deleted member 66701

D

Deleted member 66701

Hi all. I have the following code:-

Code:
			google.load('visualization', '1', {packages:['orgchart']});  
			google.setOnLoadCallback(drawChart);  
			function drawChart() {  
				var htmlString = $("div#chartContents").html();
				htmlString = htmlString.replace(/<br>/g,'');
				htmlString = htmlString.replace(/\s+/g, '');
				alert(htmlString); 
				var data = new google.visualization.DataTable();  
				data.addColumn('string', 'Node');  
				data.addColumn('string', 'Parent');  
Possible problem?--->	data.addRows(htmlString);
				var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));  
				chart.draw(data);  
			}

htmlString contains
Code:
[['Room 1', ''],
['Room 2', 'Room 1'],
['Room 3', 'Room 1']]

However the call to the chart api fails with "Uncaught Error: Every row given must be either null or an array." I think this is because data.addRows expects an array of arrays as a parameter but I'm passing a string.

I don't know how to solve this.
 
Last edited by a moderator:
Solved it - formatted string as JSON:-

Code:
["Room 1", ""],
["Room 2", "Room 1"],
["Room 3", "Room 1"],
["Room 4", "Room 2"],
["Empty Room opt 2b", "Room 2"],
["Empty Room opt 3a", "Room 3"],
["Empty Room opt 3b", "Room 3"],
["Empty Room opt 4b", "Room 4"],

Then used JSON.parse on the string. New code:-

Code:
			google.load('visualization', '1', {packages:['orgchart']});  
			google.setOnLoadCallback(drawChart);  
			function drawChart() {  
				var htmlString = $("div#chartContents").html();
				htmlString = htmlString.replace(/<br>/g,'');
				htmlString = htmlString.replace(/\s+/g, '');
				htmlString = htmlString.slice(0,-1);
				//alert(htmlString); 
				var arr = JSON.parse("[" + htmlString + "]");
				//alert(arr);
				var data = new google.visualization.DataTable();  
				data.addColumn('string', 'Node');  
				data.addColumn('string', 'Parent');  
				data.addRows(arr);
				var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));  
				chart.draw(data);  
			}
 
Last edited by a moderator:
Back
Top Bottom