javascript - 显示微调器直到数据反射(reflect)在 DOM-angular 中

标签 javascript html angular filter

我在表中显示了庞大的数据集(5000 行)。 我正在尝试通过列过滤器来过滤数据,当应用相同的过滤器时,我保留了一个微调器以显示过滤操作正在进行中

但是由于在 javascript 中 Action 如此之快,我用作标志以检查数据是否被过滤/未被过滤的 bool 变量很快就会改变

然而,将新数据反射(reflect)到 DOM 中大约需要 2 秒。我想在此 DOM 绑定(bind)时间内显示微调器。

HTML:

 <div *ngIf="isFilterClicked" class="col-md-12" style="text-align: center;color: #165cb9;height: 440px;padding-top:100px">
                <px-spinner size="100">
                </px-spinner>
            </div>
        <div *ngFor="let product of products"> 
        </div>

Javascript

this.initialProducts=this.products; //initialize 

filterTable(filterData) {
    this.isFilterClicked = true;
    const result = this.initialProducts.filter(function (o1) {
        return o1.id === filterData.id; // return the ones with equal id
    });
});             
this.products= result ;
this.isFilterClicked = false;
}

最佳答案

当 ngFor 完成时,你将 bool 变量设置为 flase

<div *ngIf="isFilterClicked" class="col-md-12" style="text-align: center;color: #165cb9;height: 440px;padding-top:100px">
  Loading data....
</div>
<button (click)="filterEvenProducts()">Filter Even</button>
<button (click)="filterOddProducts()">Filter Odd</button>
<div #allProducts *ngFor="let product of products;">
  {{product}}
</div>

组件

export class ProductComponent implements AfterViewInit {

initialProducts = [];
products = [];
isFilterClicked = false;

@ViewChildren('allProducts') things: QueryList<any>;

constructor() {
  for (let i = 0; i < 5000; i++) {
      this.initialProducts.push(`Product ${i}`);
      this.products = this.initialProducts;
    }
  }

filterEvenProducts() {
    this.isFilterClicked = true;
    this.products = this.initialProducts.filter((product, index) => (index % 2) === 0);
  }

  filterOddProducts() {
    this.isFilterClicked = true;
    this.products = this.initialProducts.filter((product, index) => (index % 2) !== 0);
  }

  ngAfterViewInit() {
    this.things.changes.subscribe(t => {
      // to avoid error ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked
      setTimeout(() => {
        this.isFilterClicked = false;
      }, 500);
    });
  }

}

关于javascript - 显示微调器直到数据反射(reflect)在 DOM-angular 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52182914/

相关文章:

JavaScript - 防止页面缩放

node.js - NPM 安装错误 :Unexpected end of JSON input while parsing near '...nt-webpack-plugin" :"0'

javascript - 错误 : Uncaught (in promise): overlay does not exist (ionic-selectable)

javascript - 我应该如何在这里使用 shouldComponentUpdate 来更新 React.js 中的父状态?

javascript - Hapi.js 实验室测试结果不一致

javascript - 创建二维 View

html - 使用 HTML 字符串生成默认浏览器

angular - 如何在 Angular 6 的 Material 日期选择器中编辑日期格式?

javascript - 从本地文件到 Angular 5 中的 Typescript 数组的 JSON 数组

asp.net - HTML/ASP.NET : &lt;input type =“hidden” name =“reference” value =“ABC”/>