widget使用 [ 2.0 版本 ]
使用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;
}
}
共 0 个回答
没有找到数据。
飒雅 北京
注册时间:2016-06-12
最后登录:2019-02-25
在线时长:11小时38分
最后登录:2019-02-25
在线时长:11小时38分
- 粉丝6
- 金钱5
- 威望0
- 积分115