Yii2 Display Data In GridView

Yii2 is much powerful and very fast provide lots of basic feature with short code.This is very simple article to display data in data gridview follow steps and achieve pagination with filtering of data in grid view.

We have to start step by step : First we will display data from database to datagrid then after we will apply filer to datagrid.

For that i had taken table of student – Table

My Database Name : student

Table name : stud

First we will create one Model name is Stud:

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\db\ActiveRecord;
use yii\web\UploadedFile;

class Stud extends ActiveRecord
{
public function rules() {
return [
[[‘name’,’address’],’required’],
[[‘imageFile’], ‘file’],
];
}
public function attributeLabels()
{
return [
‘name’ => ‘Name’,
‘no’ => ‘No’,
];
}
public function actionIndex(){
}
}

 

First we will create one Model name is StudSearch:

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\base\Configurable;
use app\models\Stud;
use app\models\StudSearch;
use yii\grid\GridView;
use yii\grid\DataColumn;
use yii\helpers\Url;

/**
* @var yii\web\View $this
*/

$this->title = ‘About’;
$this->params[‘breadcrumbs’][] = $this->title;

$this->registerJs(“$(‘#export’).click(function(){
// var formData = new FormData();
$.ajax({
url: ‘”.Yii::$app->getUrlManager()->createUrl(‘stud/export’).”‘,
method: ‘POST’,
// data: formData,
// processData:false,
// contentType:false,
// success: function () {
// alert(‘hello’);
// },
});
});

“);

$this->registerJs(“$(‘#import’).click(function(){
$.ajax({
url: ‘”.Yii::$app->getUrlManager()->createUrl(‘stud/import’).”‘,
method: ‘POST’,
});
});

“);
$this->registerJs(“$(‘#forcedownloadpdf’).click(function(){
$.ajax({
url: ‘”.Yii::$app->getUrlManager()->createUrl(‘stud/forcedownloadpdf’).”‘,
method: ‘POST’,
});
});

“);

?>

<?= Html::a("Add New”,[‘stud/studform’]) ?>
‘export’,’type’=>’button’,’class’=>’btn btn-primary’]); ?>
‘import’,’type’=>’button’,’class’=>’btn btn-primary’]); ?>
‘forcedownloadpdf’,’type’=>’button’,’class’=>’btn btn-primary’]); ?>

title) ?>

$dataProvider,
‘filterModel’ => $searchModel,
‘columns’ => [
[
‘attribute’=>’no’,
‘label’=>’Stud No’,
‘format’=>’text’,//raw, html
],
‘name’,
‘address’,
[
‘label’=>’imageFile’,
‘attribute’=>’imageFile’,

‘format’=>’raw’,

‘value’ => function($data){
return Html::img($data->imageFile,[‘alt’=>’yii’,’height’=>’60px’,’width’=>’60px’]);
}

],
[‘class’=>’yii\grid\ActionColumn’],
],
]); ?>

Leave a comment