Update output-data-widgets.md

This commit is contained in:
yolanda
2018-11-17 19:51:48 +08:00
committed by GitHub
parent ebb8a0c487
commit 5efb2482b0

View File

@ -37,12 +37,12 @@ echo DetailView::widget([
]);
```
Remember that unlike [[yii\widgets\GridView|GridView]] which processes a set of models,
[[yii\widgets\DetailView|DetailView]] processes just one. So most of the time there is no need for using closure since
`$model` is the only one model for display and available in view as a variable.
请记住,与处理一组模型的 [[yii \ widgets \ GridView | GridView]] 不同,[[yii\widgets\DetailView|DetailView]] 只处理一个。
因为 `$ model` 是唯一一个用于显示的模型,并且可以作为变量在视图中使用。
但是有些情况下可以使闭包有用。例如指定了 `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
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
argument. In the second column definition, `date` corresponds to [[\yii\i18n\Formatter::asDate()]]. The value of the
column is, again, passed as the first argument while 'php:Y-m-d' is used as the second argument value.
在上述代码中, `text` 对应于 [[\yii\i18n\Formatter::asText()]].列的值作为第一个参数传递. 在第二列的定义中, `date` 对应于 [[\yii\i18n\Formatter::asDate()]]. 该列的值再次作为第一个参数传递
同时 'php:Y-m-d' 被用作第二个参数.
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
API documentation for [[yii\grid\GridView::columns|columns]].
有关可用格式化程序的列表,请参阅 [section about Data Formatting](output-formatting.md).
Use [[yii\grid\DataColumn::filter|filter]] and [[yii\grid\DataColumn::filterInputOptions|filterInputOptions]] to
control HTML for the filter input.
对于配置数据列还有一种快捷方式格式请参阅API文档 [[yii\grid\GridView::columns|columns]].
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.
By default the label will be populated from data model. For more details see [[yii\grid\DataColumn::getHeaderCellLabel]].
使用 [[yii\grid\DataColumn::filter|filter]] 和 [[yii\grid\DataColumn::filterInputOptions|filterInputOptions]] 去控制过滤器输入的HTML
默认情况下, 列的头部有 [[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::createUrl()]] 是一样的。
假如这个属性没有设置按钮的URL将使用 [[yii\grid\ActionColumn::createUrl()]] 来创建。
- [[yii\grid\ActionColumn::visibleButtons|visibleButtons]] is an array of visibility conditions for each button.
The array keys are the button names (without curly brackets), and the values are the boolean true/false or the
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:
- [[yii\grid\ActionColumn::visibleButtons|visibleButtons]] 是控制每个按钮可见性条件的数组。
数组键是按钮名称 (没有大括号), 值是布尔值true / false或匿名函数。
如果在数组中没有指定按钮名称,将会按照默认的来显示。
回调必须像如下这样来使用:
```php
function ($model, $key, $index) {
@ -319,7 +319,7 @@ echo GridView::widget([
}
```
Or you can pass a boolean value:
或者你可以传递一个布尔值:
```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)
> to learn how to build filtering query.
> Tip: 请参阅 [Query Builder](db-query-builder.md) 尤其是 [Filter Conditions](db-query-builder.md#filter-conditions)
> 去学习如何构建过滤查询。
你可以在控制器中使用如下方法为网格视图获取数据提供者:
@ -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
@ -496,18 +496,18 @@ use yii\widgets\ActiveForm;
</div>
```
and include it in `index.php` view like so:
并将其包含在 `index.php` 视图中,如下所示:
```php
<?= $this->render('_search', ['model' => $searchModel]) ?>
```
> Note: if you use Gii to generate CRUD code, the separate filter form (`_search.php`) is generated by default,
but is commented in `index.php` view. Uncomment it and it's ready to use!
> Note: 如果使用Gii生成CRUD代码 默认情况下会生成单独的过滤器表单(`_search.php`),但是在 `index.php` 视图中已经被注释了。取消注释就可以用了!
当您需要按字段过滤时单独的过滤器表单很有用这些字段不会在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
class PostSearch extends Post
@ -524,14 +524,14 @@ class PostSearch extends Post
}
```
Extend query conditions in the `search()` method like so:
在 `search()` 扩展查询条件的方法如下:
```php
$query->andFilterWhere(['>=', 'creation_date', $this->createdFrom])
->andFilterWhere(['<=', 'creation_date', $this->createdTo]);
```
And add the representative fields to the filter form:
并将具有代表信的字段添加到过滤器表单:
```php
<?= $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
page instead of reloading the entire page. You can use it to to update only the
[[yii\grid\GridView|GridView]] content when using filters.
[[yii\widgets\Pjax|Pjax]] 小部件允许你更新页面的一部分而不是整个页面。
[[yii\grid\GridView|GridView]] 使用 Pjax 的例子如下。
```php
use yii\widgets\Pjax;
@ -753,16 +752,18 @@ Pjax::begin([
Pjax::end();
```
Pjax also works for the links inside the [[yii\widgets\Pjax|Pjax]] widget and
for the links specified by [[yii\widgets\Pjax::$linkSelector|Pjax::$linkSelector]].
But this might be a problem for the links of an [[yii\grid\ActionColumn|ActionColumn]].
To prevent this, add the HTML attribute `data-pjax="0"` to the links when you edit
the [[yii\grid\ActionColumn::$buttons|ActionColumn::$buttons]] property.
Pjax 也适用于 [[yii\widgets\Pjax|Pjax]] 小部件之间的链接以及 [[yii\widgets\Pjax::$linkSelector|Pjax::$linkSelector]] 指定的链接。
但是这可能是 [[yii\grid\ActionColumn|ActionColumn]] 链接的问题。
要防止这种情况,请在编辑 [[yii \ grid \ ActionColumn :: $ buttons | ActionColumn :: $ buttons]] 属性时将HTML属性 `data-pjax ="0"` 添加到链接中。
#### 在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
yii gii/crud --controllerClass="backend\\controllers\PostController" \
@ -770,10 +771,10 @@ yii gii/crud --controllerClass="backend\\controllers\PostController" \
--enablePjax=1
```
Which generates a [[yii\widgets\Pjax|Pjax]] widget wrapping the
[[yii\grid\GridView|GridView]] or [[yii\widgets\ListView|ListView]] widgets.
这会生成一个由 [[yii\widgets\Pjax|Pjax]] 小部件包含的 [[yii\grid\GridView|GridView]] 或者 [[yii\widgets\ListView|ListView]] 。
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.