首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度表排序,不带角度材料

角度表排序,不带角度材料
EN

Stack Overflow用户
提问于 2021-05-17 20:57:38
回答 2查看 45关注 0票数 1

我想在我的表中进行搜索,但是我想让表中首先填满我的数据。目前,只有当我开始在搜索栏中输入时,它才会被填满。

我认为问题来自于初始化组件时productsArray是空的

我的数据来自项目中的JSON文件。

下面你可以看到我目前所拥有的

代码语言:javascript
复制
export class TableComponent implements OnInit {
      searchText: any;
      productsArray: Products[] = [];
      filteredArray = [...this.productsArray];

      constructor(private http: HttpClient) {}

      ngOnInit(): void {
        this.getProducts().subscribe((res) => {
          this.productsArray = res;
        });
      }
      getProducts(): Observable < Products[] > {
        return this.http
          .get < any > ('assets/mock.data.json')
          .pipe(map((results) => results.data));
      }
      filterArray(): any {
        if (!this.productsArray.length) {
          this.filteredArray = [];
          return;
        }

        if (!this.searchText) {
          this.filteredArray = [...this.productsArray];
          return;
        }

        const products = [...this.productsArray];
        const properties = Object.keys(products[0]);

        this.filteredArray = products.filter((product) => {
          return properties.find((property) => {
              const valueString = product[property].toString().toLowerCase();
              return valueString.includes(this.searchText.toLowerCase());
            }) ?
            product :
            null;
        });
      }
    }
代码语言:javascript
复制
                        Code                       Name                       Category             
代码语言:javascript
复制
<tbody *ngFor="let product of filteredArray; let i = index">
  <tr>
    <td>{{ product.code }}</td>
    <td>{{ product.name }}</td>
    <td>{{ product.category }}</td>
  </tr>
</tbody>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-05-17 21:02:48

初始化filteredArray时,它是空的。只有在调用filterArray()时,filteredArray才会填充一些值。因此在ngOnInit()中提供filteredArray的值。试试这个:

代码语言:javascript
复制
ngOnInit(): void {
    this.getProducts().subscribe((res) => {
      this.productsArray = res;
      this.filteredArray = [...this.productsArray];
    });
  }
票数 2
EN

Stack Overflow用户

发布于 2021-05-17 21:11:04

你需要使用管道,就像这样

代码语言:javascript
复制
@Pipe({
    name: 'filterPipe'
})
export class FilterPiper implements PipeTransform {
    transform(array: any[], properties: any[], filter: string) {
        if (!array) {
            return [];
        }
        if (!properties.length || !filter) {
            return array;
        }

        filter = filter.trim().toLowerCase();
        return array.filter((element) => 
      !!(properties.find((property) => {
          const valueString = product[property].toString().toLowerCase();
          return valueString.includes(filter)
        )) 
    });
}

和在html中

代码语言:javascript
复制
 <tbody *ngFor="let product of productsArray | filterPipe: propertiesArray : searchText ; let i = index">
  <tr>
    <td>{{ product.code }}</td>
    <td>{{ product.name }}</td>
    <td>{{ product.category }}</td>
  </tr>
</tbody>

请记住,管道需要在模块中声明并导入到组件中

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67570149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档