天黑请闭眼 2017-06-13 12:05:30 2398次浏览 3条评论 0 0 0

参考网站内“白狼栈”大神的文章。不多说,直接上代码:

protected function isItemActive($item)
{
    if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
        $route = $item['url'][0];
        if ($route[0] !== '/' && Yii::$app->controller) {
            $route = ltrim(Yii::$app->controller->module->getUniqueId() . '/' . $route, '/');
        }
        $route = ltrim($route,'/');
        $route = explode('/', $route);
		$path = explode('/', $this->route);
        if ($route[1] != $path[1] && $route !== $this->noDefaultRoute && $route !== $this->noDefaultAction) {
            return false;
        }
        return true;
    }
    return false;
}
  • 评论于 2017-07-26 17:20 举报

    兄弟你这个再上个效果图就更好了

  • 评论于 2018-01-26 11:34 举报

    放到我的代码里有module的 /module/controller/action路径没问题,如果是没有module的/controller/action路径就只能是全等的时候才高亮。

    我增加了一段 如果当前path是2个元素 就是没有module的,比较下标为0也就是第一个元素,如果是3个元素就是有module的,比较下标为1也就是第二个元素

    $route = ltrim($route,'/');
    $route = explode('/', $route);
    $path = explode('/', $this->route);
    /*下面是增加的部分start*/
    $pathCount=count($path);
    if ($pathCount == 2 && $route[0] != $path[0] && $route !== $this->noDefaultRoute && $route !== $this->noDefaultAction) {
        return false;
    }
    /*上面是增加的部分end*/
    //下面要判断$pathCount==3
    if ($pathCount == 3 && $route[1] != $path[1] && $route !== $this->noDefaultRoute && $route !== $this->noDefaultAction) {
        return false;
    }
    
  • 评论于 2018-04-10 15:46 举报

    官方最新版本(2.6.0)已经解决了这个问题,可以达到子菜单级别的高亮:
    ` /**

     * Checks whether a menu item is active.
     * This is done by checking if [[route]] and [[params]] match that specified in the `url` option of the menu item.
     * When the `url` option of a menu item is specified in terms of an array, its first element is treated
     * as the route for the item and the rest of the elements are the associated parameters.
     * Only when its route and parameters match [[route]] and [[params]], respectively, will a menu item
     * be considered active.
     * @param array $item the menu item to be checked
     * @return boolean whether the menu item is active
     */
    protected function isItemActive($item)
    {
        if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
            $route = $item['url'][0];
            if ($route[0] !== '/' && Yii::$app->controller) {
                $route = ltrim(Yii::$app->controller->module->getUniqueId() . '/' . $route, '/');
            }
            $route = ltrim($route, '/');
            if ($route != $this->route && $route !== $this->noDefaultRoute && $route !== $this->noDefaultAction) {
                return false;
            }
            unset($item['url']['#']);
            if (count($item['url']) > 1) {
                foreach (array_splice($item['url'], 1) as $name => $value) {
                    if ($value !== null && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
                        return false;
                    }
                }
            }
            return true;
        }
        return false;
    }`
    
您需要登录后才可以评论。登录 | 立即注册