Jump to content

Fieldtype: Select (AKA Drop Down)


Hani

Recommended Posts

I did have need for a few things like this and it did take while to wrap my head around, but as long as the benefits are explained it's not a problem.

I would put it into context and explain that if you added age ranges (assuming you were collecting user stats during sugnuo or doing a quiz or something like that) then you can use this with selectors to pull simple stats.

The benefits aren't always immediately obvious as you might not have need it using such a simple field with selectors at the time you create the field, but the flexibility it offers if you do need to do something like that is amazing.

Link to comment
Share on other sites

  • 3 months later...

Hey Hani, I tried your select field module, as I need just a drop down with three options and I don't want to setup a Page field with three pages for a case like this. Setup worked out fine. But the value entered on page edit isn't saved to the fields db table. Any idea why?

  • Like 1
Link to comment
Share on other sites

  • 2 months later...
  • 4 weeks later...

I've this output

<li class='Inputfield InputfieldSelect Inputfield_auto_marca ui-widget InputfieldStateRequired' id='wrap_Inputfield_auto_marca'>

					<label class='ui-widget-header' for='Inputfield_auto_marca'>Marca</label>
					<div class='ui-widget-content'>

						<p class='description'>Marca</p>
						<select id="Inputfield_auto_marca" class="required" name="auto_marca">
							<option selected='selected'  value=''></option>
							<option  value='fiat'></option>
							<option  value='bmw'></option>
						</select>
					</div>
				</li>

why  <option value='fiat'></option> and not <option value='fiat'>fiat</option>... I've not change files of your module... where's the problem? thanks

EDIT -> corrected file InputfieldSelect.module

your version

$out .= "\n\t<option$selected $attrs value='" . htmlspecialchars($value, ENT_QUOTES, "UTF-8") . "'>" . $this->entityEncode($label) . "</option>";

my version

$out .= "\n\t<option$selected $attrs value='" . htmlspecialchars($value, ENT_QUOTES, "UTF-8") . "'>" . htmlspecialchars($value, ENT_QUOTES, "UTF-8") . "</option>";

why this error? Can be a server problem with "$this->entityEncode($label)"?

Edited by buothz
Link to comment
Share on other sites

  • 6 months later...
Hi Hani - thanks for this module, comes in handy once in a while.. One problem: seems that if you don't use a pair, the $label is null and then you have no labels in the dropdown.

i changed line 55 to this:

$label = $value;

and it works, not sure why or how setting the label to null works, i
guess the select field is supposed to handle that, but it isn't doing it

also i noticed that the database was showing \r\n and not just \n so i changed line 50 to this;

$options = explode("\r\n",$field->select_options);
Link to comment
Share on other sites

I noticed this too, that it doesn't work when no key is set only label. Not sure about the \r\n, I think that's system dependence.

I also noticed that if you enter 0 as a key (0:=option) it will not work with querying with selectors, as the data field is a text field and not a integer. YOu have to start with 1:=option to make it work.

Link to comment
Share on other sites

  • 1 month later...

I don't think there is a simple PW way to do this like $page->my_select->title.

This is ugly, but works:

foreach(explode("\n",$page->fields->my_select->select_options) as $option) {
    $valueLabel = explode(':=',$option);
    $value[$valueLabel[0]] = trim($valueLabel[1]);
}

echo $value[$page->my_select];
  • Like 2
Link to comment
Share on other sites

  • 1 month later...

I've just noticed something about this module. If you set the input required field to "Yes" and then try to save a page without selecting an option, it comes back with an error saying it's required..but all the values on the rest of the page still save. Is this a bug with this module or across the board?

Link to comment
Share on other sites

If a page is unpublished, PW won't let you publish it until you can save it error free (meeting required fields, etc.). If the page is already published, then ProcessWire is not going to prevent you from saving it, but will warn you that the field is required. 

Link to comment
Share on other sites

  • 3 months later...

After reading this topic I have some questions, I am fairly new to processwire and still trying to bend my head around it:

I need a select field, large quantity of options, able to change it's values in admin, reflect the changed in front output after a change.

In this topic it is recommended to use page field. Some say make pages - but that would mean I have to create a page for every option - and I need at least 30. Or are there other things available?

For example:

brancheA (option/value) - and clicking a link to BrancheA - would display a (search) page with list of items under that branche

etc.

Link to comment
Share on other sites

Pages are the preferred way to do this and 30 pages is absolutely not a problem. You'll run into usability issues with select field itself much sooner than any kind of problem related to number of pages behind the scenes.. :)

  • Like 1
Link to comment
Share on other sites

Yes, I was also reading a tutorial joss wrote earlier, they are almost the same.

But then it confused me to:

Do I need to add a file in the templates folder on the server, OR add a file in the admin part under templates?

Then it says 'blank template' but I really could not tell what is meant by that...

As for the video page field: I followed those steps in the video, but how to display in the .php template file? I looked into the cheat sheet but did not find which is outputting what?

Link to comment
Share on other sites

When it comes to create a select of any sort, you only need a template file if you will need to display those pages as actual pages, if you see what I mean.

What I tend to do is create a template that will be used as a parent (no file, just a blank template), and then create a template that you will use for the actual options. A lot of the time you can just have the title field, but if you need a value that is different from the title or you need more fields for other reasons, just add them.

With the parent template, set the family so that it can have children and define the template that will be used for those children. 

Create a page with the parent template that you want as the parent for your select. Then create some children - it will automatically choose the template you set as the child template. Since the parent template does not have a template file, it will not show up on any menu or similar searches. If you want that to be a bit more obvious on your page tree, set it as hidden as well.

Now you can create a page field and that will be your selects. You can even set the page field so that it can create more options - very useful for things like tags.

This sounds long winded, but actually, it takes just a few minutes :)

Link to comment
Share on other sites

I understand what you are trying to tell me, but still it is not very clear:

1. I made a new template from setup>templates>addnew

2. I added those two fields as suggested in the wiki - the normal title field and a text field called select_value

3. I created a top page - Selects

and here is where I get confused: in this page under settings I see template* name-of-template (from step 1)...

Now, what/where is the blank template?

I really feel stupid now. lol

Link to comment
Share on other sites

The parent page also needs its own template - you can create one and call it anything you like, I just called mine blank template to make it clear it didn't have any additional fields.

It needs a template so you can, for instance, set it to use a specific template for any children.

So, once you create a template for the parent, create a page with that template - you will only need the one page, obviously.

Then  go an add some children to this parent page using the template you created for your selects.

When you create your page field, you will then select the parent page as, umm, the parent page! (Clever, huh?)

Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...

I was in need of something like this tonight and I'm glad i found it.

Imagine my 'sadface.gif' when it did not work as expected. Maybe it's just that I don't understand it?

So on the Details page of my select option.

I put:

apple:=apple
banana:=banana
cherry:=cherry

And it works as expected.

But if I do this:
apple
banana
cherry

The dropdown is blank in my Page.

First I downloaded via the ModulesManager. Then I cloned frmo github. They are both the same version.

Link to comment
Share on other sites

So Im not quite sure what I'm doing wrong here but Im having a hard time getting a value to show up. I have 4 options:

1:=1 block

2:=2 blocks

3:=3 blocks

4:=4 blocks

now if I save the value in the page to say option 2 or 2 blocks when I choose $page->itemGridSizer I recieve no output. Is that the correct syntax?

edit: I jsut treid to set it to 

1

2

3

4

and still no dice, is this a bug or just something I'm doing wrong?

**final edit**

After some serious tinkering I coaxed my code into working. It turned out that for some reason the included code did not have access to the field, and I just needed to pass my function the variable. php noob mistake :) sorry for the junk

On a side note this plugin is awesome!! I needed something that would allow me to let the client choose one of four bootstrap layouts to use with my masonry javascript and it really blew em away! I love processwire, and I thank you for making such a useful tool.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...