i'm currently implementing an export function for our yii2 based application. (An humhub module)
I use for this the "kartik-v/yii2-export" module.
The action contains:
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'id',
'language',
'time_zone',
['class' => 'yii\grid\ActionColumn'],
];
$dataProvider = new SqlDataProvider([
'sql' => 'SELECT * FROM user WHERE status=:status'
]);
return $this->render('export',[
'dataProvider' => $dataProvider,
'gridColumns' => $gridColumns
]);
The view contains:
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns
]);
When I klick on the export function a new popup windows opens with an information that it creates the file now. After a few seconds it should start a download, but redirects to the destination page.
Do I need to add something to the controller or did I do anything else wrong?
Thanks for your help.
It's not clearly defined if that you need the "exportRequestParam" needs to be set.
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'exportRequestParam' => 'user'
]);
This way it works.
Related
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();
},
],
I've broken my mind..
I try to add img in HTML in Yii2.
I load it from db and put into view file, but when I try to return it in HTML tags it throw error.
But the picture is adding perfectly (checked via var_dump)
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'format' => 'html',
'label' => 'Image',
'value' => function($data){
return Html::img($data->getImage(), ['width'=>200]);
}
],
'id',
'mark',
'model',
'colour',
'state_num',
// 'price',
// 'status',
// 'foto:ntext',
// 'description:ntext',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I wanna see a picture in table. What happened with HTMLPurifier?
My img is saving as VARCHAR(255) in db.
PHP User Error – yii\base\ErrorException
Unserialization of configuration schema failed, sha1 of file was
da39a3ee5e6b4b0d3255bfef95601890afd80709
I've changed format to "raw", and it's started to work
I am using kartik grid view to display my data in yii 2 with pjax enabled. Every time, I search a data in the grid view, the search is done using ajax but the url keeps changing. Is there a way to keep the url unchanged? Please help me with the solution. Here is my code:
<?php use kartik\grid\GridView;?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'pjaxSettings'=>[
'neverTimeout'=>true,
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'hotel_id',
'name',
'address',
'phone_no',
'contact_person',
// 'email_address:email',
// 'website',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
You can disable pushState feature like this:
'pjax' => true,
'pjaxSettings' => [
'options' => [
'enablePushState' => false,
],
],
First ... old school, rusty Pascal/Clipper/VB developer trying to learn PHP and Yii2 at the same time.
Looking at http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$template-detail I should be able to easily remove the update and delete buttons in the ActionColumn ... I just don't know how / where to set the property.
Our GridView code is:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'last_name',
'first_name',
'email1:email',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
My gut tells me it's something to do with instantiating the ActionColumn class before we throw that in as a column ... but I've combed the forums and documentation and just can't find any example.
You are searching for the template properly. http://www.yiiframework.com/doc-2.0/yii-grid-actioncolumn.html#$template-detail
your column should be
[
'class' => 'yii\grid\ActionColumn',
'template' => "{view}",
],
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'],
],
]); ?>