Reputation: 713
Here is my Domain class
class User {
String admin
String name
String phoneNumber
String email
String questionSet
}
Here is my view.gsp for Jasper report
<g:jasperReport controller="profile" action="jasperdata" format="PDF"
</g:jasperReport>
And here is my controller
def jasperService
def jasperdata(){
List listDetails = []
Map mapDetails =[:]
Map result = [:]
def number=User.executeQuery('select phoneNumber from User')
mapDetails.put('phnno', number)
listDetails.add(mapDetails)
println listDetails
result.data = []
result.data << [phnno:listDetails]
JasperReportDef rep = jasperService.buildReportDefinition(params,request.locale,result)
ByteArrayOutputStream stream = jasperService.generateReport(rep)
response.setHeader("Content-disposition", "attachment; filename=" + 'transaction' + ".pdf")
response.contentType = "application/pdf"
response.outputStream << stream.toByteArray()
}
I can send a single data item to Jasper But i want to send list of data from controller.
Upvotes: 1
Views: 749
Reputation: 100
domain is ok but in your controller you can try:
def jasperService
def jasperdata = { User -> user
List<User> listPhoneNumber = User.list() as Object[]
JasperReportDef rep = jasperService.buildReportDefinition(params,request.locale,[data:listPhoneNumber])
ByteArrayOutputStream stream = jasperService.generateReport(rep)
response.setHeader("Content-disposition", "attachment; filename=" + 'transaction' + ".pdf")
response.contentType = "application/pdf"
response.outputStream << stream.toByteArray()
response.outputStream.flush()
}
View you can try too:
<g:jasperReport action="profile" controller="jasperdata" format="PDF" jasper="listPhone">
</g:jasperReport>
And a listPhone.jrxml, just for a simple test.
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="relatorio" language="groovy"
pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5f768b17-5eec-48ad-bfb8-2c500ea4eedb">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<style name="Title" fontName="Arial" fontSize="26" isBold="true" pdfFontName="Helvetica-Bold"/>
<style name="SubTitle" forecolor="#666666" fontName="Arial" fontSize="18"/>
<style name="Column header" forecolor="#666666" fontName="Arial" fontSize="12" isBold="true"/>
<style name="Detail" fontName="Arial" fontSize="12"/>
<field name="phoneNumber" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="26" splitType="Stretch">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="555" height="20" backcolor="#CCCCCC" uuid="3e5957c3-d694-428b-bcd9-173b2b40d517"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Verdana" isBold="true" isItalic="true"/>
</textElement>
<text><![CDATA[LIST OF PHONE NUMBER]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="36" splitType="Stretch">
<staticText>
<reportElement x="0" y="11" width="555" height="20" uuid="467b7009-00d7-48e7-81c4-7037fcddc5ea"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Verdana" isBold="true"/>
</textElement>
<text><![CDATA[PHONES]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="29" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="555" height="20" uuid="03b9a4c5-59da-4df7-9435-835eda1e83aa"/>
<textElement textAlignment="Center" verticalAlignment="Middle"/>
<textFieldExpression><![CDATA[$F{phoneNumber}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="20" splitType="Stretch">
<textField>
<reportElement x="455" y="0" width="100" height="20" uuid="b53eb0ee-b2b4-4a82-871b-bd622b308572"/>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band splitType="Stretch"/>
</summary>
</jasperReport>
Hope it helps.
Upvotes: 1