e5an Posted November 3, 2021 Share Posted November 3, 2021 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 More sharing options...
Robin S Posted November 4, 2021 Share Posted November 4, 2021 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); 2 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now