jasonhinkle / phreeze

Phreeze Framework for PHP
http://phreeze.com/
GNU Lesser General Public License v2.1
377 stars 207 forks source link

Bootstrap select - dynamically pre select the default value #241

Closed TomasValenta closed 8 years ago

TomasValenta commented 8 years ago

Hello @jasonhinkle,

first thank you very much for your perfect work, it's very useful! I want to ask you for help with bootstrap select. I made this simple DropDownList in *.tpl.php:

<select id="type">
    <option value="1">Text 1</option>
    <option value="3">Text 2</option>
    <option value="7">Text 3</option>
</select>

but how to pre select the default value? The only working solution is mark one option with "selected" parameter, as is suggested here. But how to do this dynamically?

Something like with the checkboxes?

<input type="checkbox" id="popular" name="popular" value="true" <% if (item.get('popular') == 'true') { %>checked="checked"<% } %> /> Popular?
jasonhinkle commented 8 years ago

Yes you can do it that way & it should be fine.

For some example code, add an ENUM column type to your DB with, say, 3 values. Phreeze Builder will generate a dropdown for that, which you can look at as an example of how to do yours.

TomasValenta commented 8 years ago

Thank you very much @jasonhinkle. It's working!

<select id="genre" name="genre">
    <option value=""></option>
    <option value="0"<% if (item.get('genre')=='0') { %> selected="selected"<% } %>>Drama</option>
    <option value="1"<% if (item.get('genre')=='1') { %> selected="selected"<% } %>>Fantasy</option>
</select>

For 2-10 items is this method fine, but with 300 items? :)

jasonhinkle commented 8 years ago

I think with 300 it still probably would be fine, although that is a lot of options for a dropdown. you might look for some fancy component out there to do a better job.

another idea is you can remove the logic within each option tag, that way the dropdown will initially render with no selected value. Then, add some code to page.renderModelView that sets the selected item there. That way the logic will only run once. But, it's a little more work on your part.

I'd give it a try as-is and see, it just might be fine.

TomasValenta commented 8 years ago

Thank you @jasonhinkle, I'll try it, please give me a time ;)

xtrasmal commented 8 years ago

@TomasValenta Hi Tomas, please read the comments on the bottom here https://github.com/jasonhinkle/phreeze/issues/227