0
votes

i am wondering if there is a way in TYPOSCRIPT to count the childrens in a menu.

The output should be something like:

<ul class="ebene1" data-elements="4">
    <li><a href="element1">Element 1</a></li>
    <li><a href="element1">Element 2</a></li>
    <li><a href="element1">Element 2</a></li>
    <li><a href="element1">Element 2</a></li>
</ul> 

My Code looks like this:

script.NAV = HMENU
script.NAV {
    special = directory
    special.value = 6

    1 = TMENU
    1 {
        expAll = 1
        noBlur = 1

        wrap = <ul class="menu main" id="nav-main">|</ul>

        NO = 1
        NO.wrapItemAndSub   = <li class="first element1">|</li> |*| <li class="element1">|</li> |*| <li class="last element1">|</li>

        CUR = 1
        CUR.wrapItemAndSub  = <li class="first element1 current">|</li> |*| <li class="element1 current">|</li> |*| <li class="last element1 current">|</li>
        ...

Thanks, any help is really appreciated.

3
Just curious. What is requirement of this? Is that for some Javascript or for other purposes? - biesior
Hi, no but I solved it with javascript in the meantime. :) I needed this because of a table layout and i wanted to define the width of the children depending of the number of element. So for example if there 5 elements, each has a 20% width. - lufi

3 Answers

2
votes

Use the register {register:count_MENUOBJ} like this should solve your problem.

1 = TMENU
1 {
    NO = 1
    NO {
        wrapItemAndSub.insertData = 1
        wrapItemAndSub = <li class="element-{register:count_MENUOBJ}">|</li>
    }
}
1
votes

You need the stdWrap function numrows. There you can add any table together with a select statement. Here is an example:

lib.countmenu = COA
lib.countmenu {
  10 = HMENU
  10.1 = TMENU
  10.1 {
    wrap = <ul>|</ul>
    NO {
      stdWrap.cObject = COA
      stdWrap.cObject {
        30 = TEXT
        30.numRows.table = pages
        30.numRows.select.pidInList.field = uid
        30.numRows.select.where = nav_hide!=1 AND doktype!=5 AND doktype!=6
        30.dataWrap = {field:title} [|]
      }
      wrapItemAndSub = <li>|</li>
    }
  }
}
1
votes

Actually although that may be done as @Marcus showed (I believe that snippet works, didn't check) it is probably faster approach to us JavaScript for this; as easy as:

<ul class="ebene1">
    <li><a href="element-1">Element 1</a></li>
    <li><a href="element-2">Element 2</a></li>
    <li><a href="element-3">Element 3</a></li>
    <li><a href="element-x">Element X</a></li>
</ul>

<script>$('.ebene1').attr('data-elements', $('.menu > li').length);</script>

And CSS like:

.ebene1[data-elements="4"] {
    background-color: yellow;
}

On the other hand...

If you have dynamic menu with unknown number of items, instead adding many CSS declarations like,

.ebene1[data-elements="3"] li {width: 33.333%;}
.ebene1[data-elements="4"] li {width: 25%;}
.ebene1[data-elements="5"] li {width: 20%;}

Avoid adding the data- attribute via JS and just change the width by calculating the correct value:

$('.ebene1 > li').css({width: 100 / $('.ebene1 > li').length + '%'});