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
150d5152
Commit
150d5152
authored
Mar 09, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more grid docs
parent
bd3dd038
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
125 additions
and
4 deletions
+125
-4
data-grid.md
docs/guide/data-grid.md
+124
-3
DataColumn.php
framework/grid/DataColumn.php
+1
-1
No files found.
docs/guide/data-grid.md
View file @
150d5152
...
...
@@ -15,7 +15,18 @@ automatically degrade to normal page requests and are still functioning as expec
The minimal code needed to use CGridView is as follows:
```
php
sdf
use
yii\data\GridView
;
use
yii\data\ActiveDataProvider
;
$dataProvider
=
new
ActiveDataProvider
([
'query'
=>
Post
::
find
(),
'pagination'
=>
[
'pageSize'
=>
20
,
],
]);
echo
GridView
::
widget
([
'dataProvider'
=>
$dataProvider
,
]);
```
The above code first creates a data provider and then uses GridView to display every attribute in every row taken from
...
...
@@ -50,20 +61,130 @@ echo GridView::widget([
]);
```
Note: If columns part of config isn't specified, Yii tries to show all possible data provider model columns.
Note that if columns part of config isn't specified, Yii tries to show all possible data provider model columns.
### Column classes
Grid columns could be customized by using different column classes:
```
php
echo
GridView
::
widget
([
'dataProvider'
=>
$dataProvider
,
'columns'
=>
[
[
'class'
=>
'yii\grid\SerialColumn'
,
// <-- here
// you may configure additional properties here
],
```
Additionally to column classes provided by Yii that we'll review below you can create your own column classes.
Each column class extends from
[
[\yii\grid\Column
]
] so there some common options you can set while configuring
grid columns.
-
`header`
allows to set content for header row.
-
`footer`
allows to set content for footer row.
-
`visible`
is the column should be visible.
-
`content`
allows you to pass a valid PHP callback that will return data for a row. The format is the following:
```
php
function
(
$model
,
$key
,
$index
,
$grid
)
{
return
'a string'
;
}
```
You may specify various container HTML options passing arrays to:
-
`headerOptions`
-
`contentOptions`
-
`footerOptions`
-
`filterOptions`
#### Data column
Data column is for displaying and sorting data. It is default column type so specifying class could be omitted when
using it.
TBD
#### Action column
Action column displays action buttons such as update or delete for each row.
```
php
echo
GridView
::
widget
([
'dataProvider'
=>
$dataProvider
,
'columns'
=>
[
[
'class'
=>
'yii\grid\ActionColumn'
,
// you may configure additional properties here
],
```
Available properties you can configure are:
-
`controller`
is the ID of the controller that should handle the actions. If not set, it will use the currently active
controller.
-
`template`
the template used for composing each cell in the action column. Tokens enclosed within curly brackets are
treated as controller action IDs (also called
*button names*
in the context of action column). They will be replaced
by the corresponding button rendering callbacks specified in
[
[buttons
]
]. For example, the token
`{view}`
will be
replaced by the result of the callback
`buttons['view']`
. If a callback cannot be found, the token will be replaced
with an empty string. Default is
`{view} {update} {delete}`
.
-
`buttons`
is an array of button rendering callbacks. The array keys are the button names (without curly brackets),
and the values are the corresponding button rendering callbacks. The callbacks should use the following signature:
```
php
function
(
$url
,
$model
)
{
// return the button HTML code
}
```
In the code above
`$url`
is the URL that the column creates for the button, and
`$model`
is the model object being
rendered for the current row.
-
`urlCreator`
is a callback that creates a button URL using the specified model information. The signature of
the callback should be the same as that of
[
[createUrl()
]
]. If this property is not set, button URLs will be created
using
[
[createUrl()
]
].
#### Checkbox column
CheckboxColumn displays a column of checkboxes.
To add a CheckboxColumn to the
[
[GridView
]
], add it to the
[
[GridView::columns|columns
]
] configuration as follows:
```
php
echo
GridView
::
widget
([
'dataProvider'
=>
$dataProvider
,
'columns'
=>
[
// ...
[
'class'
=>
'yii\grid\CheckboxColumn'
,
// you may configure additional properties here
],
],
```
Users may click on the checkboxes to select rows of the grid. The selected rows may be obtained by calling the following
JavaScript code:
```
javascript
var
keys
=
$
(
'#grid'
).
yiiGridView
(
'getSelectedRows'
);
// keys is an array consisting of the keys associated with the selected rows
```
#### Serial column
Serial column renders row numbers starting with
`1`
and going forward.
Usage is as simple as the following:
```
php
echo
GridView
::
widget
([
'dataProvider'
=>
$dataProvider
,
'columns'
=>
[
[
'class'
=>
'yii\grid\SerialColumn'
],
// <-- here
```
TODO: rewrite these:
-
https://github.com/samdark/a-guide-to-yii-grids-lists-and-data-providers/blob/master/grid-columns.md
...
...
framework/grid/DataColumn.php
View file @
150d5152
...
...
@@ -28,7 +28,7 @@ class DataColumn extends Column
* @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
* is specified, the value of the specified attribute will be retrieved from each data model and displayed.
*
* Also, if [[
header
]] is not specified, the label associated with the attribute will be displayed.
* Also, if [[
label
]] is not specified, the label associated with the attribute will be displayed.
*/
public
$attribute
;
/**
...
...
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