Weather App - Using Symbols instead of text - html
Ok, so I'm currently trying to create a simple weather app as part of learning how to use RSS feeds. I have the weather being displayed as text E.g. Friday: Sunny, Max Temp, Min Temp.
I want to change that text into symbols, so that instead of saying "Sunny" it displayed the image of a sun. I'll show the HTML and Javascript below. Hopefully it makes sense and I can get this problem sorted out.
HTML
<div id="home" data-role="page">
<div data-role="header" data-add-back-btn="true" >
<h1>Your Handy Little Weather App</h1>
</div>
<div data-role="content">
<img src ="./images/UWSLogo.png" width ="174" height ="116" alt ="Logo" align ="right"/>
<h2>UWS Weather</h2>
<p>Check the weather at the UWS Campuses.</p>
<p>Choose your desired location.</p>
<ul data-role="listview" data-inset="true">
<li><a id="ayrFeed" href="#ayr">Ayr</a></li>
<li><a id="dumfriesFeed" href="#dumfries">Dumfries</a></li>
<li><a id="hamiltonFeed" href="#hamilton">Hamilton</a></li>
<li><a id="paisleyFeed" href="#paisley">Paisley</a></li>
</ul>
<h2>Other Weather</h2>
<p>Find out more with our other weather options.</p>
<ul data-role="listview" data-inset="true">
<li><a id="uniFeed" href="#uni">Other Universities</a></li>
<li><a id="holidayFeed" href="#holiday">Popular Holiday Destinations</a> </li>
</ul>
</div>
</div>
</div>
<div id="ayr" data-role="page">
<div data-role="header">
<h1 id="ayrTitle"></h1>
</div>
<div data-role="content">
<ul id="ayrList" data-role="listview" data-inset="true">
<!-- Weather reports go here. -->
</ul>
</div>
</div>
<div id="dumfries" data-role="page">
<div data-role="header">
<h1 id="dumfriesTitle"></h1>
</div>
<div data-role="content">
<ul id="dumfriesList" data-role="listview" data-inset="true">
<!-- Weather reports go here. -->
</ul>
</div>
</div>
</div>
<div id="hamilton" data-role="page">
<div data-role="header">
<h1 id="hamiltonTitle"></h1>
</div>
<div data-role="content">
<ul id="hamiltonList" data-role="listview" data-inset="true">
<!-- Weather reports go here. -->
</ul>
</div>
</div>
Javscript
$(document).ready(function() {
$("#ayrFeed").bind('click', function() {
getFeed("http://open.live.bbc.co.uk/weather/feeds/en/2656708/3dayforecast.rss",
showAyrWeatherFeed);
});
$("#dumfriesFeed").bind('click', function() {
getFeed("http://open.live.bbc.co.uk/weather/feeds/en/2650798/3dayforecast.rss",
showDumfriesWeatherFeed);
});
$("#hamiltonFeed").bind('click', function() {
getFeed("http://open.live.bbc.co.uk/weather/feeds/en/2647570/3dayforecast.rss",
showHamiltonWeatherFeed);
});
function getFeed(url, success){
if(window.navigator.onLine) {
$.jGFeed(url, function(feeds) {
// Check for errors
if(!feeds){
// there was an error
return false;
} else {
localStorage.setItem(url, JSON.stringify(feeds));
success(feeds.title, feeds.entries);
}
});
} else {
// Get the fall-back...
var feed = JSON.parse(localStorage.getItem(url));
if(feed && feed.length > 0) {
success(feed.title, feed.entries);
}
}
}
function showPaisleyWeatherFeed(title, items) {
$("#paisleyTitle").text(title);
var list = $("#paisleyList");
list.empty();
for(var index = 0; index < items.length; index += 1) {
list.append(formatItem(items[index]));
}
$.mobile.changePage($("#paisley"), "flip");
list.listview("refresh");
}
function showHamiltonWeatherFeed(title, items) {
$("#hamiltonTitle").text(title);
var list = $("#hamiltonList");
list.empty();
for(var index = 0; index < items.length; index += 1) {
list.append(formatItem(items[index]));
}
$.mobile.changePage($("#hamilton"), "flip");
list.listview("refresh");
}
function formatItem(item) {
var listItem = document.createElement("li"),
anchor = document.createElement("a");
anchor.setAttribute("href", item.link);
anchor.innerText = item.title;
listItem.innerHTML = anchor.outerHTML;
return listItem.outerHTML;
}
<img src="url" alt="some_text">
Just add this to your code and include the url of the image you would like in the tag.
Here's a FIDDLE with little example if it helps you.
<div>New York Sunny 85F</div>
div {
background: #ddd;
width: 200px;
height: 80px;
line-height: 80px;
font-size: 23px;
text-align: center;
border-radius: 4px;
}
.weather-icon {
width: 38px;
height: 38px;
vertical-align: text-bottom;
}
$(function() {
$('div').html(function() {
return $(this).html().replace('Sunny','<img src="https://cdn1.iconfinder.com/data/icons/iconsland-weather/PNG/256x256/Sunny.png" class="weather-icon">');
});
});
Related
How can I rotate a circle with items inside it?
I have been trying to rotate a circle with the items inside of it using a button, when I change the rotate value of the circle directly in css it works fine but there appears to be a problem in the html code. html code: <html> <head> <title>web desgin test</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="main"> <nav> <div class="logo"> <img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/logo.png" alt=""> </div> <div class="links"> <ul> <li>Home</li> <li>Phone</li> <li>Accessories</li> <li>Cart</li> </ul> </div> </nav> <div class="information"> <img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/mobile.png" class="mobile"> <div id="circle"> <div class="feature one"> <img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/camera.png" alt=""> <div> <h1>Camera</h1> <p>108MP Main camera</p> </div> </div> <div class="feature two"> <img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/processor.png" alt=""> <div> <h1>processor</h1> <p>Exynos 990</p> </div> </div> <div class="feature three"> <img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/display.png" alt=""> <div> <h1>Display</h1> <p>6.9 inch, 1440px</p> </div> </div> <div class="feature four"> <img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/battery.png" alt=""> <div> <h1>Battery</h1> <p>4500 mA</p> </div> </div> </div> </div> <div class="controls"> <img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/arrow.png" id="upBtn"> <h3>Features</h3> <img src="C:/Users/USER/Downloads/RealPhone_WebPage_Images/images/arrow.png" id="downBtn"> </div> </div> <script> var circle = document.getElementById('circle'); var upBtn = document.getElementById('upBtn'); var downBtn = document.getElementById('downBtn'); var rotateValue = circle.style.transform(); var rotateSum; upBtn.onclick = function() { rotateSum = rotateValue + "rotate(-90deg)"; circle.style.transform = rotateSum; rotateValue = rotateSum; } </script> css code: enter code here #circle { width: 1000px; height: 1000px; position: absolute; top: 0; left: 0; border-radius: 50%; transform: rotate(0deg); .controls { position: absolute; right: 10%; top: 50%;`enter code here` transform: translateY(-50%); text-align: center; } .controls h3 { margin: 15px 0; color: #fff; } #upBtn { width: 15px; cursor: pointer; } #downBtn { width: 15px; cursor: pointer; transform: rotate(180deg); } ................................................................................
Your variable declarations are wrong if for example the id is #circle document.getElementById(circle); --> document.getElementById('circle'); It is better when you post all html according to this function
There is no circle in your html code... So when you do this : var circle = document.getElementById(circle); var circle is null, because there is not a html element where its id is circle, and you are trying to take one called like that Also, when you do this: document.getElementById(circle); is going to look for a string in every id, so it should be : document.getElementById('circle'); edit: for been clear, when you use selector, you are looking for a matching string, when you do this: var circle = document.getElementById(circle); var upBtn = document.getElementById(upBtn); var downBtn = document.getElementById(downBtn); It should be this: var circle = document.getElementById('circle'); var upBtn = document.getElementById('upBtn'); var downBtn = document.getElementById('downBtn'); In the firt one you are sayin look for a string with the value os circle var circle = 'notCircle' var circle = document.getElementById(circle); console.log(circle) In the second you are looking direclty for x string on html ids
can someone please can point out what is going wrong with this jquery or css that has been adapted from a snippet found
I've taken this jquery snippet from a post made years ago, but i've been unable to adapt it for what I want, simply highlighting in some way the text in the navigation on the left hand side based on which div is within view, It's most likely something really stupid going wrong, but I would just like a little help in getting the answer. Apologies for the dispicable formatting, I'm new! html: <div class="navwrap"> <nav> <ul> <li>PotD </li> <li>Description </li> <li>Extra </li> </ul> </nav> </div> <div class="container" id="two"> <div class="container-2-content" id="PotD"> <h2> Aquilegia vulgaris </h2> <h3> "common columbine" </h3> </div> </div> <div class="container" id="three"> <div class="container-3-content" id="Description"> <h2> Description </h2> </div> </div> <div class="container" id="four" > <div class="container-4-content" id="Extra"> <h2> Extra </h2> </div> </div> css: li a:hover, li.a.active{ cursor:pointer; font-size:30px; font-weight: 600; color: rgb(255, 253, 234); border-top: 3px solid rgb(255, 253, 234); border-bottom: 3px solid rgb(255, 253, 234); } jquery: $(document).ready(function(){ var $sections = $('.container'); $(window).scroll(function(){ var currentScroll = $(this).scrollTop(); var $currentSection $sections.each(function(){ var divPosition = $(this).offset().top; if( divPosition - 1 < currentScroll ){ $currentSection = $(this); } var id = $currentSection.attr('id'); $('').removeClass('active'); $("[href=#"+id+"]").addClass('active'); }) }); });
It's possible you are looking for something called ScrollSpy. The bootstrap framework has it ready to go.
W3C Markup Validation gives me 3 errors on one line that seems to work
I ran my website in the W3C Validator only to be given these errors: <a href="#header-anchor"> <img src="img/banner.png" width="169" height="54" alt="Logo"> </a> The errors I got are: Line 14, column 28: An a start tag seen but an element of the same type was already open. Line 14, column 28: End tag a violates nesting rules. Line 14, column 28: Cannot recover after last error. Any further errors will be ignored. I am not sure what is causing this error, so any help will appreciated. Thanks. EDIT: Apparently this piece of code is causing the error but I am not sure what it is exactly causing it: <a href="index.html"> Edit 2: Whole code: <!DOCTYPE HTML> <html> <head> <title>Title</title> <meta charset="UTF-8"> <link href="css/style.css" rel="stylesheet"> <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet"> </head> <body> <!-- Banner --> <a class="anchor" id="header-anchor"> <div id="header-wrapper"> <header role="banner" id="header"> <a href="index.html"> <img src="img/banner.png" width="169" height="54" alt="Logo"> </a> <nav role="navigation"> <ul> <li>HOME</li> <li>SERVICES</li> <li>OUR TEAM</li> <li>ABOUT</li> <li>CONTACT</li> </ul> </nav> </header> </div> <div id="banner-wrapper"> <section id="banner"> <h2>Title</h2> <h3>Tagline</h3> </section> </div> <!-- Services --> <a class="anchor" id="services-anchor"></a> <div class="wrapper"> <section id="services" class="group"> <h2>Our Services</h2> <div class="floatleft small"> <span class="fa-stack fa-3x"> <i class="fa fa-line-chart fa-stack-1x fa-inverse"></i> </span> <h3>Service 1</h3> <p>Service 1</p> </div> <div class="floatleft small"> <span class="fa-stack fa-3x"> <i class="fa fa-money"></i> </span> <h3>Service 1</h3> <p>Service 1</p> </div> <div class="floatleft small"> <span class="fa-stack fa-3x"> <i class="fa fa-globe"></i> </span> <h3>Service 1</h3> <p>Service 1</p> </div> </section> </div> <!-- About us --> <a class="anchor" id="team-anchor"></a> <section id="team" class="group"> <h2>Our Team</h2> <div class="floatleft mid"> <img src="img/ben.jpg" width="250" height="250" alt="Ben Fitchew"> <h3>Team 1</h3> <p>Team 1</p> </div> <div class="floatleft mid"> <img src="img/jeremy.jpg" width="250" height="250" alt="Jeremy Lang"> <h3>Team 1</h3> <p>Team 1</p> </div> <div class="floatright mid"> <img src="img/gianluca.jpg" width="250" height="250" alt="Gianluca Monaco"> <h3>Team 1</h3> <p>Team 1</p> </div> <div class="floatright mid"> <img src="img/will.jpg" width="250" height="250" alt="William Pattisson"> <h3>Team 1</h3> <p>Team 1</p> </div> </section> <!-- About --> <div class="wrapper"> <a class="anchor" id="about-anchor"></a> <section id="about" class="group" role="main"> <h2>About</h2> <h3>About</h3> <p>About</p> </section> </div> <!-- Slideshow --> <div class="wrapper"> <h2>Slideshow</h2> <h3>Slideshow</h3> <p class="Slideshow"><img src='images/picture1.jpg' name='SlideShow'/></p> </section> </div> <!-- Javascript code --> <script type="text/javascript"> var slideShowSpeed = 2000; var Pic = new Array(); Pic[0]='img/picture1.jpg'; Pic[1]='img/picture2.jpg'; Pic[2]='img/picture3.jpg'; Pic[3]='img/picture4.jpg'; Pic[4]='img/picture5.jpg'; Pic[5]='img/picture6.jpg'; var currentPicture = 1; var pictureNo = Pic.length; var preLoad = new Array(); for (i = 0; i < pictureNo; i++) { preLoad[i] = new Image(); preLoad[i].src = Pic[i]; } function runSlideShow() { if (document.all) { document.images.SlideShow.style.filter="blendTrans(duration=2)"; document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuration=3)"; document.images.SlideShow.filters.blendTrans.Apply(); } document.images.SlideShow.src = preLoad[currentPicture].src; if (document.all) { document.images.SlideShow.filters.blendTrans.Play(); } currentPicture = currentPicture + 1; if (currentPicture >= pictureNo) currentPicture = 0; setTimeout('runSlideShow()', slideShowSpeed); } </script> <script> runSlideShow(); </script> <!-- Contact --> <a class="anchor" id="contact-anchor"></a> <div class="map-wrapper"> <section id="contact" class="group"> <h2>Contact Us</h2> <div id="map-canvas" class="floatleft mid"></div> <div class="floatright mid"> <form id="frmContact" action="mail.php" method="post" role="form"> <label for="txtName" id="lblName">Name</label> <input type="text" name="txtName" id="txtName" aria-labelledby="lblName" required placeholder="Enter your name here" minlength="3" maxlength="70" pattern="[a-zA-Z ]+"> <span class="error_show">Name is not valid!</span> <label for="txtEmail" id="lblEmail">Email</label> <input type="email" name="txtEmail" id="txtEmail" aria-labelledby="lblEmail" required placeholder="Enter your email here" minlength="5" maxlength="254"> <span class="error_show">Email is not valid!.</span> <label for="txtMessage" id="lblMessage">Message</label> <textarea name="txtMessage" id="txtMessage" aria-labelledby="txtMessage" required placeholder="Enter your message here"></textarea> <span class="error_show">Message is not valid!</span> <input type="submit" name="submit" id="submit" value="Send Message"> </form> </div> </section> </div> <!-- Javascript --> <script src="js/jquery-2.1.1.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js"></script> <script> // Code for Google Map function initialize() { var mapCanvas = document.getElementById('map-canvas'); var myLatLng = new google.maps.LatLng(51.51463,-0.106533); var mapOptions = { center: myLatLng, zoom: 15, mapTypeId: google.maps.MapTypeId.ROADMAP } var map = new google.maps.Map(mapCanvas, mapOptions); var marker = new google.maps.Marker({ position: myLatLng, map: map, title: 'Ardevora' }); } google.maps.event.addDomListener(window, 'load', initialize); $(document).ready(function() { // Form Validation Code $('#txtName').on('input', function() { var input=$(this); var is_name=input.val(); if (is_name) { input.removeClass("invalid").addClass("valid"); input.next().hide(); } else { input.removeClass("valid").addClass("invalid"); input.next().show(); } }); $('#txtEmail').on('input', function() { var input=$(this); var regex = /^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,6}$/i; var is_email=regex.test(input.val()); if (is_email) { input.removeClass("invalid").addClass("valid"); input.next().hide(); } else { input.removeClass("valid").addClass("invalid"); input.next().show(); } }); $('#txtMessage').keyup(function(event) { var input=$(this); var message=$(this).val(); if (message) { input.removeClass("invalid").addClass("valid"); input.next().hide(); } else { input.removeClass("valid").addClass("invalid"); input.next().show(); } }); $("#submit").click(function(event) { var form_data=$("#frmContact").serializeArray(); var error_free=true; for (var input in form_data){ var element=$("#"+form_data[input]['name']); var valid=element.hasClass("valid"); var error_element=element.next(); if (!valid) { error_element.show(); error_free=false; } else { error_element.hide(); } } if (!error_free){ event.preventDefault(); } }); }); </script> <!-- Cookie Script --> <script type="text/javascript"> (function(){ var msg = "We use cookies to enhance your web browsing experience. By continuing to browse the site you agree to our policy on cookie usage."; var closeBtnMsg = "Ok I comply"; var privacyBtnMsg = "Privacy Policy"; var privacyLink = "http://www.google.com"; //check cookies if(document.cookie){ var cookieString = document.cookie; var cookieList = cookieString.split(";"); // if cookie named OKCookie is found, return for(x = 0; x < cookieList.length; x++){ if (cookieList[x].indexOf("OKCookie") != -1){return}; } } var docRoot = document.body; var okC = document.createElement("div"); okC.setAttribute("id", "okCookie"); var okCp = document.createElement("p"); var okcText = document.createTextNode(msg); //close button var okCclose = document.createElement("a"); var okcCloseText = document.createTextNode(closeBtnMsg); okCclose.setAttribute("href", "#"); okCclose.setAttribute("id", "okClose"); okCclose.appendChild(okcCloseText); okCclose.addEventListener("click", closeCookie, false); //privacy button var okCprivacy = document.createElement("a"); var okcPrivacyText = document.createTextNode(privacyBtnMsg); okCprivacy.setAttribute("href", privacyLink); okCprivacy.setAttribute("id", "okCprivacy"); okCprivacy.appendChild(okcPrivacyText); //add to DOM okCp.appendChild(okcText); okC.appendChild(okCp); okC.appendChild(okCclose); okC.appendChild(okCprivacy); docRoot.appendChild(okC); okC.classList.add("okcBeginAnimate"); function closeCookie(){ var cookieExpire = new Date(); cookieExpire.setFullYear(cookieExpire.getFullYear() +2); document.cookie="OKCookie=1; expires=" + cookieExpire.toGMTString() + ";"; docRoot.removeChild(okC); } })(); </script> <!-- Footer --> <footer role="contentinfo"> <!-- Left Footer --> <ul class="floatleft"> <li>HOME</li> <li>SERVICES</li> <li>OUR TEAM</li> <li>ABOUT</li> <li>CONTACT</li> </ul> <!-- Right Footer --> <ul class="floatright"> <p>Copyright 2014</p> </ul> </footer> </body> </html>
What is your declaration of page? Likely to be a bug in the non-completion of the <img /> tag. <img src="img/banner.png" width="169" height="54" alt="Logo" /> Tag <a> should have a title="Title a" consistent with alt="Alt img" image
The tag <a class="anchor" id="header-anchor"> has no closing tag. This causes the elements after it to be taken as its content, which in turn leads to violation of element nesting rules. The simplest fix is to add the closing tag immediately after the start tag: <a class="anchor" id="header-anchor"></a> Such an element is not good style, though. You can probably work with just id attributes and class attributes assigned to elements with real content. Fixing the problem causes some errors later in the document to be reported, but they are relatively easy to analyze (especially with the help of the explanations).
Navbar add remove class from anchor when scrolling the page
I'm new here, but I alreadyy searched a while for a solution not only on this site. Ok, here's the problem: I built a static page, with multiple sections. Each section can be shown up through an anchorlink in the navbar, which is static on the left side of the page. How can I achieve, that the link to the section, which is just shown on the screen, is active? It should be pretty easy, if the sections would be in external html-files. But I can't find a way to make this happen in my case... <div id="navbar"> <ul> <li><h5>SERVICE </br> </br></h5></li> <li><h5>LEISTUNGEN </br> </br></h5></li> <li><h5>ÜBER UNS </br> </br></h5></li> <li><h5>PARTNER </br> </br></h5></li> <li><h5>KONTAKT </br></h5></li> </ul> I think of a JavaScript that changes the classes of the links onclick, but I can't get this to work... Please help me, tell me what I should do! Last but not least, please excuse my poor english and coding knowledge ;) For further explanation of the page I want to create! This is my Navigation Menu, which has the fixed position on the left! <div id="navbar"> <ul> <li><h5>SERVICE </br> </br></h5></li> <li><h5>LEISTUNGEN </br> </br></h5></li> <li><h5>ÜBER UNS </br> </br></h5></li> <li><h5>PARTNER </br> </br></h5></li> <li><h5>KONTAKT </br></h5></li> </ul> </div> It continues with one big section, which's contents are 4 small sections. These sections look like this: <div id="weiss"> <div id="1" class="content section" data-id="1"> <div class="page page-1"> <div class="topic"> <img class="mwlogo" src="media/logo.png"/> </div> <div class="text"> <h2>...</h2> </div> <div class="vorteile"> <div class="first"> <ol> <li><p>...</li> <li><p>...</p></li> <li><p>...</p></li> <li><p>...</p></li> <li><p>...</p></li> </ol> </div> <div class="last"> <ol> <li><p>...</p></li> <li><p>...</p></li> <li><p>...</p></li> <li><p>...</p></li> <li><p>...</p></li> <li><p>...</p></li> <li><p>...</p></li> </ol> </div> </div> </div> </div> <div> I linked to the activemenuitem in the index.html: <script type="text/javascript" src="functions/js/activemenuitem.js"></script> It looks like this: function checkActiveSection () { var fromTop = jQuery(window).scrollTop() ; jquery('#weiss .section'.each(function() { var sectionOffset = jQuery(this).offset() ; if ( sectionOffset.top <= fromTop ) { jQuery('#navbar li').removeClass('active') ; jQuery('#navbar li[data-id="'+jQuery(this).data('id')+'"]').addClass('active') ; } }) ; } jQuery(window).scroll(checkActiveSection) ; jQuery(document).ready(checkActiveSection) ; jQuery('#navbar li a').click(function(e){ var idSectionGoto = jQuery(this).closest('li').data('id') ; $('html, body').stop().animate({ scrollTop: jQuery('#weiss .section[data-id="'+idSectionGoto+'"]').offset().top }, 300,function(){ checkActiveSection() ; }); e.preventDefault() ; }) ; But the problem is still, that there seems to be no way for me, to get this to work! The script loads when loading the site, but no class is being added or removed from the menu classes. What do I have to do?! Please help me!
<script type="text/javascript"> $(document).ready(function(){ $(window).scroll(function(e){ var topMargin = jQuery(window).scrollTop() ; if(topMargin < 100) { $("#navbar li a").removeClass('active'); $("#navbar li a.chap1").addClass('active'); } else if(topMargin > 100 && topMargin <200) { $("#navbar li a").removeClass('active'); $("#navbar li a.chap2").addClass('active'); } else { $("#navbar li a").removeClass('active'); $("#navbar li a.chap3").addClass('active'); } }); }); </script> <div id="navbar"> <ul> <li><h5>SERVICE </br> </br></h5></li> <li><h5>LEISTUNGEN </br> </br></h5></li> <li><h5>ÜBER UNS </br> </br></h5></li> </ul> </div>
Here is a solution adapted from Change Active Menu Item on Page Scroll? to your case : http://jsfiddle.net/sX5Kq/1/ It's much more simple even if it's not completly optimised. <div id="navbar"> <ul> <li><h5>SERVICE</h5></li> <li><h5>LEISTUNGEN</h5></li> <li><h5>ÜBER UNS</h5></li> <li><h5>PARTNER</h5></li> <li><h5>KONTAKT</h5></li> </ul> </div> <div id="sections"> <div class="section" data-id="1"> <h2>Services</h2> <p></p> </div> <div class="section" data-id="2"> <h2>LEISTUNGEN</h2> <p></p> </div> <div class="section" data-id="3"> <h2>Section 3</h2> <p></p> </div> <div class="section" data-id="4"> <h2>Section 4</h2> <p></p> </div> <div class="section" data-id="5"> <h2>Section 5</h2> <p></p> </div> </div> CSS body { font-family: Helvetica, Arial; } #navbar { position: fixed; z-index: 1; left: 0; right: 0; top: 0; width:100px ; } #navbar li.active a { color:red ; font-weight:bold ; } #sections { margin-left:110px ; } JS function checkActiveSection() { var fromTop = jQuery(window).scrollTop() ; jQuery('#sections .section').each(function(){ var sectionOffset = jQuery(this).offset() ; if ( sectionOffset.top <= fromTop ) { jQuery('#navbar li').removeClass('active') ; jQuery('#navbar li[data-id="'+jQuery(this).data('id')+'"]').addClass('active') ; } }) ; } jQuery(window).scroll(checkActiveSection) ; jQuery(document).ready(checkActiveSection) ; jQuery('#navbar li a').click(function(e){ var idSectionGoto = jQuery(this).closest('li').data('id') ; $('html, body').stop().animate({ scrollTop: jQuery('#sections .section[data-id="'+idSectionGoto+'"]').offset().top }, 300,function(){ checkActiveSection() ; }); e.preventDefault() ; }) ;
populating angular div with children's children
Folks- I have a data structure of nested parent-child structure. The first level (parents) drives angular to create a row of buttons. Selecting one of these buttons populates a list with the second level (children). Now I also need that selection to drive a third level (grandchildren). That third div needs to have all the grandchildren of the children. As I'm relatively new to angular, I'm afraid I'm looking at it too procedurally. Code follows: <html ng-app="KPI_Scorecard"> <head> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> <!--script src="js/controllers.js"></script--> <script> var $j = jQuery.noConflict(); var KPI_Scorecard = angular.module('KPI_Scorecard', []); var listL2; var myChildren; var myChildrenChildren = [] KPI_Scorecard.controller('KPIListCtrl', function ($scope) { $scope.showL2Content = function(whatChildren) { $scope.myChildren = whatChildren; $scope.myChildrenChildren; for (x=0;x<$scope.myChildren.length;x++) { myChildrenChildren = []; for (y=0;y<$scope.myChildren[x].children.length;y++) { myChildrenChildren.push($scope.myChildren[x].children[y]); } //console.log(myChildrenChildren); }; console.log(myChildrenChildren.length); }; $scope.showL3Content = function(whatChildren) { //console.log(whatChildren); }; $scope.myList = [{"id":"Jack","employeeLevel":"1","managerName":"John","l1Mgr":"none","l2Mgr":"none","goals":[{"goalName":"Margin","goalWeight":"0.5","goalColor":"11.7"},{"goalName":"Expense","goalWeight":"0.25","goalColor":"NULL"}],"children":[{"id":"Chuck","employeeLevel":"2","managerName":"Jack","l1Mgr":"Jack","l2Mgr":"none","goals":[{"goalName":"Expense","goalWeight":"0.4","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[{"id":"David","employeeLevel":"3","managerName":"Chuck","l1Mgr":"Jack","l2Mgr":"Chuck","goals":[{"goalName":"budget","goalWeight":"0.4","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[]},{"id":"Scott","employeeLevel":"3","managerName":"Chuck","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Investment","goalWeight":"0.4","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[{"id":"Jill","employeeLevel":"4","managerName":"Scott","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Data","goalWeight":"0.5","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}]},{"id":"Rick","employeeLevel":"4","managerName":"Scott","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"technology","goalWeight":"1","goalColor":"NULL"},{"goalName":"Data","goalWeight":"0.5","goalColor":"NULL"}],"children":[]}]}]},{"id":"Js","employeeLevel":"2","managerName":"Jack","l1Mgr":"Jack","l2Mgr":"none","goals":[{"goalName":"Cross","goalWeight":"0.2","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[{"id":"Alison","employeeLevel":"3","managerName":"Js","l1Mgr":"Jack","l2Mgr":"Js","goals":[{"goalName":"Research","goalWeight":"0.5","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[]},{"id":"Peter","employeeLevel":"3","managerName":"Js","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Invest","goalWeight":"0.5","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.33","goalColor":"NULL"}],"children":[]}]}]},{"id":"Jim","employeeLevel":"1","managerName":"John","l1Mgr":"none","l2Mgr":"none","goals":[{"goalName":"Actual","goalWeight":"0.34","goalColor":"1.49"},{"goalName":"Retention","goalWeight":"1","goalColor":"97"}],"children":[{"id":"Anne","employeeLevel":"2","managerName":"Jim","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Actual","goalWeight":"0.34","goalColor":"1.49"},{"goalName":"Retention","goalWeight":"1","goalColor":"97"}],"children":[{"id":"Marisa","employeeLevel":"3","managerName":"Anne","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Spending","goalWeight":"1","goalColor":"NULL"},{"goalName":"Retention","goalWeight":"1","goalColor":"97"}],"children":[]},{"id":"Linda","employeeLevel":"3","managerName":"Anne","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Actual","goalWeight":"0.34","goalColor":"1.49"},{"goalName":"Retention","goalWeight":"1","goalColor":"97"}],"children":[]}]},{"id":"Gene","employeeLevel":"2","managerName":"Jim","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Actual","goalWeight":"0.34","goalColor":"1.49"},{"goalName":"Total","goalWeight":"0.33","goalColor":"92.96"}],"children":[{"id":"Kathleen","employeeLevel":"3","managerName":"Gene","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"% Clients","goalWeight":"1","goalColor":"NULL"},{"goalName":"Employee","goalWeight":"1","goalColor":"90"}],"children":[]},{"id":"Chris","employeeLevel":"3","managerName":"Gene","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"processes","goalWeight":"1","goalColor":"NULL"},{"goalName":"Planning","goalWeight":"1","goalColor":"NULL"}],"children":[]}]}]}]; }) </script> </head> <body ng-controller="KPIListCtrl"> <div data-role="page"> <div data-role="header" data-theme="b"> <h1>SCORECARDS</h1> HOME </div> <div data-role="content" style="height:400px"> <ul data-role="listview" id="list-L2" data-divider-theme="b" data-inset="true" style="width: 20%; display: inline-block;" ng-click="$event.preventDefault()">L2 Scorecards <li ng-repeat="aName in myChildren"> {{aName.id}} </li> </ul> <ul data-role="listview" id="list-L3" data-divider-theme="b" data-inset="true" style="width: 20%; display: inline-block;" ng-click="$event.preventDefault()">L3 Scorecards <li ng-repeat="aName2 in myChildrenChildren"> {{aName2}} </li> </ul> </div> <div data-role="footer" id="list-L1" ng-click="$event.preventDefault()"> </form> <span ng-repeat="aName in myList" style="float:left"> {{user.name}}<br> <button ng-click="showL2Content(aName.children)">{{aName.id}}</button> </span></div> </div> </body> </html>
This article talks about using recursion in directives: http://sporto.github.io/blog/2013/06/24/nested-recursive-directives-in-angular/ which sounds like it will solve your problem.
this works, but I'm feeling like I should either preprocess the JSON differently, or there might be a more 'angular' way of doing this. <html ng-app="KPI_Scorecard"> <head> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script> <!--script src="https://ribbit.fmr.com/resources/statics/379584/angular.min.js"></script--> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> <!--script src="js/controllers.js"></script--> <script> var $j = jQuery.noConflict(); var KPI_Scorecard = angular.module('KPI_Scorecard', []); var listL2; var myChildren; var myL3s = [] KPI_Scorecard.controller('KPIListCtrl', function ($scope) { $scope.showL2Content = function (whatChildren) { var myL3s = [] $scope.myChildren = whatChildren; for (x = 0; x < whatChildren.length; x++) { for (y = 0; y < whatChildren[x].children.length; y++) { myL3s.push(whatChildren[x].children[y]); } }; $scope.myChildrenChildren= myL3s; console.log(myL3s); }; //console.log(myChildrenChildren); $scope.showL3Content = function (whatChildren) { //console.log(whatChildren); }; $scope.myList = [{"id":"Jack","employeeLevel":"1","managerName":"John","l1Mgr":"none","l2Mgr":"none","goals":[{"goalName":"Margin","goalWeight":"0.5","goalColor":"11.7"},{"goalName":"Expense","goalWeight":"0.25","goalColor":"NULL"}],"children":[{"id":"Chuck","employeeLevel":"2","managerName":"Jack","l1Mgr":"Jack","l2Mgr":"none","goals":[{"goalName":"Expense","goalWeight":"0.4","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[{"id":"David","employeeLevel":"3","managerName":"Chuck","l1Mgr":"Jack","l2Mgr":"Chuck","goals":[{"goalName":"budget","goalWeight":"0.4","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[]},{"id":"Scott","employeeLevel":"3","managerName":"Chuck","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Investment","goalWeight":"0.4","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[{"id":"Jill","employeeLevel":"4","managerName":"Scott","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Data","goalWeight":"0.5","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}]},{"id":"Rick","employeeLevel":"4","managerName":"Scott","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"technology","goalWeight":"1","goalColor":"NULL"},{"goalName":"Data","goalWeight":"0.5","goalColor":"NULL"}],"children":[]}]}]},{"id":"Js","employeeLevel":"2","managerName":"Jack","l1Mgr":"Jack","l2Mgr":"none","goals":[{"goalName":"Cross","goalWeight":"0.2","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[{"id":"Alison","employeeLevel":"3","managerName":"Js","l1Mgr":"Jack","l2Mgr":"Js","goals":[{"goalName":"Research","goalWeight":"0.5","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.25","goalColor":"NULL"}],"children":[]},{"id":"Peter","employeeLevel":"3","managerName":"Js","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Invest","goalWeight":"0.5","goalColor":"NULL"},{"goalName":"Support","goalWeight":"0.33","goalColor":"NULL"}],"children":[]}]}]},{"id":"Jim","employeeLevel":"1","managerName":"John","l1Mgr":"none","l2Mgr":"none","goals":[{"goalName":"Actual","goalWeight":"0.34","goalColor":"1.49"},{"goalName":"Retention","goalWeight":"1","goalColor":"97"}],"children":[{"id":"Anne","employeeLevel":"2","managerName":"Jim","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Actual","goalWeight":"0.34","goalColor":"1.49"},{"goalName":"Retention","goalWeight":"1","goalColor":"97"}],"children":[{"id":"Marisa","employeeLevel":"3","managerName":"Anne","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Spending","goalWeight":"1","goalColor":"NULL"},{"goalName":"Retention","goalWeight":"1","goalColor":"97"}],"children":[]},{"id":"Linda","employeeLevel":"3","managerName":"Anne","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Actual","goalWeight":"0.34","goalColor":"1.49"},{"goalName":"Retention","goalWeight":"1","goalColor":"97"}],"children":[]}]},{"id":"Gene","employeeLevel":"2","managerName":"Jim","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"Actual","goalWeight":"0.34","goalColor":"1.49"},{"goalName":"Total","goalWeight":"0.33","goalColor":"92.96"}],"children":[{"id":"Kathleen","employeeLevel":"3","managerName":"Gene","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"% Clients","goalWeight":"1","goalColor":"NULL"},{"goalName":"Employee","goalWeight":"1","goalColor":"90"}],"children":[]},{"id":"Chris","employeeLevel":"3","managerName":"Gene","l1Mgr":"","l2Mgr":"","goals":[{"goalName":"processes","goalWeight":"1","goalColor":"NULL"},{"goalName":"Planning","goalWeight":"1","goalColor":"NULL"}],"children":[]}]}]}]; }) </script> </head> <body ng-controller="KPIListCtrl"> <div data-role="page"> <div data-role="header" data-theme="b"> <h1>SCORECARDS</h1> HOME </div> <div data-role="content" style="height:400px"> <ul data-role="listview" id="list-L2" data-divider-theme="b" data-inset="true" style="width: 20%; display: inline-block;" ng-click="$event.preventDefault()">L2 Scorecards <li ng-repeat="aName in myChildren"> {{aName.id}} </li> </ul> <ul data-role="listview" id="list-L3" data-divider-theme="b" data-inset="true" style="width: 20%; display: inline-block;" ng-click="$event.preventDefault()">L3 Scorecards <li ng-repeat="aName2 in myChildrenChildren"> {{aName2.id}} </li> </ul> </div> <div data-role="footer" id="list-L1" ng-click="$event.preventDefault()"> </form> <span ng-repeat="aName in myList" style="float:left"> {{user.name}}<br> <button ng-click="showL2Content(aName.children)">{{aName.id}}</button> </span></div> </div> </body> </html>