2016-11-08 3 views
0

Я пытаюсь показать <Candidate></Candidate> в округе Ратленд с только голосами выше 0. Сейчас он показывает их всех только в округе Ратленд.Только XML только <Candidate></Candidate> если голосов> 0

https://jsfiddle.net/jeffd/yqbwaxts/3/

https://99centbeats.com/ResultsData.xml

So Far:

$(document).ready(function() { 
    $.ajax({ 
    type: 'GET', 
    url: 'https://99centbeats.com/ResultsData.xml', 
    //url: 'https://vtelectionresults.sec.state.vt.us/rss/2713/ResultsData.xml', 
    crossDomain: true, 
    success: parseXml 
    }); 
}); 
function parseXml(xml) { 
    $(xml).find('Town').each(function() { 
    var county = $(this).find('County').text(); //change to county for 'County' 
    window.votes = $(this).find('Votes').text(); 
    if (county == 'RUTLAND') { // change to 'RUTLAND' for Rutland County 


     if (votes !== '0'){ 


     window.townname = $(this).find('TownName').text(); 
     window.officename = $(this).find('OfficeName, Name, Votes'); 
     var newItems = $.map(officename, function(i) { 
     $(i).filter('0') 
     return $(i).text(); 
     }); 
     newItems2 = ("" + newItems).replace(/,/g, " - "); 
     $(".marquee").append(' - <strong>' + townname + '</strong>' + ' - ' + newItems2); 
    } 
    } 
    }); 
    $('.marquee').marquee({ 
    duration: 5000 // Speed of the counter (Lower = Faster) 
    }); 
} 
setTimeout(function() { 
window.location.reload(1); 
}, 300000); // Refresh Results Every 5 Minutes 

ответ

1

Похоже, что вы собираетесь на это довольно бессистемно, просто захватывая элементы и нанизывая их вместе. Лучше методично получить офисы в городе, затем их кандидаты, затем отфильтровать этих кандидатов по голосам и т. Д.

Следующие должны работать, хотя кажется, что в настоящее время в одном городе Ратленд есть только один кандидат, у которого есть голоса :

//Must have Allow-Control-Allow-Origin chrome plugin installed to work 
 

 
$(document).ready(function() { 
 
    $.ajax({ 
 
    type: 'GET', 
 
    url: 'https://99centbeats.com/ResultsData.xml', 
 
    //url: 'https://vtelectionresults.sec.state.vt.us/rss/2713/ResultsData.xml', 
 
    crossDomain: true, 
 
    success: parseXml 
 
    }); 
 
}); 
 

 
function textForOffice(office) { 
 
    var candidates = $(office).find('Candidate').filter(function(i, el) { 
 
    return $(el).find('Votes').text() > 0; 
 
    }); 
 

 
    if (candidates.length === 0) { 
 
    return ''; 
 
    } 
 

 
    var officeName = $(office).find('OfficeName').text(); 
 

 
    var candidateValues = candidates.toArray().map(function (el) { 
 
     return $(el).find('Name').text() + ' - ' + $(el).find('Votes').text(); 
 
    }); 
 

 
    return ' - ' + officeName + ' - ' + candidateValues.join(' - '); 
 
} 
 

 
function parseXml(xml) { 
 
    $(xml).find('Town').each(function() { 
 
    var town = $(this); 
 
    var county = town.find('County').text(); //change to county for 'County' 
 

 
    if (county == 'RUTLAND') { // change to 'RUTLAND' for Rutland County 
 
     var townname = town.find('TownName').text(); 
 

 
     var offices = town.find('Office'); 
 

 
     var textForOffices = offices.toArray().map(textForOffice); 
 
     
 
     $(".marquee").append(' - <strong>' + townname + '</strong>' + textForOffices.join('')); 
 
    } 
 
    }); 
 
    $('.marquee').marquee({ 
 
    duration: 5000 // Speed of the counter (Lower = Faster) 
 
    }); 
 
} 
 
setTimeout(function() { 
 
    window.location.reload(1); 
 
}, 300000); // Refresh Results Every 5 Minutes
.rssfeed { 
 
    background-color: black; 
 
    color: white; 
 
    bottom: 0; 
 
    font-family: Gotham, Helvetica Neue, Helvetica, Arial, " sans-serif"; 
 
    height: 150px; 
 
    line-height: 150px; 
 
    font-size: 32px; 
 
} 
 

 
.marquee { 
 
    overflow: hidden; 
 
    width: 100%; 
 
    white-space: nowrap; 
 
} 
 

 
.description { 
 
    width: 100%; 
 
    height: 50px; 
 
    background: red; 
 
    color: white; 
 
    line-height: 50px; 
 
    text-align: center; 
 
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
    font-size: 24px; 
 
}
<div class="description">Rutland City - Vote Count Per Ward</div> 
 
<div class="rssfeed"> 
 
    <div class="marquee"></div> 
 
</div> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script src="https://cdn.jsdelivr.net/jquery.marquee/1.4.0/jquery.marquee.min.js"></script>

+0

Благодаря JKRishe, вы гений! – Jeff

+0

@Jeff Добро пожаловать и благодарю вас за то, что вы положили мне хорошее слово в моей компании. Очень признателен. :) – JLRishe

Смежные вопросы