如何使用pdf make渲染复杂对象数组?

时间:2017-02-21 01:12:41

标签: javascript arrays json pdfmake

我正在尝试使用pdfMake将一组复杂对象处理成可读格式。这是阵列:

var areaImpact = [
{  
   "riskID":"f0bf6fa1-0a6b-e6e3-9ec08bd67751",
   "description":"Matt's printing testMatt's printing testMatt's printing test",
   "type":"Safety",
   "consequences":{  
      "items":[  
         "Matt's printing test",
         "Matt's printing again"
      ]
   },
   "safeguards":{  
      "items":[  
         "Matt's printing test",
         "Matt's printing test agin!!!"
      ]
   },
   "actions":{  
      "items":[  
         "Matt's awesome printing test"
      ]
   }
},
{  
   "riskID":"ffd23fa1-0a6b-e6e3-9ec08bd67751",
   "description":"Here's another test",
   "type":"Safety",
   "consequences":{  
      "items":[  
         "Matt's printing test",
         "Matt's printing again"
      ]
   },
   "safeguards":{  
      "items":[  
         "Matt's printing test",
         "Matt's printing test agin!!!"
      ]
   },
   "actions":{  
      "items":[  
         "Matt's awesome printing test"
      ]
   }
}
]

我希望pdf看起来像这样: Risk List Concept。  我很难想到这一点。我该怎么做?在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

经过一夜安眠之后,我能够把这一点弄清楚。可能不是唯一的方法,也不是最好的方式,但这就是我的所作所为:



function formatRiskList(riskList){
		var printableRisks = [];

		riskList.forEach(function(risk){

			printableRisks.push({text:'Description', style:'subheader'});
			printableRisks.push({text:risk.description});
			printableRisks.push({text:'Consequences', style:'subheader'});
			printableRisks.push({ul: risk.consequences.items});
			printableRisks.push({text:'Safegaurds', style:'subheader'});
			printableRisks.push({ul: risk.safeguards.items});			
			printableRisks.push({text:'Actions', style:'subheader'});
			printableRisks.push({ul: risk.actions.items});

		});
	     
	  return printableRisks;   

	}
  
  //And then, in the docdefinition, I did this:
  var dd = {content: [formatRiskList(areaImpact.riskList.items)]};




非常简单。我不知道为什么我昨晚遇到了这个问题。我认为这与我试图嵌套对象VS数组的方式有关。请根据需要提供任何反馈。

它给了我 this结果。