【问题标题】:Conditional alignment of textfields文本字段的条件对齐
【发布时间】:2013-09-04 10:04:01
【问题描述】:

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

除了在没有三振价格时价格对齐之外,一切正常。两种价格都需要水平对齐,并且在任何一种情况下都需要居中对齐。但如果删除价格为空/空白,我无法将当前价格文本字段居中对齐。当有删除价格时,这不是问题,因为两个文本字段在设计器视图中都正确对齐。

另外,如何根据罢工价格的可用性设置条件格式?如果没有删除价格,当前价格的字体颜色应该与有删除价格时不同。

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

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

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

【问题讨论】:

  • 你能用图片说明你的问题吗?
  • @AlexK 添加了图片。第一个是它应该如何。由于缺少三振价格,第二个应该使价格保持一致。谢谢!

标签: jasper-reports ireport


【解决方案1】:

您可以在Conditional Styles 的帮助下轻松做到这一点。

样本

测试csv数据源:

name,price,discount_price
Shirt,"100,00","90,99"
Trousers,"80,00",
"Sun glasses","199,99","120,00"

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>

在这个示例中,我更改了文本的 forecolor(设为 Blue)和文本的水平对齐方式(设为 Central,如果没有折扣价(未设置或等于0.0),则默认为Left

iReport 中的设计如下:

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


注意:

为了获得更好的格式,我在示例中使用了markup syntax。您还可以在this post中阅读有关样式的信息

【讨论】:

  • 完美!非常感谢
猜你喜欢
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多