you can't refer a collection that is not in present the context in jx:each or you get a 'variablename' expression is not a collection'

Issue #81 resolved
nicolas frank
created an issue

In my template I need to display a lot of optional collections depending of data provided. With jxls1 I was able to add a jsxl:if before the collection in order to ignore the collection. This is no longer working with jxls2 which just doesn't allow me to test if an entry is present in the context (no way to do jx:if condition="!empty(myVar)", myVar has to be in the Context...

But in fact, it would be so much more simple to consider that a xls:each() on a missing collection is like an each on an empty collection.

Could you change this behaviour (or provide at least a way to test is an item is available or not in the Context to evaluate it with an jx:if that could be put before the ach) ?

Thanks.

Comments (3)

  1. nicolas frank reporter

    Code change : in Util.java, in transformToCollectionObject,

    if (collectionObject == null) { return null; }

    then in achCommand.java :

    something like :

    public Size applyAt(CellRef cellRef, Context context) {
        Collection itemsCollection = Util.transformToCollectionObject(getTransformationConfig().getExpressionEvaluator(), items, context);
    

    if (itemsCollection==null) {itemsCollection = new ArrayList()} <-- any empty collection

  2. leonate NA repo owner

    jx:if command with condition like "!empty(myVar)" works fine for me.

    Also you can use JEXL to ignore empty collections in each-command with the expressions like this

    items="!empty(employees) ? employees : new('java.util.ArrayList') "
    

    But I agree we can simplify this by implementing the code change.

  3. Log in to comment