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.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How to obtain the current position in control loop


Go to End
Reply

11 Posts   3326 Views

Avatar
Terminator4

23 February 2009 at 8:52pm Community Member, 81 Posts

Hey guys,
I am trying to figure out a way to write some code for certain sapphire/forms that will be able to sort out ID clashes when using javascript. I would like to know how it can be determined if the field that is to be displayed is currently operating inside a <% control %> "template".

Is there a global variable that I can check which will notify me if I am in a control loop so that I can modify IDs in the code behind (php files) as I would like to append the control's position (ie 1,2,3,....) to the end of the attribute.

For exampe if I have a calendar called FromDate and this is in to be displayed in a control loop, the ID should rather be FormName_FromDate1, FormName_FromDate2, etc, etc

Thanks

Avatar
schellmax

23 February 2009 at 9:56pm (Last edited: 23 February 2009 9:57pm), Community Member, 126 Posts

what you're looking for is $Pos (see http://doc.silverstripe.org/doku.php?id=built-in-page-controls#dataobjectset_options)

anyway, i don't know what you're after exactly, but this reads as though it could also be accomplished using a class instead of multiple ids, then doing something like this (jquery):

$('.FormName_FromDate').eq(0) ...
$('.FormName_FromDate').eq(1) ...


etc.

(you may also use the jquery 'nth-child' selector, but it's much slower)

Avatar
Terminator4

23 February 2009 at 10:14pm Community Member, 81 Posts

Thanks - yes I have seen this before. This could work to a point but is there no way to rather do it in the actual PHP class by using a variable such as $Pos rather than in the template or will this also work in the code behinds?

Avatar
schellmax

28 February 2009 at 3:27am Community Member, 126 Posts

sorry for delay
can't exaclty imagine what this might be good for...
could you post some example code?

Avatar
UncleCheese

28 February 2009 at 3:40am 4085 Posts

In a PHP class you can use iteratorPos on the object being looped through.

class MyObject extends DataObject
{
function NiceID()
{
return "SomeString_".$this->iteratorPos;
}
}

Avatar
Terminator4

28 February 2009 at 4:27am Community Member, 81 Posts

Sorry, this does not return anything in a Page_Controller function.
Reason I need this is to make iteration unique since they are not.

Avatar
UncleCheese

28 February 2009 at 4:46am 4085 Posts

Please post all of your relevant code.

Avatar
Terminator4

28 February 2009 at 5:07am Community Member, 81 Posts

Basically I have a function called MyFunc and in the template I want to be able to make a calendar unique on a form which is in a control. If a click the calendar or use some AJAX thingy it will not work. So this is what the code is:

<% control Event %>
$Top.CalendarForm
<% end_control %>

PS: I just posted an update to Sapphire on Trac. Hopefully the fix I just submitted will allow for this to work since I can't see any other way for it to work. My fix allows a dynamic parameter to be passed into a function via the template. So, you can have: $Top.CalendarForm($ID)

Go to Top