Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions /

Previous / Next in Iterator


Reply


2 Posts   770 Views

Avatar
mightycoco

Community Member, 3 Posts

14 March 2013 at 9:27pm

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

Avatar
kinglozzer

Community Member, 172 Posts

14 March 2013 at 10:24pm

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

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