rxdazn
rxdazn

Reputation: 1390

How can I dynamically generate a multi-level dropdown menu with angular?

I have made a dropdown menu with multiple checkboxes using bootstrap (see http://jsfiddle.net/rxdazn/ryzJb/3/ ). This dropdown menu will introduce the possibility to plot multiple graphs ("measures") on the same chart + multiple options (log scale etc.).

A list of values (corresponding to "measures") will be stored in a json object. For now, I am just using a single select (users can only plot 1 chart, there are no graph type nor options):

<select ng-model="measure" ng-options="facet.path as facet.name for facet in station.facet_groups[0].facets"></select>

How can I best handle the fact that each measure type will have the same submenu?
Should I change my HTML? Should I dynamically generate values for <input> id attributes ?

<!-- graph type -->
<li class="dropdown-submenu"> <a href="#">Graph type</a>

    <ul class="dropdown-menu">
        <li>
            <input type="checkbox" id="line" name="graph" value="line">
            <label for="line">line</label>
        </li>
        <li>
            <input type="checkbox" id="dot" name="graph" value="dot">
            <label for="dot">dot</label>
        </li>
    </ul>

Upvotes: 2

Views: 3505

Answers (1)

Ye Liu
Ye Liu

Reputation: 8976

Since an id must be unique on a page, you definitely should dynamically generate them; plus, you might also want to generate the name attribute in input element, but this is totally up to your use case. Based on you fiddle, I think you can generate your menu like this:

<ul id="menu">
    <li class="dropdown-submenu" ng-repeat="facet in facets"> <a href="#">{{facet.name}}</a>

        <ul class="dropdown-menu">
            <li>
                <input type="checkbox" id="{{facet.name}}-line" name="{{facet.name}}-graph" value="line">
                <label for="line">line</label>
            </li>
            <li>
                <input type="checkbox" id="{{facet.name}}-dot" name="{{facet.name}}-graph" value="dot">
                <label for="dot">dot</label>
            </li>
        </ul>
    </li>
</ul>

I try to generate the id and name based on facet.name, you may want to change it to suit you needs.

Upvotes: 1

Related Questions