Jump to content

How to AJAX refresh field on change of another in editor?


hellomoto
 Share

Recommended Posts

I have a manufacturers page select field and a dependent models one with pages of template `model` which are allowed children of pages with template `manufacturer`. So I have this as the findPagesCode for the models page options field:

return $page->manufacturer->children();

However this requires the page be saved in order to display options. This is not ideal. 

I have a singular autoload module with the following:

public function init() {
    $this->pages->addHookAfter('render', $this, 'filterModels');
}
public function filterModels($event) {
    $page = $event->arguments('page');
    if($page->template != 'boat_vessel') return;
    $this->message("models filter");
}

It's doing nothing. 

I was thinking I could work out something with this example but I would need the above test to be working first anyway...

$this->pages->addHookAfter('changed', function(HookEvent $event) {
  $page = $event->object;
  $change = $event->arguments(0);
  if($page->template == 'boat_vessel' && $change == 'manufacturer') {
    // execute some code 
  }
});

But what? How do I refresh the models field?

Is there a way to do this in the field settings? I would think in the custom PHP textarea that `return $page->manufacturer->children();` would work but it doesn't. 

Link to comment
Share on other sites

I'm trying to alter some code I used to do something similar (fields for selected template) on another project (here) and so far just this simple direct snippet is not doing anything:

  public function init() {
    $this->pages->addHookAfter('render', $this, 'filterModels');
  }

  public function filterModels($event) {
    $page = $event->arguments('page');
    if($page->template != 'boat_vessel') return;
    $this->message("models filter");
  }

The module is active... Why no results?

Link to comment
Share on other sites

There is limited (and undocumented) AJAX 'dependent selects' support built into Page Reference fields. You must use the 'Custom find' or 'Selector string' option for defining selectable pages, and reference the source Page Reference field (that exists on the same page) using syntax like this:

parent=page.page_reference_field_name

Based on testing I have found:

  • It only works for Select, Select Multiple or AsmSelect inputfields
  • It does not work inside a repeater item
Edited by Robin S
Included 'Custom find'
  • Like 7
Link to comment
Share on other sites

  • 4 years later...
On 4/9/2017 at 6:19 PM, Robin S said:

There is limited (and undocumented) AJAX 'dependent selects' support built into Page Reference fields. You must use the 'Custom find' or 'Selector string' option for defining selectable pages, and reference the source Page Reference field (that exists on the same page) using syntax like this:

parent=page.page_reference_field_name

Based on testing I have found:

  • It only works for Select, Select Multiple or AsmSelect inputfields
  • It does not work inside a repeater item

Does this solution require JS code or php code anywhere, other than the simple instruction you provided for code in the Selector String field?

Link to comment
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
 Share

×
×
  • Create New...