在jsPdf AutoTable中尝试动态数据时遇到麻烦

时间:2016-07-25 06:36:21

标签: javascript jquery jspdf jspdf-autotable

尝试使用jsPdf AutoTable将动态数据打印到PDF中。但是我没有这样做。我在很多网站上搜索过,但没有人对动态数据说过没看过。所以这里我的问题是,有没有办法将动态数据放入表格行中,如果有人可以澄清我的话。注意:[这里没有使用HTML将数据存储到Pdf中,我直接从js获取数据]。



this.print=function(){
        	{
        		
        		var mainData =this.printData(); // Here am getting Full Json data Here
        		var steps = mainData.steps;   // From that data am Separating what i need
        		var criticality = mainData.criticality;
        		var categories = mainData.categories;
        		var checkup = mainData.checkup;
        		
            // This is For to Take the Steps Data alone
               $scope.getSteps = function(steps) {
                   var data = [];
                   for (var i = steps.length; i-- > 0;) {
                     data.push(steps[i].name+"\n"+"\n");
                   }
                   return data;
                 }
               
                     // Like wise am getting every single object data's 
                  $scope.getNumbersOfSubSteps = function(steps) {
                   var data = 0;
                   for (var i = 0 ; i < steps.length; i++) {
                     for (var j = 0; j<steps[i].steps.length; j++) {
                      }
                     data = j ;
                   }
                   return data;
                 }

                 // this is for Sub Proceeses
                 $scope.getSubProcesses = function(steps) {
                   var data = [];
                   for (var i = 0 ; i < steps.length; i++) {
                      for (var j = 0; j<steps[i].steps.length; j++) {
                          data.push(steps[i].steps[j].name+"\n");                  
                     }
                   }
                   return data;
                 }
                 
                  $scope.getCategories = function(categories) {
                   var data = [];
                   for (var i = categories.length; i-- > 0;) {
                     data.push(categories[i].name+"\n");
                   }
                   return data;
                 } 
                  
                  $scope.getCriticality = function(criticality) {
                   var data = [];
                   for (var i = criticality.length; i-- > 0;) {
                     data.push(criticality[i].name+"\n");
                   }
                   return data;
                 }
         
                 // Pdf Print Function Begins 
                  
                var columns = ["ProcessDescription", "Steps", "#ofSubProcesses", "SubSteps","Category","Criticality","CheckUp"];
                var processDescription =mainData.description;
                var processes= $scope.getSteps(steps);
                var NoOfSubProcess = $scope.getNumbersOfSubSteps(steps);
                var subProcesses = $scope.getSubProcesses(steps);
                console.log('Subprocsses length',subProcesses);
                var categories = $scope.getCategories(categories);
                var criticality = $scope.getCriticality(criticality);
                
                // The Problem Begins here , Am struggling to Get the Separate data's here !
                var rows = [
                                    [processDescription,processes,NoOfSubProcess,subProcesses,categories,criticality]
                            
                             ];
                
                var pdfsize='a1';
                var doc = new jsPDF('p', 'pt',pdfsize);
     
                doc.autoTable(columns, rows, {
                	theme: 'striped', // 'striped', 'grid' or 'plain'
                	styles: {
                	      overflow: 'linebreak',
                	      columnWidth: 'wrap'
                	    },
                	    beforePageContent: function(data) {
                	        doc.text("Process Name :"+mainData.name, 40, 30);
                	    },
                    columnStyles: {
                      1: {columnWidth: 'auto'}
                    }
                  });

                  doc.save(mainData.name+ pdfsize +".pdf");
            }
          	
       	
          
        };
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您需要替换它:

var rows = [
    [processDescription,processes,NoOfSubProcess,subProcesses,categories,criticality]
];

有这样的事情:

var rows = [];
for (var k = 0 ; k < processes.length; k++) {
    rows.push([
        processDescription,
        processes[k],
        NoOfSubProcess,
        subProcesses[k],
        categories[k],
        criticality[k]
    ]);
};

rows参数应该是一个数组数组。如果我理解正确的话,你在那里放的基本上是一个数组数组的数组。