Jump to:

3373 Posts in 998 Topics by 712 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: 347 Views
  • mightycoco
    Avatar
    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 %>
    $Project.Name
    <% end_if %>
    $Name
    <% end_loop %>

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

    Thanks,
    MC

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: Previous / Next in Iterator Link to this post

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

    http://doc.silverstripe.org/framework/en/howto/grouping-dataobjectsets

    347 Views
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.