data-grid.md 6.26 KB
Newer Older
1 2 3 4
Data grid
=========

Data grid or GridView is one of the most powerful Yii widgets. It is extremely useful if you need to quickly build admin
5
section of the system. It takes data from [data provider](data-providers.md) and renders each row using a set of columns
6 7 8 9 10 11 12 13 14
presenting data in a form of a table.

Each row of the table represents the data of a single data item, and a column usually represents an attribute of
the item (some columns may correspond to complex expression of attributes or static text).

Grid view supports both sorting and pagination of the data items. The sorting and pagination can be done in AJAX mode
or normal page request. A benefit of using GridView is that when the user disables JavaScript, the sorting and pagination
automatically degrade to normal page requests and are still functioning as expected.

Anderson Müller committed
15
The minimal code needed to use GridView is as follows:
16 17

```php
Alexander Makarov committed
18 19 20 21
use yii\data\GridView;
use yii\data\ActiveDataProvider;

$dataProvider = new ActiveDataProvider([
22 23 24 25
    'query' => Post::find(),
    'pagination' => [
        'pageSize' => 20,
    ],
Alexander Makarov committed
26 27
]);
echo GridView::widget([
28
    'dataProvider' => $dataProvider,
Alexander Makarov committed
29
]);
30 31 32 33 34 35 36 37 38 39 40 41 42
```

The above code first creates a data provider and then uses GridView to display every attribute in every row taken from
data provider. The displayed table is equiped with sorting and pagination functionality.

Grid columns
------------

Yii grid consists of a number of columns. Depending on column type and settings these are able to present data differently.

These are defined in the columns part of GridView config like the following:

```php
43
echo GridView::widget([
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    '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' => 'yii\grid\DataColumn', // can be omitted, default
            'value' => function ($data) {
                return $data->name;
            },
        ],
    ],
59
]);
60 61
```

Alexander Makarov committed
62
Note that if columns part of config isn't specified, Yii tries to show all possible data provider model columns.
63 64 65

### Column classes

Alexander Makarov committed
66 67 68 69
Grid columns could be customized by using different column classes:

```php
echo GridView::widget([
70 71 72 73 74 75
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'class' => 'yii\grid\SerialColumn', // <-- here
            // you may configure additional properties here
        ],
Alexander Makarov committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89
```

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) {
90
    return 'a string';
Alexander Makarov committed
91 92 93 94 95 96 97 98 99
}
```

You may specify various container HTML options passing arrays to:

- `headerOptions`
- `contentOptions`
- `footerOptions`
- `filterOptions`
100 101 102

#### Data column

Alexander Makarov committed
103 104 105 106 107
Data column is for displaying and sorting data. It is default column type so specifying class could be omitted when
using it.

TBD

108 109
#### Action column

Alexander Makarov committed
110 111 112 113
Action column displays action buttons such as update or delete for each row.

```php
echo GridView::widget([
114 115 116 117 118 119
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'class' => 'yii\grid\ActionColumn',
            // you may configure additional properties here
        ],
Alexander Makarov committed
120 121 122 123 124 125 126 127
```

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
128
  by the corresponding button rendering callbacks specified in [[yii\grid\ActionColumn::$buttons|buttons]]. For example, the token `{view}` will be
Alexander Makarov committed
129 130 131 132 133 134 135
  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) {
136
    // return the button HTML code
Alexander Makarov committed
137 138 139 140 141 142 143
}
```

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
144 145
  the callback should be the same as that of [[yii\grid\ActionColumn\createUrl()]]. If this property is not set,
  button URLs will be created using [[yii\grid\ActionColumn\createUrl()]].
Alexander Makarov committed
146

147 148
#### Checkbox column

Alexander Makarov committed
149 150
CheckboxColumn displays a column of checkboxes.
 
151
To add a CheckboxColumn to the [[yii\grid\GridView]], add it to the [[yii\grid\GridView::$columns|columns]] configuration as follows:
Alexander Makarov committed
152 153 154
 
```php
echo GridView::widget([
155 156 157 158 159 160 161 162
    'dataProvider' => $dataProvider,
    'columns' => [
        // ...
        [
            'class' => 'yii\grid\CheckboxColumn',
            // you may configure additional properties here
        ],
    ],
Alexander Makarov committed
163 164 165 166 167 168 169 170 171 172
```

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
```

173 174
#### Serial column

Alexander Makarov committed
175 176 177 178 179 180
Serial column renders row numbers starting with `1` and going forward.

Usage is as simple as the following:

```php
echo GridView::widget([
181 182 183
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'], // <-- here
Alexander Makarov committed
184 185
```

186 187 188 189 190 191 192 193
Sorting data
------------

- https://github.com/yiisoft/yii2/issues/1576

Filtering data
--------------

Anderson Müller committed
194
- https://github.com/yiisoft/yii2/issues/1581