Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
725ec9a9
Commit
725ec9a9
authored
Feb 26, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More data widget docs
parent
ea6f29dd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
170 additions
and
19 deletions
+170
-19
data-grid.md
docs/guide/data-grid.md
+28
-17
data-providers.md
docs/guide/data-providers.md
+109
-0
data-widgets.md
docs/guide/data-widgets.md
+33
-2
No files found.
docs/guide/data-grid.md
View file @
725ec9a9
...
...
@@ -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
...
...
docs/guide/data-providers.md
View file @
725ec9a9
...
...
@@ -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
------------------------------------------
docs/guide/data-widgets.md
View file @
725ec9a9
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
,
],
],
]);
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment