2017-01-04 16:17:57 2462次浏览 0条回答 0 悬赏 40 金钱

使用widget遍历出了导航数据,想给他加一个target不知道怎么做

class MenuItemWidget extends \yii\base\Widget
{
	public $menuType = false;
	public $options = [];
	private $menuItem = [];

	public function init()
	{
		$menuModels = MenuItem::find();
		if (!empty($this->menuType)) {
			$menuModels->where(['type_id' => $this->menuType]);
		}
		$this->menuItem = $menuModels->orderBy('weight ASC')->all();
	}

	public function run()
	{
		parent::run();
		\app\assets\smartmenus\SmartMenusAsset::register($this->view);

		return Nav::widget([
			'options' => $this->options,
			'items' => $this->makeTreeMenuItem($this->menuItem)
		]);
	}

	private static function makeTreeMenuItem($items, $currentElementId = null, $keyFieldName = 'id', $labelFieldName = 'label', $parentFieldName = 'parent_id', $parentKeyName = 'items')
	{
		if (empty($items)) return [];
		$return = [];

		foreach ($items as $item) {

			if ($item->getAttribute($parentFieldName) == $currentElementId) {

				$router = json_decode($item->url, true);

				if (empty($router)) {
					$router = empty($item->url) ? '#' : $item->url;
					$router = Url::isRelative($router) ? [$router] : $router;
				}

				$url = Url::to($router);
				$_items = self::makeTreeMenuItem($items, $item->getAttribute($keyFieldName), $keyFieldName, $labelFieldName, $parentFieldName, $parentKeyName);
				$_item = ['label' => $item->getAttribute($labelFieldName), 'url' => $url, 'target' => '_blank', 'dropDownOptions' => ['class' => 'dropdown-menu']];
				$_item = !empty($_items) ? array_merge($_item, [$parentKeyName => $_items]) : $_item;
				$return[] = $_item;
			}
		}
		return $return;

	}
}
    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
飒雅
助理

飒雅 北京

注册时间:2016-06-12
最后登录:2019-02-25
在线时长:11小时38分
  • 粉丝6
  • 金钱5
  • 威望0
  • 积分115

热门问题