I want to filter by year clicking in a checkbox, example, each checkbox has a year assigned to the value, but need to filter them when i click in each one, i am using a paginator of 10 elements in Drupal 9 Views JSON API.
the json response will be something like this:
http://example.com/jsonapi/views/busqueda_avanzada/informes?page=0&views-filter%5Bano%5D%5B0%5D=2020&views-filter%5Bano%5D%5B1%5D=2019&views-filter%5Bano%5D%5B2%5D=2002
that is too long, without saying that i have to use multiple for statements for that to assign the index everytime anybody click on that…
This is the HTML
<li><span><strong>Año</strong></span></li> <li> <input type="checkbox" value="2000" class="form-check-input" [checked]="false" #ano2000 (change)="getYear(ano2000.value)"> 2000 <input type="checkbox" value="2001" class="form-check-input" [checked]="false" #ano2001 (change)="getYear(ano2001.value)"> 2001 <input type="checkbox" value="2002" class="form-check-input" [checked]="false" #ano2002 (change)="getYear(ano2002.value)"> 2002 <input type="checkbox" value="2003" class="form-check-input" [checked]="false" #ano2003 (change)="getYear(ano2003.value)"> 2003 </li> <li> <input type="checkbox" value="2004" class="form-check-input" [checked]="false" #ano2004 (change)="getYear(ano2004.value)"> 2004 <input type="checkbox" value="2005" class="form-check-input" [checked]="false" #ano2005 (change)="getYear(ano2005.value)"> 2005 <input type="checkbox" value="2006" class="form-check-input" [checked]="false" #ano2006 (change)="getYear(ano2006.value)"> 2006 <input type="checkbox" value="2007" class="form-check-input" [checked]="false" #ano2007 (change)="getYear(ano2007.value)"> 2007 </li> <li> <input type="checkbox" value="2008" class="form-check-input" [checked]="false" #ano2008 (change)="getYear(ano2008.value)"> 2008 <input type="checkbox" value="2009" class="form-check-input" [checked]="false" #ano2009 (change)="getYear(ano2009.value)"> 2009 <input type="checkbox" value="2010" class="form-check-input" [checked]="false" #ano2010 (change)="getYear(ano2010.value)"> 2010 <input type="checkbox" value="2011" class="form-check-input" [checked]="false" #ano2011 (change)="getYear(ano2011.value)"> 2011 </li> <li> <input type="checkbox" value="2012" class="form-check-input" [checked]="false" #ano2012 (change)="getYear(ano2012.value)"> 2012 <input type="checkbox" value="2013" class="form-check-input" [checked]="false" #ano2013 (change)="getYear(ano2013.value)"> 2013 <input type="checkbox" value="2014" class="form-check-input" [checked]="false" #ano2014 (change)="getYear(ano2014.value)"> 2014 <input type="checkbox" value="2015" class="form-check-input" [checked]="false" #ano2015 (change)="getYear(ano2015.value)"> 2015 </li> <li> <input type="checkbox" value="2016" class="form-check-input" [checked]="false" #ano2016 (change)="getYear(ano2016.value)"> 2016 <input type="checkbox" value="2017" class="form-check-input" [checked]="false" #ano2017 (change)="getYear(ano2017.value)"> 2017 <input type="checkbox" value="2018" class="form-check-input" [checked]="false" #ano2018 (change)="getYear(ano2018.value)"> 2018 <input type="checkbox" value="2019" class="form-check-input" [checked]="false" #ano2019 (change)="getYear(ano2019.value)"> 2019 </li> <li class="margin_24"> <input type="checkbox" value="2020" class="form-check-input" [checked]="false" #ano2020 (change)="getYear(ano2020.value)"> 2020 <input type="checkbox" value="2021" class="form-check-input" [checked]="false" #ano2021 (change)="getYear(ano2021.value)"> 2021 </li> <li>
My service
get informesNodes() { return 'http://example.com/jsonapi/views/busqueda_avanzada/informes?page=' + this.pageNumber + '&views-filter[titulo]=' + this.titulo + '&views-filter[field_organismo_auditado_target_id]=' + this.organismoId + '&views-filter[field_tipo_de_auditoria_target_id]=' + this.auditoriaId + '&views-filter[ano]=' + this.year; } constructor(private http: HttpClient) { } getNodes(): Observable<InformesCounter> { this.pageNumber = 0; return this.http.get<InformesCounter>(`${this.informesNodes}`); } getYear(year: any): Observable<Year> { this.year = year; return this.http.get<Year>(`${this.informesNodes}`); } resetYear(year: any): Observable<Year> { this.year = 'All'; return this.http.get<Year>(`${this.informesNodes}`); }
And my component
getYear(yearValue: any){ this.buscadorService.getYear(yearValue) .subscribe((data: Year) => { this.informesNode = data.data; this.buscadorService.year = yearValue; const attributes = this.informesNode.map((data: { attributes: any;}) => data.attributes); attributes.filter((element: { ano: any; }) => element.ano === this.buscadorService.year); this.buscadorService.pageNumber = 0; this.nodeList = attributes; console.log(this.nodeList); }); }
This is the best information i can provide, but feel free to ask if you need something so you can help me with this. Thanks in advance