【问题标题】:Give custom file name to p-table export为 p-table 导出提供自定义文件名
【发布时间】:2020-03-30 22:33:12
【问题描述】:

我在我的角度应用程序中使用primeng 表。我正在尝试在用户导出表格内容时将导出的 csv 文件附加到当前时间。但是,我第一次导出时,文件名没有随着时间而更新。下次它可以工作,但文件名会附加上一次。我的猜测是导出文件名使用的是旧文件名而不是更新文件名。不知道为什么会这样。我在导出时调用了 setFile 方法。它应该采用更新的文件名。请帮助。在此先感谢。

这是我的模板代码:

<p-table [columns]="cols" #dt [value]="students" [autoLayout]="true"  exportFilename={{testFileName}}>
        <ng-template pTemplate="caption">
            <div >
                <button  mat-icon-button type="button" (click)="setFileName();dt.exportCSV()" style="float:right"><mat-icon>save_alt</mat-icon></button>
            </div>
        </ng-template>
    </p-table>

我的打字稿:

  students: any[];
  cols: any[];

  testFileName = 'SampleFile';
  constructor(private studentService: StudentService) { }

  ngOnInit() {
    this.studentService.getAllStudents().subscribe(data => this.students = data);

    this.cols = [
      { field: 'name', header: 'Name' },
      { field: 'rollNo', header: 'RollNo' },
      { field: 'class', header: 'Class' },
    ];
  }



setFileName() {
    this.testFileName = 'StudentDetailsExport' + '_' +
      new DatePipe('en-US').transform(Date.now(), 'MMM_dd_yyyy_hh_mm_ss', 'UTC') + 'Z';

  }

【问题讨论】:

    标签: angular primeng


    【解决方案1】:

    获取元素引用

    @ViewChild('dt') dt: Table;
    

    并使用 this.dt.exportFileName 设置文件名,然后使用 dt.exportCSV() 导出。

    setFileName() {
        this.dt.exportFileName = 'StudentDetailsExport' + '_' +
          new DatePipe('en-US').transform(Date.now(), 'MMM_dd_yyyy_hh_mm_ss', 'UTC') + 'Z';
       this.dt.exportCSV();
      }
    

    HTML - 只调用 setFileName() 函数

    <button  mat-icon-button type="button" (click)="setFileName()"></button>
    

    Stackblitz:https://stackblitz.com/edit/angular-ptable-io6vjb

    【讨论】:

    • 呃它的 this.dt.nativeElement.exportCSV() ..我正在创建 stackblitz
    • 您的建议与我通过调用两种方法所做的相同。对于您建议的而不是 ElementRef,我必须使用 Table 然后调用 this.dt.exportCsv()。但是问题仍然存在
    • 那么您是否使用 Table your method 对其进行了测试??
    • 是的,我用表格测试了你的方法,同样的问题
    • 好的,如果您能够在 setFileName() 中获取表格,然后使用 table.exportFileName='StudentDetailsExport' + '_' + new DatePipe('en-US').transform(Date.now( ), 'MMM_dd_yyyy_hh_mm_ss', 'UTC') + 'Z';在执行 exportCsv(); 之前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2020-06-22
    • 2020-12-28
    • 2011-04-14
    • 2011-08-18
    • 1970-01-01
    • 2014-09-22
    相关资源
    最近更新 更多