1

I would like to generate ID's for an HTML list. The list is generated dynamically from the database. I cant use a for loop or the list.zipWithIndex function because my logic contains a few loops for the generation of the list already, in which the counter needs to be incremented too. I also tried it with the defining function, but its not allowed to reasign values like this: @{id = id + 1}

Whats the best way to accomplish the generation of Id's?

Thats part of the template (uniqueId needs to be replaced with an integer):

  <div id="tree">
    <ul>
        <li id="uniqueId">
            <a class="dashboard" href="/">Dashboard</a>
        </li>
        <li id="uniqueId">
            <b>Products</b>
            <ul id="uniqueId">
            @for(cat <- Application.allCategories()) { 
              <li id="uniqueId">
                <a class="name" href="@routes.Categories.getd(cat.id).url">@cat.name</a>
              <ul>
                @for(prod <- Application.allProducts()) {
                <li id="uniqueId">
                    <a class="name" href="@routes.Product.getById(prod.id).url">@prod.name</a>
                </li>   
@*more code and the closing tags...*@
Jan P
  • 45
  • 10

1 Answers1

3

Use just ... object's id prefixed to make it unique, example for first listing:

@for(cat <- Application.allCategories()) { 
      <li id="cat_@cat.id">

for second:

@for(prod <- Application.allProducts()) {
      <li id="prod_@prod.id">

or if the same product can be displayed in several categories prefix it with cat.id as well:

@for(cat <- Application.allCategories()) { 
    <li id="cat_@cat.id">
    @for(prod <- Application.allProducts()) {
        <li id="prod_@(cat.id)_@(prod.id)">
biesior
  • 55,576
  • 10
  • 125
  • 182
  • thanks dont know why i didnt think about that ;-) putting cat_ or prod_ before each id sounds good! – Jan P Sep 13 '13 at 06:36