Adding filter to GridView widget - php

I am trying to add filter to GridView widget included in _form.php. The grid is showing fine, even filter field is shown, but the filter is not working.
Here is my code:
<?php
$searchModel = New CitySearch(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'city_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>

I have found the solution. The search model must actually be searched before attaching it to the GridView. Therefore, I just needed to add one line to get it to work:
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
The entire code would like like that:
<?php
$searchModel = New CitySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'city_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>

Related

How do I show text rather than boolean value (Yii2)?

So I have this form to create a job role and i need to show whether the job role is active or dormant. I am using boolean values 0 and 1 to represent dormant and active. This is my code in the form view (form.php).
<?= $form->field($model, 'status')->dropDownList(['1' => 'Active', '0' => 'Dormant'], ['prompt'=>'Select Option']) ?>
In my model (Application.php) I have added this function
public function getStatusLabel()
{
return $this->status ? 'Active' : 'Dormant';
}
Then in my index.php view I added to display Active/Dormant.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'Status',
'value' => 'statusLabel',
],
All is working so far. The only problem is that in my view.php (to view each application added) the status is still display 1 and 0. How do I display active/dormant in my view.php too?
You can pass this to a function like this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'Status',
'value' => function ($data) {
return $data->getStatusLabel();
},
],

how to increase container width to fix GridView scroll in yii2

I am using yii2 GridView widget for showing data in form of table.
As there are many fields to show, GridView ( below table ) showing scroll for it. But i don't want it.
I want to increase container width so that it show table without scroll.
I had search about bootstrap.css file but not found in any directory.
How do i change GridView width ?
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
.........
['class' => 'yii\grid\ActionColumn'],
],
'tableOptions' =>['style' => 'width: 1800px;'],
]); ?>
or the gridview container options
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
.........
['class' => 'yii\grid\ActionColumn'],
],
'options' =>['style' => 'width: 1800px;'],
]); ?>
http://www.yiiframework.com/doc-2.0/yii-grid-gridview.html
http://www.yiiframework.com/doc-2.0/yii-grid-gridview.html#$options-detail
http://www.yiiframework.com/doc-2.0/yii-grid-gridview.html#$tableOptions-detail
Otherwise if you need a larger page container you need a new layout
add tye new layout in you app/views/layout eg:changing the container this this way
....
<div class="container-fluid">
<?= Breadcrumbs::widget([
'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],
]) ?>
<?= Alert::widget() ?>
<?= $content ?>
</div>
....
then assign you new layout to you view before render
public function actionIndex()
{
......
$this->layout = 'my_new_layout'
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

Yii2 framework: how to format/convert Integer to Date

I need to convert an integer number to date using the Yii2 Framework for PHP.
I can convert it using the PHP function like this (as I found in this question):
date("Y-m-d H:i:s", 1498675028);
But I am new using Yii2 so I am not sure how to do it and maybe it can be done with Yii2.
I have a table with some columns and two of them (created_at and updated_at) has the integer 1498675028 (it is in the Unix time stamp format). But the user see in the view the integer number instead of a date. This is part of my index.php view file:
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
'created_at',
'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
'created_at:date',
'updated_at:date',
[
'attribute' => 'updated_at',
'value' => function ($model, $key, $index, $grid) {
return date('Y-m-d', $model->updated_at);
},
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>

how to perform customized search in pjax form on only column of table and place the search bar in layout which is common to all pages in yii 2

i want to search the column username and get the id of that username in the user table
usercontroller.php
public function actionIndex()
{
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if (Yii::$app->request->isPjax) {
return $this->renderPartial('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
} else {
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}
view/user/index.php
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('app', 'Create User'), ['create'],
['class' => 'btn btn-success']) ?>
</p>
<?php Pjax::begin(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
'email:email',
'password',
'user_type',
// 'newsletters',
// 'terms',
// 'auth_key',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
</div>
to achieve this where i have to place the code i am new to yii2 framework
thanks in advance

Yii2 show row number in grid view

I want to show row number in my grid view but I can't not find a way to do this.
I found this question Get the absolute row number in a grid but it seems to be geared towards Yii1.
This is what my grid view looks like
<?php Pjax::begin(['id' => 'leaderboard-pjax']); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'username',
'total',
],
'summary'=>'',
]); ?>
<?php Pjax::end(); ?>
and my data provider
$dataProvider = new ActiveDataProvider([
'query' => (new \yii\db\Query())
->select('user.username, score.total')
->from('user')
->leftJoin('score', 'score.user_id = user.id')
->where(['user.role' => User::ROLE_USER])
->orderBy('total DESC'),
'pagination' => [
'pageSize' => 10,
],
]);
You need ['class' => 'yii\grid\SerialColumn'],
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'username',
'total',
],
'summary'=>'',
]); ?>

Categories