0

I am trying to access to nested class parameters in Play Framework scala template.

Here is model structure:

public class Item {
    public int id;
    public String title;
    public List<Part> parts;
}

public class Part {
    public int id;
    public String name;

    public class MechanicalPart extends Part {
        public Material material;
    }
    public class ElectricalPart extends Part {
        public int voltage;
        public int watts;
    }
}

I would like to list that data with loop in scala template:

  @(item: shop.models.Item)

        <ol>List of part of @item.title
        @for(part <- item.parts) {
           <li>
                @part.name
                <ul>
                    @if(part.getClass().getSimpleName().equals("MechanicalPart")) {
                       <li>@part.material</li>
                    }
                    @if(part.getClass().getSimpleName().equals("ElectricalPart")) {
                       <li>@part.voltage</li>
                       <li>@part.watts</li>
                    }
                </ul>
           </li>
        }
        </ol>

But I can't, cause I am looping over Part class elements, and this parameters are in extended classess.

I think that If I create new instance of nested classes, or somehow cast to nested classess it could work, but I cant. Please help.

I've tried:

@(new shop.models.Part()#new MechanicalPart().material)

but id dosn't compile. Please help.

masterdany88
  • 5,041
  • 11
  • 58
  • 132

0 Answers0