Jump to content
bwakad

state and cities

Recommended Posts

Hello, right now, in backend I have the following:

1: Cities-page (parent) > city-page (child) > item-page (child)

2: States-page (parent) > state-page (child)

They are both select fields for an item to fill in - and still only in the back-end. But how would I go and make this relational?

At first I thought to move the cities and all inside the state, but then I thought this would probably not make sence, because I would end up with a lot of selects.

And all I really need is a first select (states) which in term fills the second select (cities) upon choosing a value in the first.

So in short I place those fields on my template, but how to connect them?

Share this post


Link to post
Share on other sites

When talking about the page reference field - do you mean the page field type ?

Share this post


Link to post
Share on other sites

But in general, this - and your interesting examples - would only give me two select which can be relational on a per page base, but not on its value.

Like dynamic population of dropdown lists.

But there must be a way to make one field point to another field.

Share this post


Link to post
Share on other sites

That's what I said...that tutorial is not really what you want...it was a btw...Dynamic population of dropdown lists is another matter altogether which I haven't the faintest clue about (i.e. in the Admin) save for that  it will require some Ajax...:-). Sorry, I can't be of help...

Share this post


Link to post
Share on other sites

Using Skyscrapers by the way.

Well, it's the first flaw I found in PW - not being able to make fields depend on eachother.

Maybe I just need to have my pages like this:

states > state-name > cities > city-name > items

Maybe I just leave items out. They are not totally dependend on the city.

I only have 12 states over here, and 403 cities. And states / cities would still be parent pages.

Share this post


Link to post
Share on other sites

Field dependencies, .. not sure if that would work, as this would require to reload the second select. And field dependencies isn't able to define what the page field has for the parent. As kongondo mentioned not a trivial thing, and may require some magic ajax. But maybe not. 

EDIT: My bad, I completely forgot this is possible in new version 2.4 with using a custom selector (as described in next post) I forgto about it and remember now Ryan mentioned it in some thread and to be forgotten...

There's currently no out of the box UI solution. But you could kinda can make it happen with selecting the first select, save page, selecting the second.

You can use the "custom php code" of the page fields "Input" settings tab. Instead of a parent or custom selector you would have this kind of code in the second select and read the saved value of the first.

So reading again your post...

If you have them nested as pages like:

- state1

   - city1

   - city2

- state2

  - city3

Then the first select:

"states_select" = is the page field single select to select states, using a parent or template.

Then in the second select let's say "city_select", you would use such a code to populate the page select:

if($page->states_select) return $pages->find('parent={$page->states_select}');

"$page->states_select" is the first select field on the current page. So if a page is selected there we will use a selector to get all children of the parent and return the page array to populate the second select.

Edit: with some clever hooks you could add some javascript to the admin that handles the updating, either via ajax or adding an event listener for an on change of the first select and save the page by triggering the save button. 

Don't try this as it can corrupt your page.

  • Like 1

Share this post


Link to post
Share on other sites

Well, I want to thank you all for the pointers - It does exist, but I was looking on wrong search terms in the forum and all.

I have read a post from Ryan:

Quote

Is there a way to make the values of one dropdown dependent on the chosen value of the other dropdown?
For example if I choose in a country dropdown "France" the second City dropdown will show "Paris, Marseille, Lyon, Toulouse etc."

Actually, you can do this on the dev branch. Lets assume that cities are children of countries and that is reflected in your page structure (i.e. /countries/france/paris/). Create your 2 page fields (country and city), and configure 'country' as a single page field with "parent" set to "/countries/". Next, for the "city" field configuration, use the "Custom selector to find selectable pages", present on the "input" tab. In that field, enter "parent=page.country". Save and try it out. This works with select, selectMultiple and asmSelect fields (and possibly others), though not yet with checkboxes, radios or PageListSelect.

- So, I have Skyscrapers -

my pages are:

provinces

 - province (drenthe)

 - - municipal (this)

 - - municipal (that)

 - - municipal (some)

my templates are:

provinces

 - province

 - - testing (added fields: province and municipal)

my fields are:

page field : province

> single/null

> required

> parent selectable pages is Provinces

page field : municipal

> single/null

> required

> custom selector is parent=page.province

Now it works thanks to you guys! If I select a province (which is required to choose) - I also get a select list for municipals in that province (and is also required to choose). And I do not have to use visibility because I need to choose a province and municipal.

But I do wonder why a structure like this is not working?

Provinces

- a

- b

Municipal

- a

- b

  • Like 7

Share this post


Link to post
Share on other sites

Yea I remember Ryan mentioned it somewhere but didn't remember it was already in and working dynamicly without saving page. 

That's a useful feature, just a shame nobody knows. :D

  • Like 1

Share this post


Link to post
Share on other sites

@bwakad It's simple.. if you think about it, your separate structure won't work cause it's not nested and thus parent=selected_parent. Isn't working there.

You would maybe need some different selector then, but not possitive it's possible. You need to be able to make some connection like the parent > child. 

  • Like 1

Share this post


Link to post
Share on other sites

Yeah, I thought about that.

So, like I mentioned in my last post (before this one) I made it like this....

I have made my pages:

 root (skyscrapers)

 - provinces (parent)

 - - province (name of province)

 - - - municipal (name of municipal)

In the templates for province and municipal, I just use the title fields now, but I am planning to include the map too.

Then I renamed the following:

root (skyscrapers) stays the same for now

 - cities > is now jobs

 - - city > is now company

 - - - item > is the same (I use it as function title)

Eventually I will separate company - and link that to users...

Item (the entry itself) is still using the skyscrapers template - where I just added the two fields province and municipal.

Items are saved as:  / Jobs / Company(name) / Title(function)

And before saving it is required to select a value in both which are relational. This assures me also that items are separated in the tree from province and municipal.

Working great. Maybe someone can benefit from this. I learn a lot from you guys, so I am happy with PW.

The only thing I would like is to have a fixed width of the select box... have to look in that.

The field 'municipal' was of course empty by default - producing just a little select button, maybe 5mm width -  because no province was selected. Since the field municipal was related to province I could not use a normal default value to make this field become at least display something.

The solution for this kinda field was pretty simple, in default value I just added "parent=page.province" (same as I mentioned in the post above).

  • Like 2

Share this post


Link to post
Share on other sites

thank you bwakad, that is great!

i thought this would be the first thing that i do NOT find using google, but fortunately you explained it well :) is this still only a dev branch feature? otherwise it should find it's way into the docs: https://processwire.com/api/selectors/inputfield-dependencies/ !

it took me quite a while searching for this solution and also the topic title of this thread is not the best  :rolleyes:

Share this post


Link to post
Share on other sites

On my new development I now use this tree structure:

Parentpage - countries

childpage hold "country"-name

childpage of country-name hold province-name

childpage of province-name hold city-name

country field, 1st select, for country value = normal parent select > "countries"

province field, 2nd select, for province name OF country selected value = custom selector > "parent=page.country'

city field, 3rth select, for city name OF province selected value = custom selector > "parent=page.province'.

Works great!

  • Like 4

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.

×
×
  • Create New...