I have a base64 image, and I want to display it in the following dataView
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'nome:ntext',
'img1:ntext', // here is the base64 string that is also accessed by "$ model-> img1"
],
]) ?>
I tried the following code, but it did not work
[
'attribute' => 'img1',
'value' => base64_decode($model->img1),
'format' => ['image', ['width' => '100', 'height' => '100']]
],
Try something like this
[
'attribute' => 'img1',
'value' => 'data:image/png;base64,' . $model->img1,
'format' => ['image', ['width' => '100', 'height' => '100']]
],
or
[
'attribute' => 'img1',
'value' => 'data:image/jpeg;base64,' . $model->img1,
'format' => ['image', ['width' => '100', 'height' => '100']]
],
depending on image type.
BTW: If you're storing images in database you may want to read Store pictures as files or in the database for a web app? (and tens of duplicates). It is quite likely that you will have a lot of problems because of this approach.
Related
I want to render/show an image in a view on backend, but the image location is in frontend/web/uploads.
I tried doing this on the DetailView::widget():
...
[
'attribute'=>'image',
'value'=>('/frontend/web/uploads/'.$model->image),
'format' => ['image',['width'=>'100','height'=>'100']],
],
But the image is not showing up. Any help?
Yes it is possible to access frontend/web/uploads to backend view.
First add urlManagerFrontend in backend->config->main.php like
'components' => [
'urlManagerFrontend' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'frontend/web/', //Access frontend web url
],
],
And then access the frontend/web/ to the backend like
Yii::$app->urlManagerFrontend->baseUrl.'/uploads/your_image.jpg'
In DetailView::widget like
[
'attribute'=>'image',
'value'=> function ($model) {
return Html::img(Yii::$app->urlManagerFrontend->baseUrl.'/uploads/'.$model->image, ['alt' => 'No Image', 'width' => '10px', 'height' => '10px']);
},
'format' => 'raw',
],
If this is the only place where you're linking to frontend from backend, the easiest way would be defining alias in your backend config:
Yii::setAlias('#frontendUploads', 'https://repasca.devs/uploads');
And use this alias for building file URL:
[
'attribute' => 'image',
'value' => Yii::getAlias('#frontendUploads') . '/' . $model->image,
'format' => ['image', ['width' => '100', 'height' => '100']],
],
How can I make a URL in DetailView row on yii2?
For example: blow code is something like custom row in GridView, but as you know DetailView is different and this code not works in DetailView.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'attribute' => 'file',
'label' => 'File',
'fomat' => 'Url', // I want something like this
'value' => Html::a('Dowaload The File', ['files/uloads']),
],
])
I have not found my problem's solution in similar questions.
Please help me to create Url row in DetailView.
You can use either the shorthand url formatter (that does not provide much customization options):
'attributes' => [
'file:url'
]
Or the raw format with which you can customize everything:
'attributes' =>[
[
'attribute'=>'file',
'label'=>'File',
'format'=>'raw',
'value'=>Html::a('Download the File', url),
],
...
You can create a download link to the file inside the detail view in the following way, I am assuming that the file path for download is coming from the database field file.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'attribute' => 'file',
'label' => 'File',
'value' => function ($model) {
return Html::a('Download The File', $model->file);
},
'format' => 'raw',
],
]
]);
how can I set width of column of gridview in view yii2,
reference link as below,
try this,
<?=
GridView::widget([
'model' => $model,
'options' => ['style' => 'width:80%'],
'attributes' => [
'id',
],
])
?>
To set width of one column individually something like the following:
[
'attribute' => 'attribute_name',
'headerOptions' => ['style' => 'width:20%'],
],
For all columns try:
[
'class' => 'yii\grid\SerialColumn',
'contentOptions' => ['style' => 'width:100px;'],
],
I have a view displaying a grid view for a particular model. In my local machine, it's working well but when I deploy the application to another server, an attribute is not found hence the Unknown Property Exception. When I look at the code though, the attribute is there.
Any ideas?
Here is the model class code: http://codebin.org/view/f0a713c1
The view code:
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'is_condemned',
'label' => 'Condemned',
'class' => '\kartik\grid\BooleanColumn',
'falseLabel' => 'Active',
'trueLabel' => 'Condemned'
],
],
// set your toolbar
'toolbar' => [
['content' =>
Html::a(FA::icon('plus') . ' Add', ['/equipment/default/create'], ['class' => 'btn btn-success'])
],
'{export}',
'{toggleData}',
],
// set export properties
'export' => [
'fontAwesome' => true,
'filename' => 'equipment-export-'.time(),
'exportConfig' => [
'html' => ['showCaption' => FALSE],
'pdf' => ['showCaption' => FALSE],
],
],
'bordered' => TRUE,
'striped' => TRUE,
'condensed' => TRUE,
'responsive' => TRUE,
'hover' => TRUE,
'showPageSummary' => TRUE,
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => '',
],
'persistResize' => false,
]);
The reason why it wasn't working was that the model class being imported was in lowercase. Apparently, I entered the wrong value in gii. It was just in the other servers that it was case-sensitive.
Could be either a problem with case (Uppercase, lowercase) in the namespace or in the class name or backslash path problem (/, \). In this case the class is not found and Yii shows this message.
I have defined a column in Database as float.
The column is being shown as number in the model.
I want to format the columnn in Gridview with two decimal places and couldn't find a way, how it can be done.
I have tried to use this code but getting the error like - Unknown format type: number
[
'label' => 'Charges',
'attribute' => 'category_charges',
'contentOptions' => ['class' => 'col-lg-1'],
'format' => ['number',2]
],
Thanks.
The correct syntax of formatting decimal number is like below:
'format'=>['decimal',2]
So your code should be:
[
'label' => 'Charges',
'attribute' =>'category_charges',
'contentOptions' => ['class' => 'col-lg-1'],
'format'=>['decimal',2]
],
To be more familiar with Yii2's formatting take a look at the official document:
Class yii\i18n\Formatter
Set grid column like this
'columns' => [
[
'format' => 'Currency',
'attribute' => 'amount',
],
],
in main.php add under 'components':
'formatter' => [
'class' => 'yii\i18n\formatter',
'thousandSeparator' => ',',
'decimalSeparator' => '.',
'currencyCode' => '$'
],