dswqw66280 2018-06-17 17:04
浏览 61
已采纳

PHP表+ pdfMake + AngularJS

I'm a noobie of PHP and AngularJS.

I have a webpage that communicates to a web serves with PHP - AJAX. It queries a database, and echoes the result (a big table) in an html placeholder.

I want to print the content of that table in a downloadable PDF file when the user pushes a button.

I want to use PDFmake and now it works well for test purpose, but how can I pass that content of my table to AngularJS' app? Maybe should I pass table's id to docDefinition content? In that case I don't know how to do that.

Note: Maybe my approach is uncorrent cause I have to relegate PHP to different tasks and use AngularJS to query the Database, but for now I want to mantain this approach.

Thank You

  • 写回答

1条回答 默认 最新

  • dsmvqp3124 2018-06-18 14:13
    关注

    I suggest you use an angular service (as explained in the docs )

    var bigTableApp = angular.module('bigTable',[])
    
    bigTableApp.factory('BigTableSrv', ['$resource',
    function($resource) {
      return $resource('URL_to_php_backend', {}, {
        query: {
          method: 'GET',
          params: {param1: 'value 1', param2: 'value 2'},
          isArray: true
        }
      });
    }
    ]);
    

    Then, you can use it in a controller to fetch data from the back-end and build a table structure in PDFmake's table format:

    bigTableApp.controller('BigTableController', ['$scope', 'BigTableSrv',
      function BigTableController($scope, BigTableSrv) {
        $scope.bigTable = BigTableSrv.query();
        $scope.pdfMakeTable = {
          // array of column widths, expand as needed
          widths: [10, *, 130],
          body: []
        };
    
        $scope.printTable = function() {
          pdfMakeTable.body = $scope.bigTable.map(el => {
            // process each element of your "big table" to one line of the 
            // pdfMake table, size of return array must match that of the widths array
            return [el.prop1, el.prop2, el.prop3]
          });
    
          // create the pdfMake document
          let docDefinition = {
            content: [ pdfMakeTable ]
          }
    
          // print your pdf
          pdfMake.creatPdf(docDefinition).print()
        }
      }
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!