{"id":224,"date":"2015-09-22T15:27:54","date_gmt":"2015-09-22T10:27:54","guid":{"rendered":"http:\/\/developer.uz\/blog\/?p=224"},"modified":"2017-11-06T09:12:15","modified_gmt":"2017-11-06T04:12:15","slug":"gridview-in-yii2","status":"publish","type":"post","link":"https:\/\/developer.uz\/blog\/gridview-in-yii2\/","title":{"rendered":"\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 GridView \u0432 Yii2"},"content":{"rendered":"<p>\u0422\u0435\u043c\u0430 \u0432\u0440\u043e\u0434\u0435 \u043a\u0430\u043a \u0438\u0437\u0436\u0435\u0432\u0430\u043d\u043d\u0430 \u0432\u0434\u043e\u043b\u044c \u0438 \u043f\u043e\u043f\u0435\u0440\u0435\u043a, \u043d\u043e \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u043e\u0441\u0438\u043b\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0442\u0430\u0442\u044c\u0435\u0439 \u0434\u043b\u044f \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432, \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u0445 \u0432\u0438\u0434\u0436\u0435\u0442\u0430 GridView, \u0442.\u043a \u0431\u0435\u0437 \u043d\u0435\u0433\u043e \u0443 \u043c\u0435\u043d\u044f \u0435\u0449\u0435 \u043d\u0435 \u043e\u0431\u0445\u043e\u0434\u0438\u043b\u0441\u044f \u043d\u0438 \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0435\u043a\u0442. \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0431\u0443\u0434\u0443 \u043f\u043e\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 FAQ.<\/p>\n<h2>\u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0444\u043e\u043d \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n<p>\u0415\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u043d\u0443\u0436\u043d\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u0441\u0442\u0440\u043e\u043a \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0446\u0432\u0435\u0442\u043e\u043c (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u0431\u043b\u043e\u0447\u0435\u043d\u043d\u044b\u0445 \u044e\u0437\u0435\u0440\u043e\u0432 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0430 \u0441\u0435\u0440\u043e\u043c \u0444\u043e\u043d\u0435), \u0442.\u043a \u044d\u0442\u043e \u043a\u0443\u0434\u0430 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0435\u0435, \u0447\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0430\u0434\u043f\u0438\u0441\u044c &#171;\u0432\u043a\u043b\/\u0432\u044b\u043a\u043b&#187; \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435. \u0418 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e: \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 \u0430\u0442\u0440\u0438\u0431\u0443\u0442 rowOptions<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\necho GridView::widget([\r\n    'dataProvider' =&gt; $data,\r\n    'rowOptions' =&gt; function ($model, $key, $index, $grid)\r\n    {\r\n      if($model-&gt;is_active == false) {\r\n          return ['style' =&gt; 'background-color:#778899;'];\r\n      }\r\n    },\r\n...\r\n])\r\n<\/pre>\n<p>\u0422.\u043a \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f $index &#8212; \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0440\u043e\u043a\u0438, \u043c\u043e\u0436\u043d\u043e \u0447\u0435\u0442\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0440\u0430\u0441\u043a\u0440\u0430\u0441\u0438\u0442\u044c \u0432 \u043e\u0434\u0438\u043d \u0446\u0432\u0435\u0442, \u043d\u0435 \u0447\u0435\u0442\u043d\u044b\u0435 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439. \u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043f\u0430\u0434\u0443\u0442 \u0432 \u043d\u0443\u0442\u0440\u044c \u0442\u0435\u0433\u0430 tr. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0431\u0435\u0437 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\necho GridView::widget([\r\n    'dataProvider' =&gt; $data,\r\n    'rowOptions' =&gt; ['style' =&gt; 'background-color:#778899;'],\r\n...\r\n])\r\n<\/pre>\n<h2>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0434\u0430\u043d\u043d\u044b\u0445<\/h2>\n<p>\u0415\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0434\u0430\u0442\u0443 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0441 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u043f\u043e \u043c\u0435\u0441\u044f\u0446\u0430\u043c. \u042f\u043d\u0432\u0430\u0440\u044c &#8212; \u0434\u0430\u043b\u044c\u0448\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0437\u0430 \u044f\u043d\u0432\u0430\u0440\u044c. \u0424\u0435\u0432\u0440\u0430\u043b\u044c \u0438 \u0442\u043f.\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b beforeRow \u0438 afterRow.<br \/>\n\u0410\u0442\u0440\u0438\u0431\u0443\u0442\u044b beforeRow \u0438 afterRow \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u043f\u043e \u0442\u043e\u043c\u0443 \u0436\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443, \u0447\u0442\u043e \u0438 rowOptions \u0437\u0430 \u0442\u0435\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c, \u0447\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u0435\u0434\u0435\u043d \u0434\u043e \u0438 \u043f\u043e\u0441\u043b\u0435 \u0442\u0435\u0433\u0430 tr.<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n$currentDate = date('m');\r\necho GridView::widget([\r\n    'dataProvider' =&gt; $data,\r\n    'beforeRow' =&gt;function ($model, $key, $index, $grid) use ($currentDate)\r\n    {\r\n      if($model-&gt;month != $currentDate) {\r\n          return '&lt;tr&gt;&lt;td colspan=10&gt;'.$model-&gt;month.'&lt;\/td&gt;&lt;\/tr&gt;';\r\n      }\r\n    }\r\n...\r\n])\r\n<\/pre>\n<h2>\u041f\u0440\u043e\u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0443 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 ActiveDataProvider, \u0442\u043e \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0432 query \u0443 \u0432\u0430\u0441 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 Query. \u042d\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441. ActiveDataProvider \u0434\u043e\u0431\u0430\u0432\u0438\u0442 \u043a \u043d\u0435\u043c\u0443 limit \u0438 offset \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u043e\u0433\u043e \u0432\u044b\u0432\u043e\u0434\u0430. \u041d\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0441\u0430\u043c\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a:<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n $query = MyModel::find()-&gt;where(...);\r\n $data =  new ActiveDataProvider([\r\n            'query' =&gt; $query,\r\n        ]);\r\n $totalSum = $query-&gt;sum('amount');\r\n<\/pre>\n<h2>\u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c url \u0434\u043b\u044f \u0441\u0441\u044b\u043b\u043e\u043a \u0432 ActionColumn<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0434\u043b\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0433\u0440\u0438\u0434\u043e\u0432 \u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u0434\u0440\u0435\u0441 \u0441\u0441\u044b\u043b\u043e\u043a \u0434\u043b\u044f GridView, \u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 urlCreator. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443:<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n  ['class' =&gt; 'yii\\grid\\ActionColumn',\r\n   'template' =&gt; '{update}&amp;nbsp;&amp;nbsp;&amp;nbsp;{delete}',\r\n   'urlCreator'=&gt;function($action, $model, $key, $index){\r\n       return \\yii\\helpers\\Url::to(['dashboard\/user-'.$action,'id'=&gt;$model-&gt;id]);\r\n   }\r\n  ]\r\n<\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 &#8212; \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043a\u043d\u043e\u043f\u043e\u043a \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043a\u0430\u043a\u0443-\u043d\u0438\u0431\u0443\u0434\u044c \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 ({update} {delete} {view}) &#8212; \u044d\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 template.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0422\u0435\u043c\u0430 \u0432\u0440\u043e\u0434\u0435 \u043a\u0430\u043a \u0438\u0437\u0436\u0435\u0432\u0430\u043d\u043d\u0430 \u0432\u0434\u043e\u043b\u044c \u0438 \u043f\u043e\u043f\u0435\u0440\u0435\u043a, \u043d\u043e \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u043e\u0441\u0438\u043b\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0442\u0430\u0442\u044c\u0435\u0439 \u0434\u043b\u044f \u043d\u043e\u0432\u0438\u0447\u043a\u043e\u0432, \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e\u0431 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u0445 \u0432\u0438\u0434\u0436\u0435\u0442\u0430 GridView, \u0442.\u043a \u0431\u0435\u0437 \u043d\u0435\u0433\u043e \u0443 \u043c\u0435\u043d\u044f \u0435\u0449\u0435 \u043d\u0435 \u043e\u0431\u0445\u043e\u0434\u0438\u043b\u0441\u044f \u043d\u0438 \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0435\u043a\u0442. \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0431\u0443\u0434\u0443 \u043f\u043e\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 FAQ. \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0444\u043e\u043d \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0434\u0430\u043d\u043d\u044b\u0445 \u0415\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u043d\u0443\u0436\u043d\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[21,13,19],"_links":{"self":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/224"}],"collection":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/comments?post=224"}],"version-history":[{"count":7,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/224\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/224\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/media?parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/categories?post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/tags?post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}