Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » How to obtain the current position in control loop

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

Page: 1 2
Go to End
Author Topic: 3229 Views
  • Terminator4
    Avatar
    Community Member
    81 Posts

    How to obtain the current position in control loop Link to this post

    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

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: How to obtain the current position in control loop Link to this post

    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)

  • Terminator4
    Avatar
    Community Member
    81 Posts

    Re: How to obtain the current position in control loop Link to this post

    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?

  • schellmax
    Avatar
    Community Member
    126 Posts

    Re: How to obtain the current position in control loop Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: How to obtain the current position in control loop Link to this post

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

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

  • Terminator4
    Avatar
    Community Member
    81 Posts

    Re: How to obtain the current position in control loop Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: How to obtain the current position in control loop Link to this post

    Please post all of your relevant code.

  • Terminator4
    Avatar
    Community Member
    81 Posts

    Re: How to obtain the current position in control loop Link to this post

    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)

    3229 Views
Page: 1 2
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.