how to show image instead of text with link - php

This my grid view
echo GridView::widget([
'dataProvider' => $provider,
'columns' => [
'name',
]
]);
it will showing name of file with location , But i want to show if file is pdf ,pdf image will show , if file is doc doc image will show with download option, I can do code for downloading the file but i don't know how to show image and download link instead of text.

You can also try this way:
echo GridView::widget([
'dataProvider' => $provider,
'columns' => [
'name',
[
'attribute' => 'url',
'format' => 'raw',
'value' => function ($dataProvider) {
return Html::img($dataProvider->url, ['class' => 'img- rounded'], ['alt' => '']);
}
],
]
]);

Try this:
echo GridView::widget([
'dataProvider' => $provider,
'columns' => [
'name',
'imageColumn:image',
'urlColumn:url'
]
]);

Related

Modifty GridView::Widget content Yii

I'm trying to modify a field of the GridView that I obtained following the Gii tutorial on the Yii framework website.
GII PAGE
I'm not satisfied on how the population field looks, so I'm trying to convert it with some separators.
This is the index.php of the Country View
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
'name',
[
'label' => 'Population',
'value' => 'population',
/* 'value' => Yii::$app->formatter->asDecimal((integer)population) */
/* 'value' => Yii::$app->formatter->asDecimal($model->population) */
/* 'value' => Yii::$app->formatter->asDecimal('population') */
/*'value' => Yii::$app()->format->number('population')*/
],
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
I don't know why in the CountryModel the population field is listed as integer:
['population'], 'integer'
And then when I try to convert it in the view I have some problems because 'population' is basically a String.
I commented out some of my attempts.
You can use yii\i18n\Formatter
Go to your common\config\main.php if you are using app-advanced or the app/config/main.php if app-basic and add the following under components array.
'formatter' => [
'thousandSeparator' => ',',
],
Now you can format any given number like below
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'code',
'name',
[
'attribute' => 'population',
'label' => 'Population',
'value' => function($model){
return Yii::$app->formatter->asInteger($model->population); //Output 1,000
},
],
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
Update: Like was mentioned in comments, You can also use this two technics to format the value:
//Gridview
....
[
'attribute' => 'population',
'format' => 'integer',
],
....
and/or:
//Gridview
....
'population:integer',
....

How to create grid view in Yii2 (after clicking on particular id from list open all detail of that id )?

In index.php of Member module I have write this widget to open detail after click on id.but here open a Url code as it is not url page.please give me solution. and suggestion what i use because i am not familiar with Yii2.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=>'practiceCode',
'format' => 'url',
'value'=>function ($data) {
return Html::a(Html::encode("View"),'practice/view');
},
],
'memberCode',
'firstName',
'lastName',
'email:email',
'mobile',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'lable'=>'practiceCode',
'format' => 'raw',
'value'=>function ($model, $index, $widget){
return Html::a($model->practiceCode,['practice/view','id'=>$model->practiceCode],['title'=>'Go!','target'=>'_blank']
);
// return Html::a(Html::encode("View"),'practice/view');
// return Html::a(Html::encode($data- >practiceCode),'practice/view');
},
],
Now it's working 'attribute'=>'practiceCode' instead of 'lable'=>'practiceCode',

Outputting data from table after saving with ckeditor

Am saving data using the ckeditor in yii2 in my form model but when doing the view action it displays the html tags
CKEDITOR CODE:
<?= $form->field($model, 'case_description')->widget(CKEditor::className(),[
'editorOptions' => [
'preset' => 'full',
'inline' => false,
],
]);
?>
So after I save the data in the table its saved having the html codes
example of saved data:
<b>My new project being grilled</b>
So when viewing the data using yii2 detail and gridview it always shows the <b> instead of being bold.
How can I solve the problem
EXAMPLE: of grid view outputting it in the case_description column
<?= GridView::widget([
'summary'=>"",
'showOnEmpty'=>false,
'dataProvider' => $dataProviderb,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'case_description',
],
]) ?>
You just use the 'format' => 'row' in your GridView like:
<?= GridView::widget([
'summary'=>"",
'showOnEmpty'=>false,
'dataProvider' => $dataProviderb,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'case_description',
'format' => 'raw',
],
],
]) ?>

Yii2: Kartik Gridview sum of a column in footer

I have installed the Kartik gridview extension, which is working fine.
But I couldn't find or missed it in the docs, how I can show the sum of a column in the footer.
This is my complete code in index.php
<?php
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'id',
[
//'attribute'=>'service_name',
'attribute'=>'service_name',
'value'=>'serviceName.services',
],
[
'attribute'=>'room_category',
'value'=>'roomCategory.room_category'
],
'charges_cash',
'charges_cashless',
['class' => 'yii\grid\ActionColumn']
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'fontAwesome' => true,
'showPageSummary' => true,
'dropdownOptions' => [
'label' => 'Export All',
'class' => 'btn btn-default'
]
])
?>
</div></div>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
//'service_name',
[
//'attribute'=>'service_name',
'attribute'=>'service_name',
'value'=>'serviceName.services',
],
// 'room_category',
[
'attribute'=>'room_category',
'value'=>'roomCategory.room_category'
],
'charges_cash',
'charges_cashless',
['class' => 'yii\grid\ActionColumn'],
],
'showFooter' => true
]); ?>
</div>
Looking for some help on this one.
Thanks.
I think you just need to add the page summary;
use kartik\grid\GridView;
// Create a panel layout for your GridView widget
echo GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'showPageSummary' => true
]);
Kartik describes it pretty well in the demo and plugin details.
Complete example:
GridView::widget([
'dataProvider'=> $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'class' => 'kartik\grid\ActionColumn',
'urlCreator' => function($action, $model, $key, $index) {
// using the column name as key, not mapping to 'id' like the standard generator
$params = is_array($key) ? $key : [$model->primaryKey()[0] => (string) $key];
$params[0] = \Yii::$app->controller->id ? \Yii::$app->controller->id . '/' . $action : $action;
return Url::toRoute($params);
},
'contentOptions' => ['nowrap'=>'nowrap']
],
'id',
'name',
[
'attribute'=>'total_quantity',
'pageSummary' => true
],
[
'attribute'=>'quantity_sold',
'pageSummary' => true
],
],
'showPageSummary' => true
]);
Note: please rename column class from yii\grid to kartik\grid\ . This goes for DataColum, ActionColumn etc

yii2 : how to keep the url same in kartik grid view?

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,
],
],

Categories