mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	Update output-data-widgets.md
This commit is contained in:
		@ -37,12 +37,12 @@ echo DetailView::widget([
 | 
				
			|||||||
]);
 | 
					]);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Remember that unlike [[yii\widgets\GridView|GridView]] which processes a set of models,
 | 
					请记住,与处理一组模型的 [[yii \ widgets \ GridView | GridView]] 不同,[[yii\widgets\DetailView|DetailView]] 只处理一个。
 | 
				
			||||||
[[yii\widgets\DetailView|DetailView]] processes just one. So most of the time there is no need for using closure since
 | 
					因为 `$ model` 是唯一一个用于显示的模型,并且可以作为变量在视图中使用。
 | 
				
			||||||
`$model` is the only one model for display and available in view as a variable.
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					但是有些情况下可以使闭包有用。例如指定了 `visible` ,并且你不想让 `value` 的结果为 `false`:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
However some cases can make using of closure useful. For example when `visible` is specified and you want to prevent
 | 
					 | 
				
			||||||
`value` calculations in case it evaluates to `false`:
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
echo DetailView::widget([
 | 
					echo DetailView::widget([
 | 
				
			||||||
@ -254,21 +254,21 @@ echo GridView::widget([
 | 
				
			|||||||
]);
 | 
					]);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
In the above, `text` corresponds to [[\yii\i18n\Formatter::asText()]]. The value of the column is passed as the first
 | 
					在上述代码中, `text` 对应于 [[\yii\i18n\Formatter::asText()]].列的值作为第一个参数传递. 在第二列的定义中, `date` 对应于 [[\yii\i18n\Formatter::asDate()]]. 该列的值再次作为第一个参数传递
 | 
				
			||||||
argument. In the second column definition, `date` corresponds to [[\yii\i18n\Formatter::asDate()]]. The value of the
 | 
					同时 'php:Y-m-d' 被用作第二个参数.
 | 
				
			||||||
column is, again, passed as the first argument while 'php:Y-m-d' is used as the second argument value.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
For a list of available formatters see the [section about Data Formatting](output-formatting.md).
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
For configuring data columns there is also a shortcut format which is described in the
 | 
					有关可用格式化程序的列表,请参阅 [section about Data Formatting](output-formatting.md).
 | 
				
			||||||
API documentation for [[yii\grid\GridView::columns|columns]].
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Use [[yii\grid\DataColumn::filter|filter]] and [[yii\grid\DataColumn::filterInputOptions|filterInputOptions]] to
 | 
					对于配置数据列,还有一种快捷方式格式,请参阅API文档 [[yii\grid\GridView::columns|columns]].
 | 
				
			||||||
control HTML for the filter input.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
By default, column headers are rendered by [[yii\data\Sort::link]]. It could be adjusted using [[yii\grid\Column::header]].
 | 
					
 | 
				
			||||||
To change header text you should set [[yii\grid\DataColumn::$label]] like in the example above. 
 | 
					使用 [[yii\grid\DataColumn::filter|filter]] 和 [[yii\grid\DataColumn::filterInputOptions|filterInputOptions]] 去控制过滤器输入的HTML
 | 
				
			||||||
By default the label will be populated from data model. For more details see [[yii\grid\DataColumn::getHeaderCellLabel]].
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					默认情况下, 列的头部有 [[yii\data\Sort::link]] 来呈现. 它还可以使用 [[yii\grid\Column::header]] 来调整.
 | 
				
			||||||
 | 
					要更改头部文本,您应该像上面的示例中那样设置 [[yii\grid\DataColumn::$label]] . 
 | 
				
			||||||
 | 
					默认情况下,标签应该从数据模型中填充. 更多细节请参阅 [[yii\grid\DataColumn::getHeaderCellLabel]].
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### 动作列 
 | 
					#### 动作列 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -308,10 +308,10 @@ echo GridView::widget([
 | 
				
			|||||||
- [[yii\grid\ActionColumn::urlCreator|urlCreator]] 是使用指定的模型信息来创建一个按钮URL的回调函数。
 | 
					- [[yii\grid\ActionColumn::urlCreator|urlCreator]] 是使用指定的模型信息来创建一个按钮URL的回调函数。
 | 
				
			||||||
  该回调的原型和 [[yii\grid\ActionColumn::createUrl()]] 是一样的。
 | 
					  该回调的原型和 [[yii\grid\ActionColumn::createUrl()]] 是一样的。
 | 
				
			||||||
  假如这个属性没有设置,按钮的URL将使用 [[yii\grid\ActionColumn::createUrl()]] 来创建。
 | 
					  假如这个属性没有设置,按钮的URL将使用 [[yii\grid\ActionColumn::createUrl()]] 来创建。
 | 
				
			||||||
- [[yii\grid\ActionColumn::visibleButtons|visibleButtons]] is an array of visibility conditions for each button.
 | 
					- [[yii\grid\ActionColumn::visibleButtons|visibleButtons]] 是控制每个按钮可见性条件的数组。
 | 
				
			||||||
  The array keys are the button names (without curly brackets), and the values are the boolean true/false or the
 | 
					  数组键是按钮名称 (没有大括号), 值是布尔值true / false或匿名函数。
 | 
				
			||||||
  anonymous function. When the button name is not specified in this array it will be shown by default.
 | 
					  如果在数组中没有指定按钮名称,将会按照默认的来显示。
 | 
				
			||||||
  The callbacks must use the following signature:
 | 
					  回调必须像如下这样来使用:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ```php
 | 
					  ```php
 | 
				
			||||||
  function ($model, $key, $index) {
 | 
					  function ($model, $key, $index) {
 | 
				
			||||||
@ -319,7 +319,7 @@ echo GridView::widget([
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  ```
 | 
					  ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Or you can pass a boolean value:
 | 
					  或者你可以传递一个布尔值:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  ```php
 | 
					  ```php
 | 
				
			||||||
  [
 | 
					  [
 | 
				
			||||||
@ -434,8 +434,8 @@ class PostSearch extends Post
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
> Tip: See [Query Builder](db-query-builder.md) and especially [Filter Conditions](db-query-builder.md#filter-conditions)
 | 
					> Tip: 请参阅 [Query Builder](db-query-builder.md) 尤其是 [Filter Conditions](db-query-builder.md#filter-conditions)
 | 
				
			||||||
> to learn how to build filtering query.
 | 
					> 去学习如何构建过滤查询。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
你可以在控制器中使用如下方法为网格视图获取数据提供者:
 | 
					你可以在控制器中使用如下方法为网格视图获取数据提供者:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -461,10 +461,10 @@ echo GridView::widget([
 | 
				
			|||||||
]);
 | 
					]);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Separate filter form
 | 
					### 单独过滤表单
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					大多数时候使用GridView标头过滤器就足够了,但是如果你需要一个单独的过滤器表单,你也可以很轻松的去添加。您可以使用以下内容创建部分视图 `_search.php`:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Most of the time using GridView header filters is enough, but in case you need a separate filter form,
 | 
					 | 
				
			||||||
you can easily add it as well. You can create partial view `_search.php` with the following contents:
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
<?php
 | 
					<?php
 | 
				
			||||||
@ -496,18 +496,18 @@ use yii\widgets\ActiveForm;
 | 
				
			|||||||
</div>
 | 
					</div>
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
and include it in `index.php` view like so:
 | 
					并将其包含在 `index.php` 视图中,如下所示:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
<?= $this->render('_search', ['model' => $searchModel]) ?>
 | 
					<?= $this->render('_search', ['model' => $searchModel]) ?>
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
> Note: if you use Gii to generate CRUD code, the separate filter form (`_search.php`) is generated by default,
 | 
					> Note: 如果使用Gii生成CRUD代码, 默认情况下会生成单独的过滤器表单(`_search.php`),但是在 `index.php` 视图中已经被注释了。取消注释就可以用了!
 | 
				
			||||||
but is commented in `index.php` view. Uncomment it and it's ready to use!
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					当您需要按字段过滤时,单独的过滤器表单很有用,这些字段不会在GridView中显示,也不适用于特殊筛选条件(如日期范围)。
 | 
				
			||||||
 | 
					对于按日期范围过滤,我们可以将非DB属性 `createdFrom` 和 `createdTo` 添加到搜索模型:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Separate filter form is useful when you need to filter by fields, that are not displayed in GridView
 | 
					 | 
				
			||||||
or for special filtering conditions, like date range. For filtering by date range we can add non DB attributes
 | 
					 | 
				
			||||||
`createdFrom` and `createdTo` to the search model:
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
class PostSearch extends Post
 | 
					class PostSearch extends Post
 | 
				
			||||||
@ -524,14 +524,14 @@ class PostSearch extends Post
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Extend query conditions in the `search()` method like so:
 | 
					在 `search()` 扩展查询条件的方法如下:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
$query->andFilterWhere(['>=', 'creation_date', $this->createdFrom])
 | 
					$query->andFilterWhere(['>=', 'creation_date', $this->createdFrom])
 | 
				
			||||||
      ->andFilterWhere(['<=', 'creation_date', $this->createdTo]);
 | 
					      ->andFilterWhere(['<=', 'creation_date', $this->createdTo]);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
And add the representative fields to the filter form:
 | 
					并将具有代表信的字段添加到过滤器表单:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
<?= $form->field($model, 'creationFrom') ?>
 | 
					<?= $form->field($model, 'creationFrom') ?>
 | 
				
			||||||
@ -734,11 +734,10 @@ echo GridView::widget([
 | 
				
			|||||||
]);
 | 
					]);
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Using GridView with Pjax
 | 
					### 在GridView使用Pjax
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The [[yii\widgets\Pjax|Pjax]] widget allows you to update a certain section of a
 | 
					[[yii\widgets\Pjax|Pjax]] 小部件允许你更新页面的一部分而不是整个页面。
 | 
				
			||||||
page instead of reloading the entire page. You can use it to to update only the
 | 
					[[yii\grid\GridView|GridView]] 使用 Pjax 的例子如下。
 | 
				
			||||||
[[yii\grid\GridView|GridView]] content when using filters.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
use yii\widgets\Pjax;
 | 
					use yii\widgets\Pjax;
 | 
				
			||||||
@ -753,16 +752,18 @@ Pjax::begin([
 | 
				
			|||||||
Pjax::end();
 | 
					Pjax::end();
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Pjax also works for the links inside the [[yii\widgets\Pjax|Pjax]] widget and
 | 
					Pjax 也适用于 [[yii\widgets\Pjax|Pjax]] 小部件之间的链接以及 [[yii\widgets\Pjax::$linkSelector|Pjax::$linkSelector]] 指定的链接。
 | 
				
			||||||
for the links specified by [[yii\widgets\Pjax::$linkSelector|Pjax::$linkSelector]].
 | 
					但是这可能是 [[yii\grid\ActionColumn|ActionColumn]] 链接的问题。
 | 
				
			||||||
But this might be a problem for the links of an [[yii\grid\ActionColumn|ActionColumn]].
 | 
					要防止这种情况,请在编辑 [[yii \ grid \ ActionColumn :: $ buttons | ActionColumn :: $ buttons]] 属性时将HTML属性 `data-pjax ="0"` 添加到链接中。
 | 
				
			||||||
To prevent this, add the HTML attribute `data-pjax="0"` to the links when you edit
 | 
					
 | 
				
			||||||
the [[yii\grid\ActionColumn::$buttons|ActionColumn::$buttons]] property.
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#### 在Gii中使用Pjax的GridView / ListView
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Since 2.0.5, [Gii](start-gii.md) 的CRUD生成器有一个 `$enablePjax` 选项,可以通过web界面或者命令行使用。
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#### GridView/ListView with Pjax in Gii
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Since 2.0.5, the CRUD generator of [Gii](start-gii.md) has an option called
 | 
					 | 
				
			||||||
`$enablePjax` that can be used via either web interface or command line.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
```php
 | 
					```php
 | 
				
			||||||
yii gii/crud --controllerClass="backend\\controllers\PostController" \
 | 
					yii gii/crud --controllerClass="backend\\controllers\PostController" \
 | 
				
			||||||
@ -770,10 +771,10 @@ yii gii/crud --controllerClass="backend\\controllers\PostController" \
 | 
				
			|||||||
  --enablePjax=1
 | 
					  --enablePjax=1
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Which generates a [[yii\widgets\Pjax|Pjax]] widget wrapping the
 | 
					这会生成一个由 [[yii\widgets\Pjax|Pjax]] 小部件包含的 [[yii\grid\GridView|GridView]] 或者 [[yii\widgets\ListView|ListView]] 。
 | 
				
			||||||
[[yii\grid\GridView|GridView]] or [[yii\widgets\ListView|ListView]] widgets.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Further reading
 | 
					
 | 
				
			||||||
 | 
					进一步阅读
 | 
				
			||||||
---------------
 | 
					---------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- [Rendering Data in Yii 2 with GridView and ListView](http://www.sitepoint.com/rendering-data-in-yii-2-with-gridview-and-listview/) by Arno Slatius.
 | 
					- [Rendering Data in Yii 2 with GridView and ListView](http://www.sitepoint.com/rendering-data-in-yii-2-with-gridview-and-listview/) by Arno Slatius.
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user