文本字段的条件对齐

nkr*_*pta 0 jasper-reports ireport

我正在为具有产品价格(取自数据源)的图像设计模板.如果产品具有删除价格,则图像会显示它,然后显示当前价格.如果没有删除价格,则仅显示当前价格.

一切正常,除了没有三振出价时的价格调整.两种价格都需要水平放在一起,并且在任何一种情况下都是中心对齐的.但如果删除线价格为空/空白,我无法对当前价格文本字段进行居中对齐.当存在三振出价时,这不是问题,因为两个文本字段在设计器视图中都正确对齐.

另外,如何根据strikout-price的可用性放置条件格式?如果不存在删除线价格,则当前价格的字体颜色应与有删除价格时的字体颜色不同.

编辑:以下两种情况的图像

当Strikeout-price可用时,它应该是这样的:

当Strikeout-price可用时,它应该是它的外观

当Strikeout-price不可用时,显示价格的文本字段应居中对齐:

当Strikeout-price不可用时,显示价格的文本字段应居中对齐

Ale*_*x K 5

您可以在条件样式的帮助下轻松完成.

例子

测试csv数据源:

name,price,discount_price
Shirt,"100,00","90,99"
Trousers,"80,00",
"Sun glasses","199,99","120,00"
Run Code Online (Sandbox Code Playgroud)

JRXML文件:

<?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="two_textfields_one_on_other" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="cb5d2807-1ef9-4ffb-8df8-fcb781054bb0">
    <style name="priceStyle" hAlign="Left">
        <conditionalStyle>
            <conditionExpression><![CDATA[$F{discount_price} == null || $F{discount_price} == 0.]]></conditionExpression>
            <style forecolor="#3333FF" hAlign="Center" vAlign="Top"/>
        </conditionalStyle>
    </style>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="price" class="java.lang.Double"/>
    <field name="discount_price" class="java.lang.Double"/>
    <detail>
        <band height="40" splitType="Stretch">
            <textField isStretchWithOverflow="true">
                <reportElement uuid="93ad241e-5257-4664-b3ab-f6448f786775" x="0" y="0" width="114" height="20"/>
                <box>
                    <topPen lineWidth="0.25"/>
                    <leftPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement markup="styled"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement uuid="6b53f6c6-5d48-4879-8934-d446150e1375" style="priceStyle" positionType="Float" x="0" y="20" width="114" height="20"/>
                <box>
                    <leftPen lineWidth="0.25"/>
                    <bottomPen lineWidth="0.25"/>
                    <rightPen lineWidth="0.25"/>
                </box>
                <textElement markup="styled"/>
                <textFieldExpression><![CDATA[($F{discount_price} != null && $F{discount_price} > 0.) ?  "<style isStrikeThrough='true'>"+ $F{price} + " $</style>  " + $F{discount_price} + " $" : $F{price} + " $"]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>
Run Code Online (Sandbox Code Playgroud)

在此示例中,我更改了文本的前景色(使其为蓝色)和文本的水平对齐(使其成为中心,默认为),以防折扣价格不存在(未设置或等于0.0).

iReport中的设计如下所示:

在此输入图像描述

结果将是(通过iReport中的预览):

在此输入图像描述


注意:

为了更好的格式化,我在我的示例中使用了标记语法.您还可以在这篇文章中阅读有关样式的内容