Commit 725ec9a9 by Alexander Makarov

More data widget docs

parent ea6f29dd
......@@ -29,23 +29,25 @@ Yii grid consists of a number of columns. Depending on column type and settings
These are defined in the columns part of GridView config like the following:
```php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
// A simple column defined by the data contained in $dataProvider.
// Data from model's column1 will be used.
'column1',
// More complex one.
array(
'class' => 'CDataColumn', // can be omitted, default
'name'=>'column1',
'value'=>function($data,$row){
return $data->name;
},
'type'=>'raw',
),
),
));
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// A simple column defined by the data contained in $dataProvider.
// Data from model's column1 will be used.
'id',
'username',
// More complex one.
[
'class' => 'DataColumn', // can be omitted, default
'name' => 'column1',
'value' => function ($data) {
return $data->name;
},
'type'=>'raw',
],
],
]);
```
Note: If columns part of config isn't specified, Yii tries to show all possible data provider model columns.
......@@ -53,6 +55,15 @@ Note: If columns part of config isn't specified, Yii tries to show all possible
### Column classes
#### Data column
#### Action column
#### Checkbox column
#### Serial column
TODO: rewrite these:
- https://github.com/samdark/a-guide-to-yii-grids-lists-and-data-providers/blob/master/grid-columns.md
......
......@@ -10,12 +10,121 @@ In Yii there are three built-in data providers: [[yii\data\ActiveDataProvider]],
Active data provider
--------------------
`ActiveDataProvider` provides data by performing DB queries using [[\yii\db\Query]] and [[\yii\db\ActiveQuery]].
The following is an example of using it to provide ActiveRecord instances:
```php
$provider = new ActiveDataProvider([
'query' => Post::find(),
'pagination' => [
'pageSize' => 20,
],
]);
// get the posts in the current page
$posts = $provider->getModels();
~~~
And the following example shows how to use ActiveDataProvider without ActiveRecord:
```php
$query = new Query;
$provider = new ActiveDataProvider([
'query' => $query->from('tbl_post'),
'pagination' => [
'pageSize' => 20,
],
]);
// get the posts in the current page
$posts = $provider->getModels();
```
Array data provider
-------------------
ArrayDataProvider implements a data provider based on a data array.
The [[allModels]] property contains all data models that may be sorted and/or paginated.
ArrayDataProvider will provide the data after sorting and/or pagination.
You may configure the [[sort]] and [[pagination]] properties to
customize the sorting and pagination behaviors.
Elements in the [[allModels]] array may be either objects (e.g. model objects)
or associative arrays (e.g. query results of DAO).
Make sure to set the [[key]] property to the name of the field that uniquely
identifies a data record or false if you do not have such a field.
Compared to `ActiveDataProvider`, `ArrayDataProvider` could be less efficient
because it needs to have [[allModels]] ready.
ArrayDataProvider may be used in the following way:
```php
$query = new Query;
$provider = new ArrayDataProvider([
'allModels' => $query->from('tbl_post')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
// get the posts in the current page
$posts = $provider->getModels();
```
> Note: if you want to use the sorting feature, you must configure the [[sort]] property
so that the provider knows which columns can be sorted.
SQL data provider
-----------------
SqlDataProvider implements a data provider based on a plain SQL statement. It provides data in terms of arrays, each
representing a row of query result.
Like other data providers, SqlDataProvider also supports sorting and pagination. It does so by modifying the given
[[sql]] statement with "ORDER BY" and "LIMIT" clauses. You may configure the [[sort]] and [[pagination]] properties to
customize sorting and pagination behaviors.
`SqlDataProvider` may be used in the following way:
```php
$count = Yii::$app->db->createCommand('
SELECT COUNT(*) FROM tbl_user WHERE status=:status
', [':status' => 1])->queryScalar();
$dataProvider = new SqlDataProvider([
'sql' => 'SELECTFROM tbl_user WHERE status=:status',
'params' => [':status' => 1],
'totalCount' => $count,
'sort' => [
'attributes' => [
'age',
'name' => [
'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
'default' => SORT_DESC,
'label' => 'Name',
],
],
],
'pagination' => [
'pageSize' => 20,
],
]);
// get the user records in the current page
$models = $dataProvider->getModels();
```
> Note: if you want to use the pagination feature, you must configure the [[totalCount]] property
to be the total number of rows (without pagination). And if you want to use the sorting feature,
you must configure the [[sort]] property so that the provider knows which columns can be sorted.
Implementing your own custom data provider
------------------------------------------
Data widgets
============
\ No newline at end of file
============
ListView
--------
DetailView
----------
DetailView displays the detail of a single data [[model]].
It is best used for displaying a model in a regular format (e.g. each model attribute is displayed as a row in a table).
The model can be either an instance of [[Model]] or an associative array.
DetailView uses the [[attributes]] property to determines which model attributes should be displayed and how they
should be formatted.
A typical usage of DetailView is as follows:
```php
echo DetailView::widget([
'model' => $model,
'attributes' => [
'title', // title attribute (in plain text)
'description:html', // description attribute in HTML
[ // the owner name of the model
'label' => 'Owner',
'value' => $model->owner->name,
],
],
]);
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment