Jump to:

3461 Posts in 1065 Topics by 740 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Previous / Next in Iterator

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 716 Views
  • mightycoco
    Community Member
    3 Posts

    Previous / Next in Iterator Link to this post

    Is there a way (preferably in the template engine) to access the previous/next item in a DataList?

    I have a Datalist, containing Worktimes, each Worktime is a has_one to a project (Project -> has_many(Worktimes))

    I'd like to show in a template a list of worktimes.
    If i Iterate through the worktimes

    <% loop Worktimes %>$Project.Name - $Name<% end_loop %>

    I get something like

    MyProject1 - Something1
    MyProject1 - Something2
    MyProject2 - Something1
    MyProject2 - Something2

    But I'd like to have something like

    MyProject1 - Something1
    - Something2
    MyProject2 - Something1
    - Something2

    So to speak, show the Project-Name only, if the previous Worktime is in a different Project...
    My idea was something like this in the template:

    <% loop Worktimes %>
    <% if Previous.Project.ID != $project.ID %>
    <% end_if %>
    <% end_loop %>

    For this to work the DataList Iterator would need something like a Previous/Next implementation. Any Ideas, how to achieve this?


  • kinglozzer
    Community Member
    146 Posts

    Re: Previous / Next in Iterator Link to this post

    What you need is a GroupedList, so you could group your data by Project:


Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.