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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

How to obtain the current position in control loop


Go to End


11 Posts   4703 Views

Avatar
Terminator4

Community Member, 81 Posts

23 February 2009 at 8:52pm

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

Community Member, 126 Posts

23 February 2009 at 9:56pm

Edited: 23/02/2009 9:57pm

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

Community Member, 81 Posts

23 February 2009 at 10:14pm

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

Community Member, 126 Posts

28 February 2009 at 3:27am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

28 February 2009 at 3:40am

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

Community Member, 81 Posts

28 February 2009 at 4:27am

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

Forum Moderator, 4102 Posts

28 February 2009 at 4:46am

Please post all of your relevant code.

Avatar
Terminator4

Community Member, 81 Posts

28 February 2009 at 5:07am

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