Ich versuche, einen 'Project Index' durch Tags mit jQuery und CSS zu filtern.Filterliste Ergebnisse mit <select/> Dropdown-Liste Wert
ich zur Zeit unter den .val()
der ausgewählten Option in einer <select>
Dropdown-Liste und prüfen, ob es an die inneren .html()
ein .tag
gleich ist. Wenn sie übereinstimmen, wird der Projekteintrag schwarz, wenn er sonst hellgrau wird.
Mein aktueller Code funktioniert, wenn das Projekt ein Tag hat, aber wenn es mehrere Tags gibt, bricht es. Meine Vermutung ist, dass, obwohl es eine gleich dem ausgewählten Dropdown-Wert gibt, die anderen sind nicht, deshalb mit der else
Bedingung?
Was fehlt mir? Gibt es einen besseren/effizienteren Weg dies zu tun?
HTML
<main>
<div class="index__container">
<section class="index">
<ul class="index__header">
<li>Project</li>
<li>Filter: <select id="index__filter">
<option selected="selected" value='ALL'>All</option>
<option value="Pedagogical Explorations">Pedagogical Explorations</option>
<option value="Research Production">Research Production</option>
<option value="Spatial Practice">Spatial Practice</option>
<option value="Exhibition">Exhibition</option>
</select></li>
<li>Year</li>
</ul>
<a href="">
<ul class="index__entry">
<li>Multiscale Strategies to Reactivate Transhumance in Spain</li>
<li>
<span class="tag">Spatial Practice</span>
</li>
<li>2017</li>
</ul>
</a>
<a href="">
<ul class="index__entry">
<li>Architecture after Speculation</li>
<li>
<span class="tag">Research Production</span>
</li>
<li>2017</li>
</ul>
</a>
<a href="">
<ul class="index__entry">
<li>Polyester Merino Chair</li>
<li>
<span class="tag">Spatial Practice</span>
</li>
<li>2017</li>
</ul>
</a>
<a href="">
<ul class="index__entry">
<li>1500 caracteres</li>
<li>
<span class="tag">Spatial Practice</span>
<span class="tag">Research Production</span>
</li>
<li>2017</li>
</ul>
</a>
<a href="">
<ul class="index__entry">
<li>Europan 13: Die Arbeitersiedlung</li>
<li>
<span class="tag">Spatial Practice</span>
</li>
<li>2015</li>
</ul>
</a>
<a href="">
<ul class="index__entry">
<li>Asymmetric Meta-Mapping</li>
<li>
<span class="tag">Pedagogical Explorations</span>
</li>
<li>2017</li>
</ul>
</a>
<a href="">
<ul class="index__entry">
<li>Chicago Architecture Biennial</li>
<li>
<span class="tag">Spatial Practice</span>
<span class="tag">Research Production</span>
<span class="tag">Exhibition</span></li>
<li>2015</li>
</ul>
</a>
</section>
</div>
JS
// Filter Project Index
var $indexSelect = $('#index__filter');
var $indexEntry = $('.index__entry');
var $tag = $('.tag');
$indexSelect.change(function(){
var selectedValue = $(this).val();
if(selectedValue == 'ALL'){
$indexEntry.css('color', 'black');
return;
}
$tag.each(function(i,option){
if ($(this).html() == selectedValue) {
$(this).closest('ul').css('color', 'black');
console.log($(this).text());
} else {
$(this).closest('ul').css('color', 'lightgray');
}
});
});
Es funktioniert, wie Sie es umgesetzt haben. Entsprechend Ihrer Bedingung macht es das zusammenpassende Tag * schwarz * und unübertroffene als * grau *. Wenn Sie ein anderes Ergebnis wünschen, erläutern Sie diese Anforderung bitte im Detail. – Sinha
jedes Beispiel, wenn es mehrere Tags gibt und es bricht? –