我想在表格完全加载后显示弹出窗口。我使用的是load属性,但它不起作用。有没有其他方法来实现这一目标?没有亲子概念。
以下是我的代码component.ts
export class FileUploadComponent {
public files: UploadFile[] = [];
data: AOA = [ [1, 2], [3, 4] ];
wopts: XLSX.WritingOptions = { bookType: 'xlsx', type: 'array' };
fileName: string = 'SheetJS.xlsx';
showData: boolean = false;
public dropped(event: UploadEvent) {
this.files = event.files;
for (const droppedFile of event.files) {
if (droppedFile.fileEntry.isFile) {
const reader: FileReader = new FileReader();
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
fileEntry.file((file: File) => {
var filePath = file;
reader.onload = (e: any) => {
const bstr: string = e.target.result;
const wb: XLSX.WorkBook = XLSX.read(bstr, {type: 'binary'});
const wsname: string = wb.SheetNames[0];
const ws: XLSX.WorkSheet = wb.Sheets[wsname];
this.data = <AOA>(XLSX.utils.sheet_to_json(ws, {header: 1}));
};
reader.readAsBinaryString(file);
this.showData = true;
this.infoModal.hide();
});
} else {
this.showData = false;
const fileEntry = droppedFile.fileEntry as FileSystemDirectoryEntry;
}
}
}
showPopUp(){
console.log('yes');
}
}
以下是我的component.htm
<div class="modal-body">
<file-drop id="infoFileModal" headertext="Drop files here" (onFileDrop)="dropped($event)" (onFileOver)="fileOver($event)" (onFileLeave)="fileLeave($event)"></file-drop>
</div>
<div class="card-body">
<div *ngIf="showData" class="upload-table table-responsive">
<table #table class="table-bordered table-striped" id="dest_table" (load)="showPopup()">
<tr *ngFor="let row of data">
<td *ngFor="let val of row">
{{val}}
</td>
</tr>
</table>
</div>
<div *ngIf="!showData" class="div-upload">
No Data Found
</div>
</div>
答案 0 :(得分:1)
原生表没有任何load
事件。应将此逻辑委派给加载数据的服务。
或者,正如评论中所建议的那样,您可以使用ngAfterViewInit()
。
但是:如果表首先被渲染,并且你加载数据,那么它将无法正常工作
答案 1 :(得分:1)
好的,这是另一种方法。
由于您的表需要几分钟才能进行渲染,因此在填充数据数组之后,您唯一的机会就是只要需要就收听更改事件。为了防止showPopUp() - 方法在每次完成的迭代中被触发,你使用Observable.debounceTime(),它只在最后一次change-event之后的时间超过给定时间(以毫秒为单位)时调用方法。
组件
import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Subscription} from "rxjs/Subscription";
@ViewChild('table') table: any;
private timer= Observable.timer(1000); // wait one second before calling the method
private subscription: Subscription;
ngOnDestroy(): void {
if(this.subscription){
this.subscription.unsubscribe();
}
}
public dropped(event: UploadEvent) {
// ...
reader.readAsBinaryString(file);
this.showData = true;
this.infoModal.hide();
// call the watcher with a delay of one second
// in order to give angular enough time to build up the table
this.subscription = this.timer.subscribe( () => {
this.triggerTableWatcher();
});
// ...
}
triggerTableWatcher() {
// define the object to listen to
const trigger = this.table.changes();
// define the debounce-time (adjust it, if it doesn't fit your needs)
const debounceAt = trigger.debounceTime(1000);
// subscribe to the trigger and tell which method to call eventually
debounceAt.subscribe(() => this.showPopUp());
}
HTML模板(仅限其中的重要部分)
<div class="card-body">
<div *ngIf="showData" class="upload-table table-responsive">
<table #table class="table-bordered table-striped" id="dest_table">
<tr *ngFor="let row of data">
<td *ngFor="let val of row">
{{val}}
</td>
</tr>
</table>
</div>
答案 2 :(得分:0)
您可以订阅文件阅读器的onloadend()方法。请查看代码的注释部分。
public dropped(event: UploadEvent) {
this.files = event.files;
for (const droppedFile of event.files) {
if (droppedFile.fileEntry.isFile) {
const reader: FileReader = new FileReader();
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
fileEntry.file((file: File) => {
var filePath = file;
reader.onload = (e: any) => {
const bstr: string = e.target.result;
const wb: XLSX.WorkBook = XLSX.read(bstr, {type: 'binary'});
const wsname: string = wb.SheetNames[0];
const ws: XLSX.WorkSheet = wb.Sheets[wsname];
this.data = <AOA>(XLSX.utils.sheet_to_json(ws, {header: 1}));
};
reader.readAsBinaryString(file);
// subscribe to the onloadend()-method here
reader.onloadend = (e) => {
this.showPopUp();
};
this.showData = true;
this.infoModal.hide();
});
} else {
this.showData = false;
const fileEntry = droppedFile.fileEntry as FileSystemDirectoryEntry;
}
}
}
showPopUp(){
console.log('yes');
}