Data table integrated with Angular not showing export buttons such as Excel, PDF

我与影子孤独终老i 提交于 2019-12-24 17:15:56

问题


"styles": [
    "../node_modules/bootstrap/dist/css/bootstrap.min.css",
    "../node_modules/datatables.net-dt/css/jquery.dataTables.css",
    "../node_modules/datatables.net-buttons-dt/css/buttons.dataTables.css",
    "styles.css"
  ],
  "scripts": [
    "../node_modules/jquery/dist/jquery.min.js",
    "../node_modules/popper.js/dist/umd/popper.min.js",
    "../node_modules/bootstrap/dist/js/bootstrap.min.js",
    "../node_modules/jquery/dist/jquery.js",
    "../node_modules/datatables.net/js/jquery.dataTables.js",
    "../node_modules/datatables.net-buttons/js/dataTables.buttons.js",
    "../node_modules/datatables.net-buttons/js/buttons.colVis.js",
    "../node_modules/datatables.net-buttons/js/buttons.flash.js",
    "../node_modules/datatables.net-buttons/js/buttons.html5.js",
    "../node_modules/datatables.net-buttons/js/buttons.print.js",   
 ],

This is the angular-cli.json I have created file for my project.

Component for the project looks like this;

ngOnInit() {
this.dtOptions = {
  pagingType: 'full_numbers',
  pageLength: 10,
  dom: 'Bfrtip',
  buttons: [
    'copy', 'print', 'csv','columnsToggle','colvis','pdf','excel']    
  };
  this.loadapi();
}

Here I have created a datatable from a sample json. Every thing was fine except the button to export excel and pdf didn't show other buttons are showing on UI. What may be the issue?


回答1:


Please Follow steps:

1: Install its dependencies:

npm install jszip --save
npm install datatables.net-buttons --save
npm install datatables.net-buttons-dt --save

2:Add the dependencies in the scripts and styles attributes:

    {
  "projects": {
    "your-app-name": {
      "architect": {
        "build": {
          "options": {
            "styles": [
              ...
              "node_modules/datatables.net-buttons-dt/css/buttons.dataTables.css"
            ],
            "scripts": [
              ...
              "node_modules/jszip/dist/jszip.js",
              "node_modules/datatables.net-buttons/js/dataTables.buttons.js",
              "node_modules/datatables.net-buttons/js/buttons.colVis.js",
              "node_modules/datatables.net-buttons/js/buttons.flash.js",
              "node_modules/datatables.net-buttons/js/buttons.html5.js",
              "node_modules/datatables.net-buttons/js/buttons.print.js"
            ],
            ...
}

If you want to have the excel export functionnality, then you must import the jszip.js before the buttons.html5.js file.

3:HTML:

<table datatable [dtOptions]="dtOptions" class="row-border hover"></table>

4:DtOptions in Typescript:

        this.dtOptions = {
      dom: 'Bfrtip',
      // Configure the buttons
      buttons: [
        'copy',
        'print',
        'csv',
        'excel',
        'pdf'
      ]
    };

For more help refer this link.



来源:https://stackoverflow.com/questions/49322350/data-table-integrated-with-angular-not-showing-export-buttons-such-as-excel-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!