在我的Angular项目中需要筛选更多项目。

huangapple go评论68阅读模式
英文:

More items to be filtered in my angular project

问题

I'm creating a test project and I put a filter where I can filter the last name from my list of users.
But I wanted to filter not only the last name but also the name, email and username.
I tried to think of how to make these changes in my code but I couldn't so far. I wanted to know if you could help me.

My HTML:

<input type="text" [(ngModel)]="lastname" (input)="Search()"/>
<mat-table [dataSource]="usuarios">
    <!-- Name Column -->
    <ng-container matColumnDef="firstName">
        <mat-header-cell *matHeaderCellDef> Nome </mat-header-cell>
        <mat-cell *matCellDef="let element">{{ element.firstName }}</mat-cell>
    </ng-container>
    <!-- Last Name Column -->
    <ng-container matColumnDef="lastName">
        <mat-header-cell *matHeaderCellDef>Sobrenome</mat-header-cell>
        <mat-cell *matCellDef="let element">{{ element.lastName }}</mat-cell>
    </ng-container>
    <!-- UserName Column -->
    <ng-container matColumnDef="username">
        <mat-header-cell *matHeaderCellDef>Username</mat-header-cell>
        <mat-cell *matCellDef="let element">{{ element.username }}</mat-cell>
    </ng-container>
    <!-- Email Column -->
    <ng-container matColumnDef="email">
        <mat-header-cell *matHeaderCellDef>E-mail</mat-header-cell>
        <mat-cell *matCellDef="let element">{{ element.email }}</mat-cell>
    </ng-container>
</mat-table>

My component.ts:

export class GerenciamentoUsuariosListaComponent implements OnInit {
    @Input() usuarios: any[] = [];
    usuarios1: any;
    lastname: string;

    // ...

    readonly displayedColumns = ['firstName', 'lastName', 'username', 'email', 'actions'];

    constructor(private service: GerenciamentoUsuariosService) {
        this.lastname = '';
    }

    // ...

    ngOnInit(): void {
        this.refreshListUser1();
    }

    refreshListUser1() {
        this.usuarios1 = this.service.list().subscribe(
            resp => {
                this.usuarios = resp.content;
                console.log(this.usuarios);
            }
        );
    }

    Search() {
        if (this.lastname !== '') {
            this.usuarios = this.usuarios.filter(res => {
                const fullName = res.firstName + ' ' + res.lastName + ' ' + res.username + ' ' + res.email;
                return fullName.toLocaleLowerCase().includes(this.lastname.toLocaleLowerCase());
            });
        } else {
            this.ngOnInit();
        }
    }
}

If I missed more information and you need it, please let me know.

英文:

I'm creating a test project and I put a filter where I can filter the last name from my list of users.
But I wanted to filter not only the last name but also the name, email and username.
I tried to think of how to make these changes in my code but I couldn't so far. I wanted to know if you could help me.

My HTML:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;input type=&quot;text&quot; [(ngModel)]=&quot;lastname&quot; (input)=&quot;Search()&quot;/&gt;
&lt;mat-table [dataSource]=&quot;usuarios&quot;&gt;
    &lt;!-- Name Column --&gt;
    &lt;ng-container matColumnDef=&quot;firstName&quot;&gt;
      &lt;mat-header-cell *matHeaderCellDef&gt; Nome &lt;/mat-header-cell&gt;
      &lt;mat-cell *matCellDef=&quot;let element&quot;&gt;{{ element.firstName }}&lt;/mat-cell&gt;
    &lt;/ng-container&gt;
    &lt;!-- Last Name Column --&gt;
    &lt;ng-container matColumnDef=&quot;lastName&quot;&gt;
      &lt;mat-header-cell *matHeaderCellDef&gt;Sobrenome&lt;/mat-header-cell&gt;
      &lt;mat-cell *matCellDef=&quot;let element&quot;&gt;{{ element.lastName }}&lt;/mat-cell&gt;
    &lt;/ng-container&gt;
    &lt;!-- UserName Column --&gt;
    &lt;ng-container matColumnDef=&quot;username&quot;&gt;
        &lt;mat-header-cell *matHeaderCellDef&gt;Username&lt;/mat-header-cell&gt;
        &lt;mat-cell *matCellDef=&quot;let element&quot;&gt;{{ element.username }}&lt;/mat-cell&gt;
      &lt;/ng-container&gt;
      &lt;!-- Email Column --&gt;
      &lt;ng-container matColumnDef=&quot;email&quot;&gt;
        &lt;mat-header-cell *matHeaderCellDef&gt;E-mail&lt;/mat-header-cell&gt;
        &lt;mat-cell *matCellDef=&quot;let element&quot;&gt;{{ element.email }}&lt;/mat-cell&gt;
      &lt;/ng-container&gt; ....      

<!-- end snippet -->

My component.ts:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

export class GerenciamentoUsuariosListaComponent implements OnInit {
  @Input() usuarios: any[] = [];
  usuarios1: any;
  lastname :string;

 ..

  readonly displayedColumns = [&#39;firstName&#39;, &#39;lastName&#39;, &#39;username&#39;,&#39;email&#39;,&#39;actions&#39;];

  constructor(private service: GerenciamentoUsuariosService) {
    this.lastname = &#39;&#39;;

  }


..

  ngOnInit(): void {
    this.refreshListUser1()
  }


  refreshListUser1() {
    this.usuarios1 = this.service.list().subscribe(
      resp =&gt; {
        this.usuarios = resp.content
       console.log(this.usuarios)

      });

  }


  Search(){
    if(this.lastname != &quot;&quot;){
      this.usuarios = this.usuarios.filter(res =&gt;{
        return res.lastName.toLocaleLowerCase().match(this.lastname.toLocaleLowerCase());
      })
    }else if(this.lastname == &quot;&quot;){
      this.ngOnInit();
    }

  }
}

<!-- end snippet -->

If I missed more information and you need it, please let me know.

答案1

得分: 1

你可以根据你想要的条件进行筛选

this.usuarios = this.usuarios.filter(res => {
    const keyword = this.lastname.toLocaleLowerCase();
    return res.firstName.toLocaleLowerCase().includes(keyword) || 
           res.lastName.toLocaleLowerCase().includes(keyword) ||
           res.username.toLocaleLowerCase().includes(keyword) ||
           res.email.toLocaleLowerCase().includes(keyword);
});

这是用于部分搜索的代码,如果你想进行完全匹配搜索,那么你需要进行相等性检查。

希望对你有帮助。

英文:

You can filter out based on as many condition you want

this.usuarios = this.usuarios.filter(res =&gt;{
    const keyword = this.lastname.toLocaleLowerCase();
    return res.firstName.toLocaleLowerCase().includes(keyword) || 
           res.lastName.toLocaleLowerCase().includes(keyword) ||
           res.username.toLocaleLowerCase().includes(keyword) ||
           res.email.toLocaleLowerCase().includes(keyword);
    
});

That is for the partial search and if you want full search then you need to do the equality check.

Hope it helps you

huangapple
  • 本文由 发表于 2023年6月18日 23:19:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定