5

我目前正在创建一个 joomla 菜单模块,但我遇到了一些问题..

我正在尝试将子菜单项分成 3 列,目前我正在使用这个:

$counter = 0;

if($item->level == 2):
    $counter += count($item);
endif;

if($item->level == 1):
    $counter = 0;
endif;

if($counter%3 == 0 && $item->level == 2){
        echo '</ul><ul class="col-lg-3">';
    }

但这只是将它们分组为 3

这是整个 default.php:

<?php

// No direct access
defined('_JEXEC') or die;

// Note. It is important to remove spaces between elements.
$counter = 0;

?>

<ul class="nav navbar-nav <?php echo $class_sfx; ?>  nav-mega"<?php
$tag = '';
if ($params->get('tag_id') != null)
{
    $tag = $params->get('tag_id') . '';
    echo ' id="' . $tag . '"';
}
?>>

<?php
    foreach ($list as $i => &$item) {

        if($item->level == 2):
            $counter += count($item);
        endif;

        if($item->level == 1):
            $counter = 0;
        endif;

        $class = 'item-' . $item->id;
        if ($item->id == $active_id) {
            $class .= ' current';
        }

        if (in_array($item->id, $path)){
            $class .= ' active';
        }elseif ($item->type == 'alias'){
            $aliasToId = $item->params->get('aliasoptions');
            if (count($path) > 0 && $aliasToId == $path[count($path) - 1]) {
                $class .= ' active';
            }elseif (in_array($aliasToId, $path)){
                $class .= ' alias-parent-active';
            }
        }

        if ($item->deeper){
            $class .= ' deeper dropdown';
        }

        if ($item->parent){
            $class .= ' parent';
        }

        if (!empty($class)){
            $class = ' class="' . trim($class) . '"';
        }

        echo '<li' . $class . '>';

        // Render the menu item.
        switch ($item->type){
            case 'separator':
            case 'url':
            case 'component':
                require JModuleHelper::getLayoutPath('mod_blogmenu', 'default_' . $item->type);
            break;

            default:
                require JModuleHelper::getLayoutPath('mod_blogmenu', 'default_url');
            break;
        }

        // The next item is deeper.
        if($counter%3 == 0 && $item->level == 2){
            echo '</ul><ul class="col-lg-3">';
        }

        if ($item->deeper){
            echo '<div class="dropdown-menu mega-dropdown">';
            echo '<div class="mega-image col-lg-3 thumbnail visible-md visible-lg"><img src="'.$item->menu_image.'" /></div>';
            echo '<ul class="col-lg-3">';
        }
                // The next item is shallower.
        elseif ($item->shallower){

            echo str_repeat('</ul><div class="mega-caption"></div></div>', $item->level_diff);
        }
                // The next item is on the same level.
        else {
            //echo '</li>';
        }
    }

?>
</ul>

对不起很多代码;我仍在努力学习 PHP 并尝试了解 Joomla 的做事方式,这对我来说并不容易。

4

2 回答 2

1

你能把它放到 CSS 列中吗?

<!DOCTYPE html>
<html>
<head>

<style>
div.columns {
column-width: auto;
-moz-column-width: auto;
-webkit-column-width: auto;
column-count: 3;
-moz-column-count: 3;
-webkit-column-count: 3;
column-gap: 1em;
-moz-column-gap: 1em;
-webkit-column-gap: 1em;
}

.nosplit {
display: inline-block;
}
</style>

</head>

<body>

<div class="columns">
<ul>
<li>item01</li>
<li>item02</li>
<li>item03</li>
<li>item04</li>
<li class="nosplit">item05
    <ul>
    <li>item05a</li>
    <li>item05b</li>
    </ul>
    </li>
<li>item06</li>
<li>item07</li>
<li class="nosplit">item08
    <ul>
    <li>item08a</li>
    <li>item08b</li>
    <li>item08c</li>
    </ul>
    </li>
<li>item09</li>
<li>item10</li>
</ul>

</body>

</html>
于 2013-10-03T19:58:22.423 回答
1

在遍历所有菜单项之前,计算每个菜单项的所有子菜单项。(免责声明:我不了解 Joomla 菜单,但我希望 $item->parent->id 引用父项的 id。)

$submenuItemsTotals = array();
foreach ($list as $i => &$item) { 
   if ($item->level == 2) {
       if (!isset($submenuItemsTotal[$item->parent->id])) {
          $submenuItemsTotal[$item->parent->id] = 1;
       } else {
          $submenuItemsTotal[$item->parent->id]++;
       }
   }
}

$itemsPerColumn = array();
foreach ($submenuItemsTotals as $parentId => $submenuItemsTotal) {
   $itemsPerColumn[$parentId] = ceil($submenuItemsTotal / 3); 
}


// Here comes your existing code with a small change

foreach ($list as $i => &$item) { 
    [your code....]

    // The next item is deeper.
    if($item->level == 2 && ($counter % $itemsPerColumn[$item->parent->id]) == 0){
        echo '</ul><ul class="col-lg-3">';
    }

    [your code....]
}
于 2013-10-04T21:02:47.987 回答