简体   繁体   English

使用自定义itemView yii2将小部件放置在自定义小部件内

[英]Placing a widget inside a custom widget with custom itemView yii2

I want to create a custom widget named 'IssueList' which will extend 'ListView'. 我想创建一个名为“ IssueList”的自定义小部件,该小部件将扩展“ ListView”。

<?php
namespace frontend\components;

use yii\base\Widget;
use yii\widgets\ListView;

class IssueList extends Widget{
public $dataProvider;


public function init(){
    parent::init();
}
public function run(){
    return ListView::widget([
        'dataProvider' => $this->dataProvider,
        'itemOptions' => [
            'class' => 'item issue-item'
        ],
        'options' => [
            'class' => 'issue_list'
        ],
        'itemView' => '_issueListView',
        'layout' => '{items}{pager}',
    ]);

}

}?>

However the ListView has a custom itemView. 但是,ListView具有自定义itemView。 When rendering the widget I get this error 渲染小部件时出现此错误

The view file does not exist: 视图文件不存在:

/var/www/clients/client1/web1/frontend/views/comments/_issueListView.php /var/www/clients/client1/web1/frontend/views/comments/_issueListView.php

Its obviously looking in the wrong directory, how do I change this? 它显然在错误的目录中查找,该如何更改?

itemView is passed to yii\\base\\View render() which is responsible for rendering view. itemView传递给yii \\ base \\ View render() ,负责渲染视图。 So you can change view path how you want, using aliases if needed: 因此,您可以根据需要使用别名来更改视图路径:

Renders a view. 渲染视图。

The view to be rendered can be specified in one of the following formats: 可以用以下格式之一指定要渲染的视图:

  • path alias (eg "@app/views/site/index"); 路径别名(例如“ @ app / views / site / index”);
  • absolute path within application (eg "//site/index"): the view name starts with double slashes. 应用程序内的绝对路径(例如“ // site / index”):视图名称以双斜杠开头。 The actual view file will be looked for under the view path of the application. 实际的视图文件将在应用程序的视图路径下查找。
  • absolute path within current module (eg "/site/index"): the view name starts with a single slash. 当前模块内的绝对路径(例如“ / site / index”):视图名称以单个斜杠开头。 The actual view file will be looked for under the view path of the current module. 实际的视图文件将在当前模块的视图路径下查找。
  • relative view (eg "index"): the view name does not start with @ or /. 相对视图(例如“索引”):视图名称不以@或/开头。 The corresponding view file will be looked for under the view path of the view $context. 相应的视图文件将在视图$ context的视图路径下查找。 If $context is not given, it will be looked for under the directory containing the view currently being rendered (ie, this happens when rendering a view within another view). 如果未给出$ context,它将在包含当前正在渲染的视图的目录下查找(即,在另一个视图内渲染视图时会发生这种情况)。

If you want it to be more dynamic, you can pass closure to itemView with the following signature: 如果您希望它更具动态性,则可以使用以下签名将闭包传递给itemView

function ($model, $key, $index, $widget) {
    ...
}

See docs for itemView here . 在此处查看有关itemView文档。

Besides the API Documentation, rendering views and specifying paths is described in official docs in Views (Rendering Views) section. 除了API文档外,“ 视图(渲染视图)”部分的官方文档中还介绍了渲染视图和指定路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM