Tuesday, March 23, 2010

Alternate record coloring in jasper reports

Alternate record styles in jasper reports



in .jrxml file

 we need to write one style like this

     <style name="Row" isDefault="false" mode="Transparent">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            <style isDefault="false" style="Row" backcolor="#E6DAC3"/>
        </conditionalStyle>
    </style>



Need to give this style name  for each record


    <reportElement style="Row" mode="Opaque" x="0" y="0" width="555" height="15"/>



So my .jrxml file is like this

<?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="null" language="groovy" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <style name="Title" isDefault="false" fontName="Times New Roman" fontSize="50" isBold="true" pdfFontName="Times-Bold"/>
    <style name="SubTitle" isDefault="false" forecolor="#736343" fontName="Arial" fontSize="18"/>
    <style name="Column header" isDefault="false" forecolor="#666666" fontName="Arial" fontSize="12" isBold="true"/>
    <style name="Detail" isDefault="false" fontName="Arial" fontSize="12"/>
    <style name="Row" isDefault="false" mode="Transparent">
        <conditionalStyle>
            <conditionExpression><![CDATA[$V{REPORT_COUNT}%2 == 0]]></conditionExpression>
            <style isDefault="false" style="Row" backcolor="#E6DAC3"/>
        </conditionalStyle>
    </style>
    <queryString language="SQL">
        <![CDATA[SELECT
     bp_indent.`indent_id` AS bp_indent_indent_id,
     bp_indent.`indent_no` AS bp_indent_indent_no,
     bp_indent.`req_emp_id` AS bp_indent_req_emp_id,
     bp_indent.`indent_date` AS bp_indent_indent_date,
     bp_indent.`indent_status` AS bp_indent_indent_status
FROM
     `bp_indent` bp_indent]]>
    </queryString>
    <field name="bp_indent_indent_id" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="bp_indent_indent_no" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="bp_indent_req_emp_id" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="bp_indent_indent_date" class="java.sql.Timestamp">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="bp_indent_indent_status" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="31" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="16" splitType="Stretch">
            <line>
                <reportElement positionType="FixRelativeToBottom" x="0" y="15" width="555" height="1"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
            <staticText>
                <reportElement style="Column header" x="0" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
               <text><![CDATA[bp_indent_indent_id]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="111" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
                <text><![CDATA[bp_indent_indent_no]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="222" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
                <text><![CDATA[bp_indent_req_emp_id]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="333" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
                <text><![CDATA[bp_indent_indent_date]]></text>
            </staticText>
            <staticText>
                <reportElement style="Column header" x="444" y="0" width="111" height="15" forecolor="#736343"/>
                <textElement/>
                <text><![CDATA[bp_indent_indent_status]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="15" splitType="Stretch">
            <frame>
                <reportElement style="Row" mode="Opaque" x="0" y="0" width="555" height="15"/>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="0" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.Integer"><![CDATA[$F{bp_indent_indent_id}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="111" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.String"><![CDATA[$F{bp_indent_indent_no}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="222" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.Integer"><![CDATA[$F{bp_indent_req_emp_id}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="333" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.sql.Timestamp"><![CDATA[$F{bp_indent_indent_date}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="true">
                    <reportElement style="Detail" x="444" y="0" width="111" height="15"/>
                    <textElement/>
                    <textFieldExpression class="java.lang.String"><![CDATA[$F{bp_indent_indent_status}]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch">
            <line>
                <reportElement positionType="FixRelativeToBottom" x="0" y="3" width="555" height="1"/>
                <graphicElement>
                    <pen lineWidth="0.5" lineColor="#999999"/>
                </graphicElement>
            </line>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="25" splitType="Stretch">
            <frame>
                <reportElement mode="Opaque" x="-21" y="1" width="597" height="24" forecolor="#D0B48E" backcolor="#F2EBDF"/>
                <textField evaluationTime="Report">
                    <reportElement style="Column header" x="533" y="0" width="40" height="20" forecolor="#736343"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression class="java.lang.String"><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement style="Column header" x="453" y="0" width="80" height="20" forecolor="#736343"/>
                    <textElement textAlignment="Right" verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression class="java.lang.String"><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression>
                </textField>
                <textField pattern="EEEEE dd MMMMM yyyy">
                    <reportElement style="Column header" x="22" y="1" width="197" height="20" forecolor="#736343"/>
                    <textElement verticalAlignment="Middle">
                        <font size="10" isBold="false"/>
                    </textElement>
                    <textFieldExpression class="java.util.Date"><![CDATA[new java.util.Date()]]></textFieldExpression>
                </textField>
            </frame>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

4 comments:

  1. really helpful....thanks a lot :)

    ReplyDelete
  2. in ireport make a box. put it in detail...fill it with color.
    find this property and set it as shown


    Print When Expression = new Boolean(($V{REPORT_COUNT}% 2) == 0)

    ReplyDelete