under the course content there, I want to make the Active column to show either "Yes" or **"No"**At first it was showing "1" or "0", but that's not what I want to show. These are my attempted codes, and I'm getting this error. Appreciate it if anyone can please guide me.
Unknown Property – yii\base\UnknownPropertyException
Getting unknown property: app\models\Coursecontent::isActive
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\DetailView;
/* #var $this yii\web\View */
/* #var $model app\models\Course */
$this->title = $model->course_name;
$this->params['breadcrumbs'][] = ['label' => 'Courses', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
\yii\web\YiiAsset::register($this);
?>
<div class="course-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('<< Back', ['index'], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Update', ['update', 'id' => $model->course_id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->course_id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'course_name',
'description',
['attribute' => 'active', 'value' => $model->isActive,'contentOptions' => ['style' => $model->active == 1 ? 'color:green' :'color:red']],
'lastupdate',
],
]) ?>
<h2>Course Content</h2>
<?= Html::a('Create Coursecontent', ['coursecontent/create'], ['class' => 'btn btn-success']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'chapter_name',
'description',
'video_link',
['attribute' => 'active',
'value' => function ($model, $key, $index, $column) {return $model->isActive;},
'contentOptions' => function ($model, $key, $index, $column) {
return $model->active == 1 ? ['style' => 'color:green'] : ['style' => 'color:red'];
}],
['class' => 'yii\grid\ActionColumn',
'contentOptions' => ['class' => 'text-center'],
'buttons' => [
'view' => function ($url,$model) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>',['coursecontent/view', 'id' => $model->coursecontent_id]);
},
'update' => function ($url,$model) {
return Html::a('<span class="glyphicon glyphicon-pencil"></span>',['coursecontent/update', 'id' => $model->coursecontent_id]);
},
'delete' => function ($url,$model) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>',['coursecontent/delete', 'id' => $model->coursecontent_id],
['data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]);
},
]],
],
]); ?>
</div>
Have to make 2 changes to your code.
Change all $model->isActive to $model->active.
Return "Yes" or "No" depending to $model->active value.
Update DetailView:
[
'attribute' => 'active',
'value' => function ($model) {
return $model->active ? "Yes" : "No";
},
'contentOptions' => ['style' => $model->active ? 'color:green' :'color:red']
],
Update GridView:
[
'attribute' => 'active',
'value' => function ($model) {
return $model->active ? "Yes" : "No";
},
'contentOptions' => function ($model, $key, $index, $column) {
return $model->active ? ['style' => 'color:green'] : ['style' => 'color:red'];
}
],
Related
The model is Booking.php. I need to get data from another model called ServiceCategory.php and add a filter to it in Gridview on my index.php. I created filter on view/index.php as below.
['label' => 'Service Category', 'attribute' => 'category.name', 'filter' => ArrayHelper::map(app\models\Servicecategory::find()->where(['status' => true])->asArray()->all(), 'id', 'name')],
This is my index.php
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\grid\GridView;
use app\models\Servicecategory;
/* #var $this yii\web\View */
/* #var $searchModel app\models\BookingSerach */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Bookings';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="list-booking">
<h1 class=""><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p class="form-inline text-right">
<label>Client:</label>
<?= Html::dropDownList('client', null, ArrayHelper::map($clients, 'id', 'fullname'), ['prompt' => 'Please Select', 'class' => 'form-control']) ?>
<?= Html::a('+ New Booking', ['booking/create/'], ['class' => 'btn btn-primary client-create']) ?>
</p>
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
['label' => 'Client', 'value' => 'user.client.view', 'format' => 'raw'],
'postCode',
'start',
'end',
['label' => 'Service Category', 'attribute' => 'category.name', 'filter' => ArrayHelper::map(Servicecategory::find()->where(['status' => true])->asArray()->all(), 'id', 'name')],
//'numberOfDays',
//'date',
//'followUpEmail:email',
// 'followUpEmailSent:ckeckbox',
//'Status',
['attribute' => 'status', 'value' => 'Status', 'filter' => array_filter(\app\models\Booking::$statuses)],
['class' => 'yii\grid\CheckboxColumn',
'header' => 'follow Up',
'checkboxOptions' => function($model, $key, $index) {
$url = \yii\helpers\Url::to(['booking/followup/' . $model->id]);
return ['onclick' => 'js:followUp(this, "' . $url . '")', 'checked' => false, 'id' => 'followup'];
}
],
['class' => 'yii\grid\ActionColumn',
'headerOptions' => ['style' => 'width:15%'],
'template' => '{view} {approval} {update} {delete} ',
'buttons' => [
'view' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', ['/booking/review/' . $model->id], [
'title' => Yii::t('app', 'Review'),
]);
},
'approval' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-ok"></span>', ['/booking/approval/' . $model->id], [
'title' => Yii::t('app', 'Additional Details'),
'class' => 'error',
]);
},
],
],
],
]);
?>
</div>
<?php
$script = <<< JS
$('.client-create').on('click', function () {
select = $(this).prev();
if(select.val()){
location.href = $(this).attr('href')+"/"+select.val();
} else {
$(this).parent().addClass('has-error');
}
return false;
});
JS;
$this->registerJs($script);
$this->registerJs("
function followUp(e, url){
$('#modal').modal('show').find('#modalContent').load(url);
}", yii\web\View::POS_END);
?>
I can get the data to the column on gridview but the filter is not working. Can anyone help me with this?
I solved it myself. For your information I post it here. I changed the attribute as the model contains and added the value as category.name
['attribute' => 'categoryID', 'value' => 'category.name','filter' => ArrayHelper::map(Servicecategory::find()->where(['status' => true])->asArray()->all(), 'id', 'name')],
Can you help me to add a filter on checkbox column on Grid view widget yii2? I used yii\grid\CheckboxColumn to add the check box column in the Gridview on my index.php. But I couldn't add a filter above the column as the other columns. Please look into my code below.
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
['label' => 'Client', 'attribute' => 'name', 'value' => 'client.view', 'format' => 'raw'],
'postCode',
'start',
'end',
[
'attribute' => 'categoryID',
'value' => 'category.name',
'filter' => ArrayHelper::map(Servicecategory::find()->where(['status' => true])->asArray()->all(), 'id', 'name')
],
[
'attribute' => 'status',
'headerOptions' => ['style' => 'width:12%'],
'value' => 'Status',
'filter' => array_filter(\app\models\Booking::$statuses),
'filterInputOptions' => ['class' => 'form-control', 'prompt' => 'All']
],
['class' => 'yii\grid\CheckboxColumn',
'header' => 'follow Up',
'contentOptions' => ['class' => 'text-center'],
'checkboxOptions' => function($model, $key, $index) {
$url = \yii\helpers\Url::to(['booking/followup/' . $model->id]);
return ['onclick' => 'js:followUp("' . $url . '")', 'checked' => $model->followUpEmailSent ? true : false, 'value' => $model->followUpEmailSent];
}
],
['class' => 'yii\grid\ActionColumn',
'headerOptions' => ['style' => 'width:10%'],
'template' => '{view} {approval} {update} {delete} ',
'buttons' => [
/* 'view' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', ['/booking/review/' . $model->id], [
'title' => Yii::t('app', 'Review'),
]);
}, */
'approval' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-ok"></span>', ['/booking/approval/' . $model->id], [
'title' => Yii::t('app', 'Additional Details'),
'class' => 'error',
]);
}
],
],
],
]);
Following is the checkbox column.
['class' => 'yii\grid\CheckboxColumn',
'header' => 'follow Up',
'contentOptions' => ['class' => 'text-center'],
'checkboxOptions' => function($model, $key, $index) {
$url = \yii\helpers\Url::to(['booking/followup/' . $model->id]);
return ['onclick' => 'js:followUp("' . $url . '")', 'checked' => $model->followUpEmailSent ? true : false, 'value' => $model->followUpEmailSent];
}
],
Can someone help with this?
You may use your Gii tool-> CRUD generator to create your filter file.
Then you can pass your params to search model like this:
$searchModel = new SearchModel;
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
You will need to return your $dataProvider from SearchModel
Use the search file for implement filters on grid.
I suggest that do not use gridview filter in your case.
Write your search related code in search file and add checkbox on search form.
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>
<div class="ideas-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?php echo $form->field($model, 'name[]')->checkboxList(
['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']);
?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
I have the controller code:
public function actionDownload($id) {
$download = Status::findOne($id);
$path = Yii::getAlias('#webroot').'/uploads/status/'.$download->image_web_filename;
if (file_exists($path)) {
return Yii::$app->response->SendFile($path,$download->image_src_filename);
exit;
} else {
throw new NotFoundHttpException("can't find {$download->image_src_filename} file");
}
}
I have the view code in the GridView:
['attribute'=>'Download',
'format'=>'raw',
'value' => function($data)
{
return
Html::a('Download file', ['status/download','id'=>$data->id],['class' => 'btn btn-primary']);
}
],
This is what displayed on the screen.The pdf is only downloaded when the I refresh the browser but this still remains on the screen what is the problem?:
%PDF-1.4 %�쏢 5 0 obj <> stream x��]K�%ECű%P4���y��.�u�#�Q �zt��P�0��_��'��gx����/3�n���zܞ���xaAR7o�s�<�;�����܈�o��w?8��]�y�/'�O�FU��#b㍤���G����͇'r����4�+B��{3���SW����'��<�������A�ǃ*x���N27r�$���V������w�_?��k�~���+�'wFr6��f! ��ȍ7�Ӡ���UG���g;=�2�j��Ǒ �oƱQƇfh�?�WҘ�0۟��/�l�E���i(�{��* ��'�W��8��T�j 2��p������\��aܵH�$�Ci��u��$j��QZ���aIk����\o���D2Y)�/FV��nġ�a����� �_ؐ��rf�� �7�*� �IN�J{�l����Cqr�D�������[�]9���E�7�����k�j���9N�\�EC��F����&ռ-����uJV����3����L!�f��x���F�����̈́��#��U����:*g��&C�ӤD��q�17�#Jg+�J����w��ߨ?M_�a���R�ݼ��zu%��\�#�7$M&���-dO"#�]�i�89�}��<��� m��04�I�bHJ �O�M��`f�Ϯ�E�{2}`���n�����.l���+cZ?�����6C��N��XZJW�ylȒ��"�D�ͧ�8s���q��om��ݍx�6(��T�9Uɨ�g���x���\���'�Ͻ����BE��|��� p���9P]��R*����C��������jC�m���1��#K���Y���rt=�:�x����W����qr�便p4��"��B�}=�2������p��!x+�)zv���̳���\^*�R�ugȎ!J<��q�~���C��/��ˏU8�C7A0`�6x����6ۗ#�b���Ħ�(��Sk̍�"|����9�" �G�`#LD���e�B�Q���� d��[q�Z�aځ��-j�����Zk�}F4���l���y�T ��B��|���ҕ~/����a�+�7��Ai=W���1�
The problem that I had in this specific code is that I had to remove
<?php Pjax::begin(); ?> and <?php Pjax::end(); ?> and also 'pjax'=>true above and in the gridview column so that it worked I didnt alter anything in the actionDownload($id) code
Before editing(original erroneous code)
`....................................
<h3><?= Html::encode($this->title) ?></h3>
<p>
<?= Html::a('Create Status', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php Pjax::begin(); ?>
<?php
$gridColumns = [
['class' => 'kartik\grid\SerialColumn'],
'id',
'message',
'permissions',
'created_by',
'created_at',
// 'updated_at',
// 'image_src_filename',
//'image_web_filename',
['attribute'=>'Download',
'format'=>'raw',
'value' => function ($data) {
return
Html::a('Download file', ['status/download','id'=>$data->id], ['class' => 'btn btn-primary']);
}
],
];
?>
<?php
$export = ExportMenu::widget([
'dataProvider' => $dataProvider,
//'columns' => $gridColumns,
'fontAwesome' => true,
'exportConfig' => [
ExportMenu::FORMAT_HTML => false,
ExportMenu::FORMAT_TEXT => false,
ExportMenu::FORMAT_CSV => false,
ExportMenu::FORMAT_EXCEL => false,
ExportMenu::FORMAT_EXCEL_X => false,
],
]);
?>
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'containerOptions' => ['style' => 'overflow: auto'], // only set when $responsive = false
'toolbar' => [
$export
],
'pjax' => true,
'bordered' => true,
'striped' => true,
'responsive' => true,
'floatHeader' => true,
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => 'Forms Uploaded by Clients.',
],
]);
?>
<?php Pjax::end(); ?></div>`
After editing(original erroneous code)
`
<h3><?= Html::encode($this->title) ?></h3>
<p>
<?= Html::a('Create Status', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php
$gridColumns = [
['class' => 'kartik\grid\SerialColumn'],
'id',
'message',
'permissions',
'created_by',
'created_at',
// 'updated_at',
// 'image_src_filename',
//'image_web_filename',
['attribute'=>'Download',
'format'=>'raw',
'value' => function ($data) {
return
Html::a('Download file', ['status/download','id'=>$data->id], ['class' => 'btn btn-primary']);
}
],
];
?>
<?php
$export = ExportMenu::widget([
'dataProvider' => $dataProvider,
//'columns' => $gridColumns,
'fontAwesome' => true,
'exportConfig' => [
ExportMenu::FORMAT_HTML => false,
ExportMenu::FORMAT_TEXT => false,
ExportMenu::FORMAT_CSV => false,
ExportMenu::FORMAT_EXCEL => false,
ExportMenu::FORMAT_EXCEL_X => false,
],
]);
?>
<?php
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'containerOptions' => ['style' => 'overflow: auto'], // only set when $responsive = false
'toolbar' => [
$export
],
'bordered' => true,
'striped' => true,
'responsive' => true,
'floatHeader' => true,
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => 'Forms Uploaded by Clients.',
],
]);
?>`
Thats all !! I found out.
Just add
ob_clean();
before
return Yii::$app->response->SendFile($path,$download->image_src_filename);
.
ob_clean() will clean your output and fix the problem .
It worked for me.
Thanks
It is look like you need header for that.
try with this code:
public function actionDownload($id) {
header("Content-type:application/pdf");
........
if is not working try to add this header too
header("Content-Disposition:attachment;filename='filename.pdf'");
I am new in yii2, Right now i am creating sample crud appliacation. I used pjax for gridview, It is working fine for me, My problem is when i update my row at that time pjax also calling now i want to disable this pjax for update button. How can i resolve this issue ? Here is my code
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\helpers\Url;
use yii\widgets\Pjax;
/* #var $this yii\web\View */
/* #var $searchModel backend\models\PostSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Posts';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="post-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Post', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php \yii\widgets\Pjax::begin(
['id' => 'StickerList', 'timeout' => false, 'enablePushState' => false, 'clientOptions' => ['method' => 'GET']]
); ?>
<?= GridView::widget([
'dataProvider' => $model->search(),
'filterModel' => $model,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute'=>'PostType',
'filter'=>Html::activeDropDownList($model, 'PostType',array(""=>"All","1"=>"Status","2"=>"Images","3"=>"Video"),['class'=>'form-control','prompt' => 'Select Post Type']),
],
'PostTitle',
[
'header' => 'Artist',
'attribute' => 'ArtistName',
],
[
'header' => 'Date Posted',
'attribute' => 'DatePosted',
'filter' => false,
],
[
'header' => '# Likes',
'attribute' => 'TotalLikes',
'filter' => false,
],
[
'header' => '# Comments',
'attribute' => 'TotalComments',
'filter' => false,
],
[
'header' => 'Exclusive',
'attribute' => 'IsExclusive',
'filter'=>Html::activeDropDownList($model, 'IsExclusive',array(""=>"All","0"=>"Normal","1"=>"Exclusive"),['class'=>'form-control','prompt' => 'Select Exclusive']),
],
[
'header' => 'Status',
'attribute' => 'Status',
'filter'=>Html::activeDropDownList($model, 'Status',array(""=>"All","1"=>"Active","2"=>"Inactive"),['class'=>'form-control','prompt' => 'Select Status']),
],
[
'class' => 'yii\grid\ActionColumn',
'template'=>'{update}',
'buttons' => [
'update' => function ($url,$model) {
$url = Url::toRoute('post/update?id='.$model['PostID']);
return Html::a('<span class="glyphicon glyphicon-pencil"></span>',$url);
},
],
],
],
]); ?>
<?php \yii\widgets\Pjax::end(); ?>
</div>
You should need to add [ 'data-pjax' => false ] or [ 'data-pjax' => '0' ] in anchor tag options of update button.
For example,
[
'class' => 'yii\grid\ActionColumn',
'template'=>'{update}',
'buttons' => [
'update' => function ($url,$model) {
$url = Url::toRoute('post/update?id='.$model['PostID']);
return Html::a('<span class="glyphicon glyphicon-pencil"></span>',$url, ['data-pjax' => '0']);
},
],
]
So I have created a simple search form, and created the query within my controller (I am aware this isn't the best way to do it and should be done in my model/search model however for the time being it will do).
I was wondering how can I add an option to my form within the two dropdown with the label All which if passed will mean that that part of the query isn't applied.
Below is my view
<?php $form = ActiveForm::begin(['id' => 'home-search','method' => 'post', 'action' => Url::to(['productitem/search'])]); ?>
<?= $form->field($productitem, 'name')->textInput(array('placeholder' => 'What are you looking for?'))->label(false) ?>
<?= $form->field($productitem, 'brand_id')->dropDownList(
ArrayHelper::map(ProductBrand::find()->all(),'id','name'),
['prompt'=>'Select Brand']
)->label(false) ?>
<?= $form->field($productitem, 'category_id')->dropDownList(
ArrayHelper::map(ProductCategory::find()->all(),'id','name'),
['prompt'=>'Select Department']
)->label(false) ?>
<div class="form-group search-button">
<button type="submit" class="btn btn-primary" name="login-button">Search <i class="fa fa-lg fa-arrow-circle-o-right"></i></button>
</div>
<?php ActiveForm::end(); ?>
Below is my controller/query
public function actionSearch()
{
$query = ProductItem::find()
->andFilterWhere(['like', 'name', $_POST['ProductItem']['name']])
->andFilterWhere(['in', 'brand_id', $_POST['ProductItem']['brand_id']])
->andFilterWhere(['in', 'category_id', $_POST['ProductItem']['category_id']]);
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
return $this->render('search', [
'dataProvider' => $dataProvider,
]);
}
hope this will offer some reference.
<?= $form->field($model, 'categoryid1')->widget(Select2::className(), [
'id' => 'categoryid1',
'name' => 'categoryid1',
'data' => ArrayHelper::map(Category::getCategoryByPid(0), 'id', 'name'),
'options' => [
'placeholder' => 'choose first',
],
'pluginOptions' => [
'allowClear' => true
],
])->label('first') ?>
<?= $form->field($model, 'categoryid2')->widget(DepDrop::className(), [
'type' => DepDrop::TYPE_SELECT2,
'id' => 'categoryid2',
'name' => 'categoryid2',
'data' => $model->categoryid1 ? ArrayHelper::map(Category::getCategoryByPid($model->categoryid1), 'id', 'name') : [],
'pluginOptions' => [
'depends' => ['product-categoryid1'],
'placeholder' => 'choose second',
'url' => Url::to(['/category/second'])
]
])->label('second'); ?>
<?= $form->field($model, 'categoryid')->widget(DepDrop::className(), [
'type' => DepDrop::TYPE_SELECT2,
'data' => $model->categoryid2 ? ArrayHelper::map(Category::getCategoryByPid($model->categoryid2), 'id', 'name') : [],
'pluginOptions' => [
'depends' => ['product-categoryid1', 'product-categoryid2'],
'placeholder' => 'third',
'url' => Url::to(['/category/third'])
]
])->label('third'); ?>