Jump to content
Pierre-Luc

Last and first selectors (in repeaters) ?

Recommended Posts

Is there something like that available in PW ? I need to filter something in a repeater by its last value.

Something like that: 

repeater.last.field

repeater.first.field

  • Like 1

Share this post


Link to post
Share on other sites

The repeater is basically a PageArray. So you can use the normal Page/WireArray methods.

$myRepeater->first();

$myRepeater->last();

Look at the Cheatsheet under Page/WireArray. Hope this helps.

Sorry read it to fast.

But it's a PageArray and regarding to the documentation this might work:

$myRepeater->last()->find("myRepeater.fieldname != '' ").

Hopefully someone else can help.

Share this post


Link to post
Share on other sites

After a bit of testing it seems that it takes into account all the values in the repeater (which is ok for my use). But I'd still like to know if it's possible to use selectors that way.

Share this post


Link to post
Share on other sites

Don't understand the question exactly, but here are some nice docs: http://processwire.com/api/fieldtypes/repeaters/

Also repeater pages have a special template called "repeater_yourField". So theoretically you could also get repeater pages the following way:

// Get last item sorted by created DESC
$item = $pages->find("template=repeater_yourField,sort=-created,limit=1"); 

// Get first item sorted by created with an additional condition 
$item = $pages->find("template=repeater_yourField,sort=created,field|field2%=blub,limit=1"); 
  • Like 3

Share this post


Link to post
Share on other sites

would love that too, could use it on a table field to know while searching if the first/last entry matches

Share this post


Link to post
Share on other sites
3 hours ago, Can said:

would love that too, could use it on a table field to know while searching if the first/last entry matches

It requires a couple of steps, but so long as your pages selector doesn't return a huge number of results this should be fairly low impact:

$value = 'Foo';
$all_matches = $pages->find("my_table.my_field=$value");
$first_row_matches = $pages->newPageArray();
foreach($all_matches as $match) {
    if($match->my_table->first()->my_field == $value) $first_row_matches->add($match);
}

 

  • Like 1

Share this post


Link to post
Share on other sites

interesting approach thanks for sharing :-)

think the first subselector could be a nice addition to simplify things ;-)

Share this post


Link to post
Share on other sites

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By neonwired
      I have a front end form for creating new pages, repeater and repeater matrix field don't seem to save any data. I was considering handling the data manually but can't seem to get anything useful from the post data, are there any methods i can use?
    • By t0b1
      Hello there, and thanks for ProcessWire!
      I'm getting to know ProcessWire while doing my first project using it and I really like it so far.
      The challenge I'm facing right now is the following:
      I have a One-Pager using fullpage.js, realized as a single PW-Page containing a Repeater Field where each Repeater Item is one Section.
      Some Sections are supposed to have a little menu at the Top which references/links to different Sections of the Website, so I wanted to use a Checkbox Field "Top Menu" to decide if a Section gets a Menu and a Page Reference Field to choose the different Sections (Repeater Items) it should contain.
      I've already accomplished this by pasting the following code into /site/ready.php:
       
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) { if($event->object->hasField == 'top_menu_entries') { $page = $event->arguments('page'); if($page instanceof RepeaterPage) $page = $page->getForPage(); $event->return = $page->; } });  
      The only problem that still remains is that when I select the Entry of the Repeater Item itself it doesnt save the selection, meaning after I saved it's unselected again.
      On some Sections I do want a Menu-Entry for the Section itself though (which would be styled differently and not link anywhere) for Continuity-Reasons, any ideas on how to achieve that?
    • By Anders
      I want to allow full text search on my site. There is a very nice solution that comes right out of the box:
      $selector = "title|body~=$q, limit=50"; This works, but to make it even better I would want to give higher weight to pages where the search term occurs in the title, than if it just occurs in the body. After all, a page with the title "Wine from France" is probably the best match for the search "france wine". How do I accomplish this in ProcessWire?
      I can see three possible paths, but I am not very fond of any of them:
      Do a direct SQL query, circumventing the API, along these lines. But I would prefer to abstract away the database layout if at all possible. Use something like ElasticSearch, but to be honest that would be to complicated to set up and maintain in the long run. Make multiple lookups, first for matches in the title, then for matches in the body, and merge and sort in PHP. My suspicion is that this would get complicated quite quickly. For instance, how do you deal with a page that has two of the three search terms in the title and the third in the body? Is there a magic option four I should look into? Or are any of the above options better than the others? Any input is welcome!
    • By iipa
      Hi!
      I have two Page Reference fields: Category and Subcategory. Category is parent of Subcategory, and can have 0 to n Subcategories. Fields are selected by dropdown selection.
      I'm trying to achieve following logic:
      1. Select Category (obviously works)
      2. Change Subcategory options based on selected Category (this works)
      3. Hide the Subcategory field if Category has no Subcategories (to prevent weird dropdown with nothing to select)
      I have tried to investigate two possible alternatives to achieve step 3:
      A) Make dynamic condition into Subcategory selection's "Only visible if..." field
      B) Make a hook that fires when Category changes, then hides Subcategory field if Category has no children
      So far I haven't been successful in either. Conditions I've tried always lead to hidden field, and I haven't been able to find set up a hook that fires on field change.
      So here's the question (finally): Is there a way to alter field visibility in API, for example in ready.php or inside a hook? In API I could loop through categories, find the ones that have children and then make a selector based on their IDs.
    • By schwarzdesign
      I'm working on a news feed that will show the most recent news in a full teaser grid, and all older news as a simpler archive-type list view. My selectors so far:
      $news_full = $page->children("template=news, limit={$page->feed_count_full}"); $news_archive = $page->children("template=news, start={$page->feed_count_full}, limit=9999"); The $page->feed_count_full field controls how many items to show in the teaser grid (I've confirmed it contains the correct value, and the $news_full selector works as intended).
      This works, but I don't like the limit in the second selector. Unfortunately, if I leave it out (i.e. I only specify a start, not a limit), the start is ignored and I get all news instead. Not a big problem as we will never have more than 9999 news, but it still bothers me, as semantically speaking I don't want to set a limit in this case.
      Is this the intended behaviour of start/limit selectors? Is there a cleaner way to specify an offset (start selector) without a limit?
      ProcessWire Version 3.0.123
      Thanks!
×
×
  • Create New...