Jump to content

'Prepend' a Repeater Item?[SOLVED]


e5an
 Share

Recommended Posts

Or more generally, change the order of repeater items from the API?

I'm doing something like this in response to a form submission:

$thatPage->of( false );
$newItem = $thatPage->my_repeater->getNew();
$newItem->foo = 'bar';
$newItem->save();
$thatPage=>my_repeater->add( $newItem );
$thatPage->save();

This works; the newly created item is added to the end of the array.

But what I really wanted was to add it to the start of the item array ( like 'unshift' rather than 'push' ).

It seemed like I should be able to do that by just changing the line

$thatPage=>my_repeater->add( $newItem );

to

$thatPage=>my_repeater->prepend( $newItem );

Surprisingly, this behaves exactly the same. The newly created item is still the last one in the list.

If I can't insert it at position 0, could I insert it at the end, then move it up to the start in the next line? (How?)

I guess the obvious 'solution' is to drag-and-drop reverse the order of all the repeater items in the backend GUI, then change the php template of that page to display them in reverse order... Could have already done that and moved on but I'd rather understand whatever it is here I'm not getting.

 

 

 

 

Link to comment
Share on other sites

When adding/saving Repeater items the sort order is determined by the "sort" value of each Repeater page, not by any order you might apply to the Repeater PageArray. An explanation of sort values is here.

You could use the $pages->sort() method to set the sort value of the Repeater page you're adding and it will automatically update the sort values of the sibling Repeater pages.

$thatPage->of( false );
$newItem = $thatPage->my_repeater->getNew();
$newItem->foo = 'bar';
$newItem->save();
$thatPage=>my_repeater->add( $newItem );
$thatPage->save();
// Set the sort value of the new item to 0 and adjust sibling sort values
$pages->sort($newItem, 0);

 

  • Like 2
  • Thanks 2
Link to comment
Share on other sites

  • e5an changed the title to 'Prepend' a Repeater Item?[SOLVED]

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...