[[oktatas:web:angular:angular_filter|< Angular]]
====== Angular JavaScript ======
* **Szerző:** Sallai András
* Copyright (c) Sallai András, 2023
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]]
* Web: https://szit.hu
===== Projekt =====
ng new app01
cd app01
ng generate component comp1
code .
ng serve -o
===== Egy keresőmező =====
==== Az app.component.html ====
Dolgozók
==== A comp1 komponens ====
Az |
Név |
Település |
{{emp.id}} |
{{emp.name}} |
{{emp.city}} |
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-comp1',
templateUrl: './comp1.component.html',
styleUrls: ['./comp1.component.scss']
})
export class Comp1Component implements OnInit {
employees: any;
employeeList: any;
constructor() { }
ngOnInit(): void {
this.employeeList = [
{ "id": 1, "name": "Csendes Irén", "city": "Szeged" },
{ "id": 2, "name": "Csendes Borbála", "city": "Pécs" },
{ "id": 3, "name": "Felhős Béla", "city": "Szeged" },
{ "id": 4, "name": "Lind Evelin", "city": "Pécs" },
{ "id": 5, "name": "Csendes Lajos", "city": "Szegi" },
{ "id": 6, "name": "Csendes Ágnes", "city": "Szegi" },
{ "id": 7, "name": "Arany Tamás", "city": "Szegi" },
{ "id": 8, "name": "Rendes Valér", "city": "Szerencs" },
{ "id": 9, "name": "Éles Gábor", "city": "Szerencs" },
{ "id": 10, "name": "Remek Imre", "city": "Szegi" },
{ "id": 11, "name": "Vilka Lajos", "city": "Szegi" },
]
this.employees = this.employeeList;
}
changeFilter(value: string) {
console.log(value)
let a = this.employeeList.filter( (emp:any) => {
if(emp.city.indexOf(value)>-1) {
return emp;
}
})
this.employees = a;
}
}
===== Két keresőmező =====
Az |
Név |
Település |
{{emp.id}} |
{{emp.name}} |
{{emp.city}} |
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-comp1',
templateUrl: './comp1.component.html',
styleUrls: ['./comp1.component.scss']
})
export class Comp1Component implements OnInit {
employees: any;
employeeList: any;
nameFilter: string = '';
cityFilter: string = '';
constructor() { }
ngOnInit(): void {
this.employeeList = [
{ "id": 1, "name": "Csendes Irén", "city": "Szeged" },
{ "id": 2, "name": "Csendes Borbála", "city": "Pécs" },
{ "id": 3, "name": "Felhős Béla", "city": "Szeged" },
{ "id": 4, "name": "Lind Evelin", "city": "Pécs" },
{ "id": 5, "name": "Csendes Lajos", "city": "Szegi" },
{ "id": 6, "name": "Csendes Ágnes", "city": "Szegi" },
{ "id": 7, "name": "Arany Tamás", "city": "Szegi" },
{ "id": 8, "name": "Rendes Valér", "city": "Szerencs" },
{ "id": 9, "name": "Éles Gábor", "city": "Szerencs" },
{ "id": 10, "name": "Remek Imre", "city": "Szegi" },
{ "id": 11, "name": "Vilka Lajos", "city": "Szegi" },
]
this.employees = this.employeeList;
}
changeNameFilter(content: string) {
this.nameFilter = content;
this.changeFilter();
}
changeCityFilter(content: string) {
this.cityFilter = content;
this.changeFilter();
console.log('vmi')
}
changeFilter() {
let a = this.employeeList.filter( (emp:any) => {
if(
emp.name.indexOf(this.nameFilter)>-1 &&
emp.city.indexOf(this.cityFilter)>-1)
{
return emp;
}
})
this.employees = a;
}
}