I have adjusted the widget like:
> import kartik/grid/GridView
{{ use('kartik/grid/GridView') }}
> use GridView::widget(...)
{{
grid_view_widget(
{
'dataProvider': dataProvider,
'export': false,
'responsive': true,
'condensed': true,
'toolbar': [
'{export}',
'{toggleData}'
]
}
)
}}
but the table was cut and toolbar wasn't shown:
Try
GridView::widget([
'dataProvider' => $dataProvider,
'columns' => $columns,
'pjax' => true,
'condensed' => true,
'panel' => [
'heading' => '<h3 class="panel-title">' . $this->title,
'before' => Html::a('<i class="glyphicon glyphicon-plus"></i>', ['create'],
['class' => 'btn btn-success']) . ' ' .
Html::a('<i class="glyphicon glyphicon-repeat"></i>', ['index'], ['class' => 'btn btn-info']),
],
'export' => false,
'toolbar' => [
'{toggleData}'
]])
With this atleast your toolbar will be shown. Let me know what happens. :)
Related
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'];
}
],
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 have a gridview with pjax.
<div class="request-index">
<div id="ajaxCrudDatatable">
<?=
GridView::widget([
'id' => 'crud-datatable',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax' => true,
'columns' => require(__DIR__ . '/_columns.php'),
'toolbar' => [
['content' =>
Html::a('<i class="glyphicon glyphicon-plus"></i> Add Request', ['create'], ['role' => 'modal-remote', 'title' => 'Create new Requests', 'class' => 'btn btn-success']) .
Html::a('<i class="glyphicon glyphicon-repeat"></i> Reload', [''], ['data-pjax' => 1, 'class' => 'btn btn-primary', 'title' => 'Reset Grid']) .
'{toggleData}' .
'{export}'
],
],
'panel' => [
'type' => 'primary',
'heading' => '<i class="glyphicon glyphicon-list"></i> Requests listing',
'before' => '<button type="button" class="btn btn-danger btn-secondary"> Belum Selesai : <strong>'. $count_request_belum_selesai .'</strong> </button>',
'after' => BulkButtonWidget::widget([
'buttons' => Html::a('<i class="glyphicon glyphicon-trash"></i> Delete All', ["bulk-delete"], [
"class" => "btn btn-danger btn-xs",
'role' => 'modal-remote-bulk',delete this item'
]),
]) .
'<div class="clearfix"></div>',
]
])
?>
</div>
if you see from code above, please concern with
'before' => '<button type="button" class="btn btn-danger btn-secondary"> Not finished : <strong>'. $count_request_belum_selesai .'</strong> </button>',
When this button is clicked, I want to running a function in my model named RequestSearch :
public function searchRequestBelumSelesai(){
$query = Request::findAll(['tanggal_selesai' => NULL]);
return $query;
}
Which is the gridview will be displayed the result, is it possible ?
In the action of the controller that will receive the click of your button, you must modify the dataProvider given to the View, so that it looks like something like this:
$dataProvider= new ActiveDataProvider([
'query' => $your_model->searchRequestBelumSelesai(),
]);
I've just started with Yii (2.0) and I have a problem.
I need to make sure that the Maximum Order Quantity is always equal or greater than the Minimum Order Quantity. Which means, that you can't order a minimum of 10, and maximum of 5 for example.
Here is a screenshot.
And some code:
<?= $form->field($model, 'minimum_order_quantity')->widget(TouchSpin::classname(), [
'options' => [
'placeholder' => 'Minimum Order Quantity ...',
'class' => 'input-lg',
],
'pluginOptions' => [
'buttonup_class' => 'btn btn-primary',
'buttondown_class' => 'btn btn-info',
'buttonup_txt' => '<i class="glyphicon glyphicon-plus-sign"></i>',
'buttondown_txt' => '<i class="glyphicon glyphicon-minus-sign"></i>'
],
]) ?>
<?= $form->field($model, 'maximum_order_quantity')->widget(TouchSpin::classname(), [
'options' => [
'placeholder' => 'Maximum Order Quantity ...',
'class' => 'input-lg',
],
'pluginOptions' => [
'buttonup_class' => 'btn btn-primary',
'buttondown_class' => 'btn btn-info',
'buttonup_txt' => '<i class="glyphicon glyphicon-plus-sign"></i>',
'buttondown_txt' => '<i class="glyphicon glyphicon-minus-sign"></i>'
],
]) ?>
This would be the _form.php.
Thanks in advance!
You can do this with a rule in you Model:
[['maximum_order_quantity'], 'compare', 'compareAttribute' => 'minimum_order_quantity', 'operator' => '>='],