• Resolved s

    (@sdnazdi)


    Hi,

    I like to create multiple custom fields which are as follows. Assume I like to create address section, and I want to define a field for a list of 20 provinces, and (one/some) field(s) for their respective cities. I mean if I choose one Province, then in the cities’ field, it only shows this Province’ cities not all cities of the country.

    My question is that

    • Is it possible to do this with only 2 relationship fields, one for list of Provinces, and one for their respective list of cities, and I can choose group of cities for each province?
    • If not possible, should I define for instance 20 separate relationship fields which each of them includes the name of one Province?
    • If none of my methods is correct, what would be the correct method?

      Thanks
    • This topic was modified 2 years, 4 months ago by s.
    • This topic was modified 2 years, 4 months ago by s.
    • This topic was modified 2 years, 4 months ago by s.
    • This topic was modified 2 years, 4 months ago by s.
    • This topic was modified 2 years, 4 months ago by s.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support pdclark

    (@pdclark)

    • Having one field change based on the value of another field without a page refresh requires monitoring and updating the fields with JavaScript.
    • Hypothetically, if one desires a “no code” solution, this could be done entirely through the Pods GUI by defining a relationship field for every province. I would not say one “should” do this, but it would technically function.
    • It may be possible to do with two fields only if one monitors their state manually with JavaScript, filtering and setting values as a user interacts. This is not trivial, as Pods field state is managed by React, which causes the DOM to frequently change, because React manages state separately. It might be possible with https://docs.pods.io/code/dfv-js-api/ . It would certainly be possible with a JavaScript MutationObserver, although that is a fairly advanced technique. In modifying the state of select fields managed by React, it would have to not only monitor for user interactions, but also monitor for changes made by React rendering loops to make sure filters and change events stay attached.
    • The simplest method may be to define a custom field group using https://developer.ww.wp.xz.cn/plugins/metadata/custom-meta-boxes/ , which could contain several HTML field inputs as well as JavaScript to manage their display. One would have to save the field data as documented at the link, but the JavaScript would be as simple as possible, allowing for well-established convenience methods such as jQuery .on( ‘change’, … ) to monitor form changes, .val() to set default values, and .remove() and .append() to switch out the options within the secondary select field. Remove and append are critical in this case, as many browsers do not support showing / hiding <option>s within a <select>, due to realities where rendering is often handed off to the operating system rather than the browser for that field type.
    Plugin Support pdclark

    (@pdclark)

    The MutationObserver approach would by far be the most complex, but would allow two native Pods fields to behave in novel ways. Here is an example of similar functionality: https://ww.wp.xz.cn/support/topic/sorting-autocomplete-selections/

    If looking for speed, flexibility, and simplicity, a custom Metabox with the core WordPress API might be a good fit and is a useful skill for many situations.

    If one needs it working in 60 minutes or less, 20 city fields per province will do it.

    I tend to approach problems not so much in terms of “correct” or “incorrect”, but rather in terms of performance, time, usability, and maintainability. Programming is language — there are many ways to write the same thing.

    WebObserver is fancy, but might break on updates.

    Many Pods fields is potentially costly computationally, but is easiest to manage and may be within acceptable thresholds if there are not hundreds of other fields on the same page.

    A custom Metabox might feel daunting if unfamiliar with PHP and JS, but the required methods are fairly simple and well-documented due to their wide use and age.

    Thread Starter s

    (@sdnazdi)

    Thanks @pdclark. I will try your suggestion.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Conditinal logic for multi step relationship field / Items with subitems’ is closed to new replies.