如何在运行时从jasper中删除标题标记?

时间:2016-01-06 10:37:24

标签: java jasper-reports

我的标题标签有3个子图像和2个文本字段。在任何情况下,如果所有3个孩子都为空,我想从报告中删除/隐藏完整的标题标签。有没有办法在运行时执行它而不更改我的jrxml文件。

1 个答案:

答案 0 :(得分:1)

您可以将jrxml加载到null对象中并设置标题JasperDesign design = JRXmlLoader.load("jasper/dbTest2.jrxml"); design.setTitle(null); JasperReport report = JasperCompileManager.compileReport(design);

例如

JasperPrint

请注意,如果要删除乐队,您需要在填写报告之前知道

填写报告后,您将拥有printWhenExpression这也可以操作,但您需要逐页进行操作(不再是标题带,而是页面表示)。删除对象意味着您需要移动所有其他对象......

正确的方法通常是使用title频段上的<title> <band height="201"> <printWhenExpression><![CDATA[$F{image1}!=null && $F{text1}!=null && $F{text2}!=null]]></printWhenExpression> ... your images and text fields </band> </title>

JasperDesign design = JRXmlLoader.load("jasper/dbTest2.jrxml");
JRDesignBand title = (JRDesignBand) design.getTitle();
title.setPrintWhenExpression(new JRDesignExpression("$F{image1}!=null && $F{text1}!=null && $F{text2}!=null"));

因此,请考虑修改您的jrxml或在运行时添加此表达式。

def type_spec_iterable(obj, name):
    tps = set(type_spec(e) for e in obj)
    if len(tps) == 1:
        return name + "<" + next(iter(tps)) + ">"
    else:
        return name + "<?>"


def type_spec_dict(obj):
    tps = set((type_spec(k), type_spec(v)) for (k,v) in obj.iteritems())
    keytypes = set(k for (k, v) in tps)
    valtypes =  set(v for (k, v) in tps)
    kt = next(iter(keytypes)) if len(keytypes) == 1 else "?"
    vt = next(iter(valtypes)) if len(valtypes) == 1 else "?"
    return "dict<%s, %s>" % (kt, vt)


def type_spec_tuple(obj):
    return "tuple<" + ", ".join(type_spec(e) for e in obj) + ">"


def type_spec(obj):
    t = type(obj)
    res = {
        int: "int",
        str: "str",
        bool: "bool",
        float: "float",
        type(None): "(none)",
        list: lambda o: type_spec_iterable(o, 'list'),
        set: lambda o: type_spec_iterable(o, 'set'),
        dict: type_spec_dict,
        tuple: type_spec_tuple,
    }.get(t, lambda o: type(o).__name__)
    return res if type(res) is str else res(obj)


if __name__ == "__main__":
    class Foo(object):
        pass
    for obj in [
        1,
        2.3,
        None,
        False,
        "hello",
        [1, 2, 3],
        ["a", "b"],
        [1, "h"],
        (False, 1, "2"),
        set([1.2, 2.3, 3.4]),
        [[1,2,3],[4,5,6],[7,8,9]],
        [(1,'a'), (2, 'b')],
        {1:'b', 2:'c'},
        [Foo()], # todo - inheritance?
    ]:
        print repr(obj), ":", type_spec(obj)