I want to show json formated data in gridview. I have following:
Json Data:
[{"c_id":1,"c_name":"Marokand","c_logo":"Marokand","c_slogan":"Marokand","c_address":"Tashkent Amir Temur, Uzbekistan","c_website":"www.maroqand.uz","c_email":"info#maroqand.uz","c_owner":"someone",},{"c_id":2,"c_name":"Apple LLC","c_logo":"","c_slogan":"","c_address":"","c_website":"","c_email":"","c_owner":""}
In my view file, I have following code:
<?= GridView::widget([
'dataProvider'=>$dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'c_id',
'c_name',
'c_logo',
'c_slogan',
'c_address',
// 'c_website',
// 'c_email:email',
// 'c_owner',
// 'c_short_description:ntext',
// 'c_description:ntext',
// 'c_history:ntext',
// 'c_goal:ntext',
// 'cci_memerbship_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
How can I show json data in gridview?
Use ArrayDataProvider: Refer Link
On controller file first decode json data like this and init data provider:
use yii\data\ArrayDataProvider;
use yii\helpers\Json;
$data = Json::decode("YOUR JOSN DATA", true);
$dataProvider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => ['c_id'],
],
]);
on view side:
<?= GridView::widget([
'dataProvider'=>$dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'c_id',
'c_name',
'c_logo',
'c_slogan',
'c_address',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Related
I'm trying to use yii2-export extention.
When I export, after pressing OK for the 'disable any popup...' message, the pop up window appears, but no download is being done.
My code:
use kartik\export\ExportMenu;
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'idCandidate',
['class' => 'yii\grid\ActionColumn'],
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'exportConfig' => [
GridView::HTML => [],
GridView::CSV => [],
]
]);
Any idea?
Thanks.
i am trying to use kartik export and its not working for me. In config file I have added following code:
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
],
],
In Composer i have added folowing code
"kartik-v/yii2-export": "#dev",
"kartik-v/yii2-mpdf":"#dev",
"kartik-v/yii2-grid": "#dev"
My view code is like this:
<?php
use yii\helpers\Html;
use kartik\grid\GridView;
use yii\bootstrap\Tabs;
use kartik\export\ExportMenu;
use yii\widgets\Pjax;
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="general-info-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php
<p>
<?= Html::a('create', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php
$gridColumns = [
'sfcl_name',
[
'attribute'=> 'org_type',
'value' => 'orgType.cv_lbl'
],
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'columns' => [
'sfcl_name',
'phone',
[
'attribute'=>'regd_dt_ad',
'format'=>['date', 'php:Y-M-d'],
'xlFormat'=>'mmm\-dd\, yyyy', // different date format
'width'=>'100px'
],
[
'attribute'=> 'org_type',
'value' => 'orgType.cv_lbl'
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
My controller code is:
My controller code to call this view : public function actionIndex()
{
$searchModel = new SfclGeneralSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
On click of export menu nothing is happening.In tutorial i saw there were options for exporting to pdf ,html,csv,json,text.such options doesnot appers in my case.Is the css not working or what?
you run "composer update" command?.
also in your web.php
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
],
],
add the property downloadAction, like this:
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
'downloadAction' => 'gridview/export/download',
]
],
I'm trying to use kartik gridview and got the following error. -
Undefined variable: gridColumns
I've checked - Yii2: Kartik Gridview sum of a column in footer. But didn't find the issue in my code.
Code of index.php -
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'s_period',
'filter'=>ArrayHelper::map(Salary::find()->asArray()->all(), 's_period', 's_period'),
],
's_empid',
's_empname',
[
'attribute'=>'s_epf',
'pageSummary'=>true
],
//['class' => 'yii\grid\ActionColumn'],
],
]); ?>
if you assign manually the columns you don't need a var iwth columns specification so remove it
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//'columns' => $gridColumns,
'showPageSummary' => true,
'columns' => [
['class' => 'kartik\grid\SerialColumn'],
[
'attribute'=>'s_period',
'filter'=>ArrayHelper::map(Salary::find()->asArray()->all(), 's_period', 's_period'),
],
's_empid',
's_empname',
[
'attribute'=>'s_epf',
'pageSummary'=>true
],
//['class' => 'yii\grid\ActionColumn'],
],
]); ?>
i have model from "feedbackmodel" :
public function search($params)
{
$query = Feedback::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['TANGGAL'=>SORT_DESC]],
'pagination' => array('pageSize' => 10),
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'ID_KOMENTAR' => $this->ID_KOMENTAR,
'id' => $this->id,
'TANGGAL' => $this->TANGGAL,
]);
$query->andFilterWhere(['like', 'KOMENTAR', $this->KOMENTAR]);
return $dataProvider;
}
and the gridview code like this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'layout'=>"{items}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
'columns' => [
[ 'attribute' => 'iduser.photo',
'format' => 'html',
'value'=> function($data) { return Html::img($data->imageurl) . " <p class='feedback-username'>" . $data->username . "</p>"; },
'contentOptions'=>['style'=>'max-width: 10px; max-height: 10px'],
],
[ 'attribute' => 'KOMENTAR',
'format' => 'raw',
'value' => function($model) { return $model->KOMENTAR ."<br><p class='feedback-date'>". $model->TANGGAL ."</p>";},
],
[ 'class' => 'yii\grid\ActionColumn',
'contentOptions'=>['style'=>'width: 5px;'],
'template' => '{update} {delete}'
],
],
]); ?>
But why the page button is not shown, its limited but i can see the other data bcos the i cant find the button next or prev to the other page
I test your code
if I remove the layout=>"{items}", the page buttons are show otherwise not (i think your layout assignment mean show only the item and not the pager) try simply comment it
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//'layout'=>"{items}",
'tableOptions' => ['class' => 'table table-bordered table-hover'],
'showFooter'=>false,
'showHeader' => false,
'pager' => [
'firstPageLabel' => 'First',
'lastPageLabel' => 'Last',
],
..........
How to searching ,sorting ,filtering for two table in yii2 grid view? after generating from gii tool ,i got searching sorting for one table columns but i have to apply searching ,sorting ,filtering on columns of another table.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
// 'account_activation_token',
'email:email',
'profile.name',
[
'attribute' => 'name',
'value' => 'profile.name',
'enableSorting' => true,
//here i want to use the filter option just like in yii 1.1 but i dont know how to use the textbox search in yii2.0
],
// 'usertype',
// 'status',
// 'created_at',
// 'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Try Using:
[
'attribute' => 'name',
'value' => 'profile.name',
'enableSorting' => true,
'filter' => function($model){
return Html::activeTextInput($searchModel, $this->name),
}
],