hellomoto

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

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. 

Share this post


Link to post
Share on other sites

Actually it does, but you have to save the page to get the childpages field options.

How can I auto-save the parentpage field on selection change and immediately subsequently auto-refresh the childpage field?

Share this post


Link to post
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?

Share this post


Link to post
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 6

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 MilenKo
      Hello guys.
      I've decided to get brave and start my first delayed output profile for a remake of my knowledge sharing profile. It went all.good so far but I decided to make it multilingual as to fit the users needs.
      For starters, added a field named: image_single and limited the input to one image as this would be used for the logo. Added.the markup to allow the front end editing (method D or direct edit tag to the <img>. After double clicking on the image, I see the pop-up showing up for a second and then closes. As far as there are no errors in the logs, I am a bit stuck to find the reason. I've read earlier that some users had issues with multilingual fields but could not find anything to point me to the right direction. Any ideas or suggestions?
    • By dweeda
      How do I migrate a template with all its fields and values to a new site? Is their an export/import process?
    • By rareyush
      I am using module "FormTemplateProcessor" to get data in a pages but they all are unpublished and when I am trying to display them with relevent pages it not working
       
      page1 p1 p2 p3 page2 (FormTemplateProcessor) up1 (unpublished page and have p1 id on field knows as "ID") up2 (unpublished page and have p3 id on field knows as "ID") up3 (unpublished page and have p2 id on field knows as "ID") now here
      up1, ip2, up3 can be created by p1, p2 or p3 and I am saving respectively I'm saving their id in field which is being used by up1,up2,up3, etc template.
       
      now I want to show the data from unpublished pages which belongs to p1, p2 and p3 respectively
       
       
      anyone has experienced this or knows something which can help me out ?
    • By Peter Knight
      A minor gripe but one that trips me up every time.
      When there's naming error on a field, ALL the field values are erased and I have to setup everything again. IE name, type, label and descriptions details and sometimes notes.
      Would be really helpful if ProcessWire didn't zap all the values but just cleared the illegal field name. 
       

       
       
       
    • By louisstephens
      I have been following @bernhard's great tutorial on creating a Dashboard and everything has very easy to understand so far. However, I need to add a select field that a template is using (so users can create a new page from the dashboard), but I am really at a complete lost on how to achieve this. I have been googling, but can't really find anything on the matter.
      $field = $this->modules->get('InputfieldSelect'); $field->name = "Select Manufacturer"; $field->findPagesSelector = "template=basic-page"; //not needed for select field $field->labelFieldName = "title"; $field->name = "Manufacturer Select"; $field->columnWidth = 34; $fieldset->add($field); I have the above code, which was originally an InputfieldPage, but since this is an options field, I have changed it. Has anyone actually gotten a select from a template (and its' values) in a module?