I am using JavaScript to display a list of data arrays on my website from Google sheets. At the moment the data is just printed as a long list so I would like to style it, starting by having each array in its own div. At the moment all the data is appended into the same div and is only separated by <p> tags.
In my code below you can see I have tried to create the individual divs with class 'group' but this just repeatedly prints all the data into <div class="group">, instead of each array being in a <div class="group">.
// ID of the Google Spreadsheet
var spreadsheetID = "1pruXYXrbITR7CoQXZG1I1kyS4DahYBXmGMDEK2T6MpY";
// Make sure it is public or set to Anyone with link can view
var url = "https://spreadsheets.google.com/feeds/list/1pruXYXrbITR7CoQXZG1I1kyS4DahYBXmGMDEK2T6MpY/od6/public/values?alt=json";
$(document).ready(function() {
//Get JSON data
$.getJSON(url, function(data) {
var entry = data.feed.entry;
// the master obj
var grouped = {};
// socs obj
var socs = {};
var i = 0;
entry.forEach(function(a) {
grouped[a.gsx$name.$t] = grouped[a.gsx$name.$t] || [];
socs[i] = a.gsx$name.$t;
grouped[a.gsx$name.$t].push({
position: a.gsx$position.$t,
member: a.gsx$member.$t
});
i++;
});
// remove the duplicates from socs array
var uniqueSocs = [];
$.each(socs, function(i, el) {
if ($.inArray(el, uniqueSocs) === -1) uniqueSocs.push(el);
});
$(uniqueSocs).each(function(key, content) {
//debug
//console.log(grouped[content]);
//the socs and positions
socPos = grouped[content]
var $newDiv = $("<div/>").addClass("group");
//print the soc
$('.test').append($newDiv);
$('.group').append("<h4>" + content + "</h4>");
$(socPos).each(function(key, param) {
//print the positions
$('.group').append(param.position + " - " + param.member + "<br />")
});
});
});
});