Reputation: 811
So I have a small filter for some publications, and all in all it works fine however, I've encountered a problem which is Child Term filters only work when I've clicked on it's parent before, It's my guess this is a scope issue but I do not know how I can fix it, any help would be very much appreciated.
edit: using Angular filters, I was already just forgot to post the complete snippet
<div class="row">
<div class="onefourth">
<div class="linklist terms">
<ul>
<li ng-repeat="term in terms">
<div ng-class="{parent: term.parent == null, active: $first }" ng-show="term.parent==null">
<input type="radio" name="terms" id="label_{{$index}}" ng-value="{{term.ID}}" ng-model="$parent.search.terms.categoria_pub.ID">
<label for="label_{{$index}}">{{term.name}}</label>
</div>
<ul>
<li ng-repeat="childTerm in terms">
<div ng-if="childTerm.parent.ID==term.ID">
<input type="radio" name="terms" id="label_{{$index}}" ng-value="{{childTerm.ID}}" ng-model="$parent.search.terms.categoria_pub.ID">
<label for="label_{{$index}}">{{childTerm.name}}</label>
</div>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="threefourths">
<div class="row">
<div ng-repeat="file in files | filter:search:strict">
<div class="publication">
<div class="onefourth">
<a ng-href="{{ file.custom_fields.jcf_link[0] }}" target="_blank">
<div class="imgholder">
<img ng-src="{{ file.featured_image.source }}" src="<?php echo get_template_directory_uri(); ?>/img/placeholder_pub.jpg">
</div>
</a>
</div>
<div class="threefourths">
<h4>{{file.title}}</h4>
<div class="content" ng-bind-html="file.content | safeHtml">
</div>
<br>
<a ng-href="{{ file.custom_fields.jcf_link[0] }}" class="title" target="_blank">Descargar Publicación</a>
</div>
</div>
</div>
</div>
</div>
edit: some sample data
[
{
"ID": 19,
"name": "Libros, Monografías y notas técnicas",
"slug": "libros-monografias-notas",
"description": "",
"taxonomy": "categoria_pub",
"parent": {
"ID": 4,
"name": "Documentos ",
"slug": "documentos-",
"description": "",
"taxonomy": "categoria_pub",
"parent": null,
"count": 9,
"link": "http://www.example.com/categoria_pub/documentos-/",
"meta": {
"links": {
"collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
"self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/4"
}
}
},
"count": 5,
"link": "http://www.example.com/categoria_pub/libros-monografias-notas/",
"meta": {
"links": {
"collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
"self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/19"
}
},
"$$hashKey": "object:23"
},
{
"ID": 30,
"name": "Todo",
"slug": "todo",
"description": "",
"taxonomy": "categoria_pub",
"parent": null,
"count": 10,
"link": "http://www.example.com/categoria_pub/todo/",
"meta": {
"links": {
"collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
"self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/30"
}
},
"$$hashKey": "object:24"
},
{
"ID": 4,
"name": "Documentos ",
"slug": "documentos-",
"description": "",
"taxonomy": "categoria_pub",
"parent": null,
"count": 9,
"link": "http://www.example.com/categoria_pub/documentos-/",
"meta": {
"links": {
"collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
"self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/4"
}
},
"$$hashKey": "object:25"
},
{
"ID": 17,
"name": "Breves, Informes de opinión",
"slug": "breves",
"description": "",
"taxonomy": "categoria_pub",
"parent": {
"ID": 4,
"name": "Documentos ",
"slug": "documentos-",
"description": "",
"taxonomy": "categoria_pub",
"parent": null,
"count": 9,
"link": "http://www.example.com/categoria_pub/documentos-/",
"meta": {
"links": {
"collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
"self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/4"
}
}
},
"count": 5,
"link": "http://www.example.com/categoria_pub/breves/",
"meta": {
"links": {
"collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
"self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/17"
}
},
"$$hashKey": "object:26"
},
{
"ID": 20,
"name": "Artículos recomendados",
"slug": "articulos-recomendados",
"description": "",
"taxonomy": "categoria_pub",
"parent": null,
"count": 0,
"link": "http://www.example.com/categoria_pub/articulos-recomendados/",
"meta": {
"links": {
"collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
"self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/20"
}
},
"$$hashKey": "object:27"
},
{
"ID": 15,
"name": "Transcripciones webinars",
"slug": "transcripciones-webinars",
"description": "",
"taxonomy": "categoria_pub",
"parent": {
"ID": 4,
"name": "Documentos ",
"slug": "documentos-",
"description": "",
"taxonomy": "categoria_pub",
"parent": null,
"count": 9,
"link": "http://www.example.com/categoria_pub/documentos-/",
"meta": {
"links": {
"collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
"self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/4"
}
}
},
"count": 0,
"link": "http://www.example.com/categoria_pub/transcripciones-webinars/",
"meta": {
"links": {
"collection": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms",
"self": "http://www.example.com/wp-json/taxonomies/categoria_pub/terms/15"
}
},
"$$hashKey": "object:28"
}
]
edit: jsfiddle sorry it took so long
https://jsfiddle.net/785p43cb/22/
Upvotes: 0
Views: 213
Reputation: 1264
I have forked your fiddle and updated it. Here it is:
https://jsfiddle.net/ignaciovillaverde/5g6weppn/4/
It is working a little bit different. First of all, I deleted de strict
comparator in file in files | filter:search
, you were not using it. I also divided your filters in 2 different ways, one using the text type input and other using the radio buttons. For the radio buttons, I have used ng-show="hasSelectedCategory(file.terms.categoria_pub) || !selectedCategory"
.
Also notice that for ng-model
directive in the radio buttons I needed to use ng-model="$parent.selectedCategory"
and ng-model="$parent.$parent.selectedCategory"
.
I have also included a function for asking if the file has the selected category. Take a look at it.
Hope it helps!
Upvotes: 2
Reputation: 1264
How about using AngularJS filters
?
https://docs.angularjs.org/api/ng/filter/filter
Here is an example of a custom filter:
Please take in count that you should modify the customFilter for solving your needs.
Upvotes: 0