select options for template field

Kai Wernicke's Avatar

Kai Wernicke

11 Jan, 2011 10:32 AM via web

Hi!

I'm wondering how i could build a select out of template field data?! I know how to access the selected fields for a given item but how do i access all possible values to make a list out of them?

Thanks, Kai

  1. Support Staff 2 Posted by Steve Smith on 12 Jan, 2011 03:56 PM

    Steve Smith's Avatar

    Are you looking to build a select box on the front side of your website? There's currently no way to get a list of the 'options' for a select in the template language. What would you use this for?

  2. 3 Posted by Kai Wernicke on 12 Jan, 2011 04:02 PM

    Kai Wernicke's Avatar

    I want to add some "flags" to a persons profile, like city and profession. Then i want to display matching persons in a list based on selected criteria.

    So i want to insert selected options as a class to the li and be able to display all avaiable options in the select box so that i can trigger displaying of li's via jquery.

    Thanks, kai

  3. 4 Posted by Kai Wernicke on 14 Jan, 2011 09:23 AM

    Kai Wernicke's Avatar

    Anyone?

    Let me clarify: I have a page. Below this page are subpages containing employees of my customer. Each emploee has some attributes like city, profession etc.

    Now i want to display a list of all employees and filter this list if an item from a selectbox ist selected. For example i want to display only employees working in one city...

    I know how to build the list and flag every

    • with classes based on selected criteria.

    But to build a jquery script tro filter this list i need an array of all available options.

    Thanks, Kai

  4. 5 Posted by John Nunemaker on 25 Jan, 2011 02:41 PM

    John Nunemaker's Avatar

    Kai,
    There is not a way to do what you are doing in themes. If you prefix the classes, you could easily just loop through the items with jquery and collect all the unique prefixed class names. Does that make sense?

  5. 6 Posted by Kai Wernicke on 26 Jan, 2011 09:05 AM

    Kai Wernicke's Avatar

    John,

    Well, yes. Sort of :)

    That's what i want to do but how do i access these options to use as a class prefix?
    As far as i have seen it i can only access the options of the given item but not all available ones...

    Do i have to loop over all items and build an array of options for my select list and if so how?

    Thanks, Kai

  6. 7 Posted by John Nunemaker on 26 Jan, 2011 02:26 PM

    John Nunemaker's Avatar

    You are correct, there is not access to a template's field data. The only way to access the data is through the item.

    What I was trying to explain is this. Lets say you have Mishawaka, Elkhart and Holland as options for the city select. When you render the list of people, you can add the city their city as a data attribute. Something like this:

    <div class="person" data-city="{{ item.data.city }}">
     ...
    </div>
    

    Then, you can use jquery to map all the cities uniquely into an array like this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
      <title>example</title>
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
      <script type="text/javascript">
        $(document).ready(function() {
          var cities = $.unique($('div.person').map(function() {
            return $(this).attr('data-city');
          }));
    
          console.log(cities)
        });
      </script>
    </head>
    <body>
      <div class="person" data-city="Mishawaka">
        ...
      </div>
    
      <div class="person" data-city="Elkhart">
        ...
      </div>
    
      <div class="person" data-city="Mishawaka">
        ...
      </div>
    
      <div class="person" data-city="Holland">
        ...
      </div>
    </body>
    </html>
    

    Does that better explain what I was thinking?

  7. 8 Posted by Kai Wernicke on 26 Jan, 2011 04:37 PM

    Kai Wernicke's Avatar

    Hi John!

    Thats it! Thanks a lot!!

    Cheers, Kai

  8. Kai Wernicke closed this discussion on 26 Jan, 2011 04:37 PM.

  9. 9 Posted by John Nunemaker on 26 Jan, 2011 04:39 PM

    John Nunemaker's Avatar

    No problem. So glad I could help!

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

17 May, 2012 10:45 PM
20 Mar, 2012 05:05 PM
26 Apr, 2012 01:40 AM
07 Mar, 2012 04:42 PM