Repeaters
Several people forwarded questions about Kentico repeaters after viewing the first partner webinar given by Petr, so it made sense for the Tips and Tricks this month to answer at least some of these.
A repeater is essentially nothing more than a loop. It loops through a group of items, repeatedly applying a transformation to each. However, if you dig a little deeper, you’ll find that the Repeater is one of the real strengths of Kentico.
Given the appropriate queries and transformations for a given document type, a user with little or no programming experience can construct pages on the fly using repeaters, choosing not only what is displayed, but how it is displayed, when it is will be displayed, and to whom it will be displayed.
Once you get past the initial question - “What is the big deal about repeaters?” - and begin using them, you very quickly come to the next question - “How can I conditonally change what is displayed?“, and while I won’t try to answer every question that then stems from this, I’ll provide the groundwork which will get you started.
Conditional Display
There are several ways to conditionally display content within a transformation, but two that I use regularly are:
- the conditional operator
- block visibility
The Conditional operator
One of my favourite constructs within C# is the conditional operator. It uses the syntax condition?true:false and is by far the easiest way to add conditonal display elements to a transformation.
The use of the conditional operator can be demonstrated via a simple test for whether the record being displayed is the first record, but I’ll follow up with a couple of other funky real world examples that I use regularly.
Conditional first record
The following transformation displays “First record” for the first record displayed by the repeater, and “Not first record” for each record other than the first.
<%# DataItemIndex ==0?”First record”:”Not first record” %>
Conditional last record
The following transformation displays “Last record” for the first record displayed by the repeater, and “Not last record” for each record other than the last.
<%# DataItemIndex==DataRowView.DataView.Count-1?”Last record”:”Not last record” %>
Conditional based upon changing value
A requirement you’ll come across fairly regularly in reporting is the ability to show a value if it is different from the row above it.
The following transformation displays “Changed” if the value contained within the “name” field is different from the value contained within the “name” field for the previous record, and “Not changed” if it is the same. You’ll notice that the test also looks for the first row. This is because the frist row has no previous row to test against.
<%# DataItemIndex==0?”Changed”:DataRowView.DataView[DataItemIndex][“name”].ToString()!=DataRowView.DataView[DataItemIndex-1][“name”].ToString()?”Changed”:”Not changed” %>
Although this one looks a little funky, it’s basically just saying: if this is the first record display “Changed”. Otherwise, check whether the value of the “name” field is different to the previous value of the “name” field, and display “Changed” if it is different, or “Not changed” if it is the same.
Block visibility
The conditional operator very quickly becomes unwieldy and difficult to work with if you have entire chunks of HTML that you wish to display conditonally. The following example demonstrates this.
<%# DataItemIndex ==0?”<div class=\”changedrecord\”><img src=\”/images/changedrecord.gif\” alt=\”Changed record\” /></div><p>Caption</p>”:”” %>
This can be overcome using a block visibility technique, which basically involves making an entire block of HTML visible based upon a condition operator.
The following example does exactly the same as the previous code, but in a way that is much easier to read and maintain.
<div
runat=”server”
visible=”<%# DataItemIndex==0?”true”:”false” #>”
class=”changedrecord”>
<img
src=”/images/changedrecord.gif”
alt=”Changed record” />
<p>Caption</p>
</div>
Hope this shines a light on what is a very cool capability, that a lot of people are not yet taking advantage of.
Trackback URL:
http://www.kenticodeveloper.com/trackback/efba035d-9d74-40eb-b94f-894191b0a0e7/Conditional-transformations-in-Kentico.aspx
Comments