Yii2: Number format - php

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' => '$'
],

Related

TYPO3 BE TCA type Preview Image

i search for a Solution:
How can i add a Preview Image for a TCA type?
Example: I have 3 different types and would like to display a preview image for them. The same as the t3 backend layouts.
Just like the Backend-Layout:
Maybe there is a solution to this?
The documentation is explaining it:
https://docs.typo3.org/m/typo3/reference-tca/main/en-us/Columns/Examples.html#select-drop-down-for-records-represented-by-images
This is the example code:
[
'columns' => [
'select_single_12' => [
'label' => 'select_single_12 foreign_table selicon_field',
'config' => [
'type' => 'select',
'renderType' => 'selectSingle',
'foreign_table' => 'tx_styleguide_elements_select_single_12_foreign',
'fieldWizard' => [
'selectIcons' => [
'disabled' => false,
],
],
],
],
],
]
And the code for the field of the connected table is this:
[
'ctrl' => [
'title' => 'Form engine elements - select foreign single_12',
'label' => 'fal_1',
'selicon_field' => 'fal_1',
// ...
],
'columns' => [
// ...
'fal_1' => [
'label' => 'fal_1 selicon_field',
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
'fal_1',
[
'maxitems' => 1,
],
$GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext']
),
],
],
// ...
];
As you have a strange label inside your field it's not clear if you want to link to an image, to a text or even to a field that offers a combination like it's done with relations to the table sys_file by the intermediate table sys_file_ref.
So defining more precise what you need exactly might help to give you a more detailed answer. You can edit your answer to add this description.

Adding string filter and sorting to datacolumn gridview yii2

['header'=>'Bill No',
'attribute'=>'billid',
'filter'=>ArrayHelper::map(Bills::find()->all(),'id','billno'),
'format' =>'text',
'value'=> function($data){
return ($data->billid)?$data->bill->billno:'Nope';
}]
By using this code, I am getting a drop-down box as a filter, but bill number is going to be in 10000's. So I want an input type text as a filter for my foreign key.
Relationship:
DB Table:
Cashbook
Column: billed
On
DB Table:
Bills
Column:id
And I want to extract bill no from the id for this filter. Please Help
I want to search and sort for billno.
A better alternative is to use kartik\Select2 so that you can have the dropdown and search both 2 in 1.
There is an option for the minimum number of character that you have to type in to open the drop-down rather than opening the drop-down with 1000's of options at once by clicking on it.
minimumInputLength: Minimum number of characters required to start a search.
[
'header' => 'Bill No',
'attribute' => 'billid',
'filter' => \kartik\widgets\Select2::widget([
'data' => ArrayHelper::map(Bills::find()->all(),'id','billno'),
'model' => $searchModel,
'attribute' => 'billid',
'options' => [
'placeholder' => 'Bill No',
'id' => 'bill_id',
],
'theme' => \kartik\widgets\Select2::THEME_BOOTSTRAP,
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => '3',
],
]),
'format' =>'text',
'value'=> function($data){
return ($data->billid)?$data->bill->billno:'Nope';
}
],
Hope that helps you out

TYPO3 how to add virtual column to the TCA?

how can I add an virtual column to the TCA (TYPO3 8)? I have in a 1:n table with data and I want to display the count of the data in the backend to current element.
I need something like this:
$fields = [
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '7d'
]
]
],
'counts30d' => [
'exclude' => false,
'label' => 'last 30 days',
'config' => [
'type' => 'none',
'procFunc' => '\Namespace\MyClass->MyMethod',
'readOnly' => true,
'params' => [
'period => '30d'
]
]
],
];
pseudo function:
public function myMethod($element, $params){
$sql = "SELECT count(*) FROM TABLE WHERE pid=$element[uid] and date > $params[period]";
return sql_count…
}
The field should only be informative for the backend users.
Does anyone have an idea?
Thanks
Oliver
The TCA field type user is exactly what you are looking for:
'counts7d' => [
'exclude' => false,
'label' => 'last 7 days',
'config' => [
'type' => 'user',
'userFunc' => \Namespace\MyClass::class . '->MyMethod',
'parameters' => [
'period => '7d',
],
],
],
The TCA field type none is exactly what you are looking for. Type none is the only type that does not necessarily need a database field. To manipulate it you can use userFunc which allow you to use custom php function.

yii2 - how to set currency decimal value

I want my currency to ignore decimal value, so far I have this:
main.php:
'formatter' => [
'class' => 'yii\i18n\Formatter',
'thousandSeparator' => '.',
'decimalSeparator' => ',',
'currencyCode' => '€',
],
view:
[
'attribute' => 'Score',
'format' => 'currency',
],
Any idea on how to move forward?
The manual on currencyCode:
The 3-letter ISO 4217 currency code indicating the default currency to use
Try setting currencyCode to 'EUR' (though that doesn't seem to be that important) and put the formatter in an array
[
'attribute' => 'Score',
'format' => [
'currency',
'EUR',
[
\NumberFormatter::MIN_FRACTION_DIGITS => 0,
\NumberFormatter::MAX_FRACTION_DIGITS => 0,
]
],
],
This requires the PHP intl extension to be installed. Status of the extension can be tested by calling extension_loaded('intl'). In absence of the extension, your best bet is probably to write a custom formatter.
<?php
namespace app\components;
class Formatter extends \yii\i18n\Formatter
{
public function asRoundedCurrency($value, $currency)
{
return $this->asInteger(round($value)) . ' ' . $currency;
}
}
Use it instead of the default formatter an then call it like this:
[
'attribute' => 'Score',
'format' => ['roundedCurrency', 'EUR'],
]
This also allows you to freely set the currency symbol.
In main.php:
'formatter' => [
'class' => 'yii\i18n\Formatter',
'locale' => 'yourLocale', //ej. 'es-ES'
'thousandSeparator' => '.',
'decimalSeparator' => ',',
'currencyCode' => 'EUR',
],
Be sure that php_intl extensions is installed. It works for me.
Link to the documentation yii-i18n-formatter.

How to pass "elementId" in barcode generator in Gridview Yii2?

I have gone through this:
http://www.yiiframework.com/extension/yii-barcode-generator-8-types/
https://github.com/Vilochane/Yii-Barcode-Generator
http://www.yiiframework.com/extension/yii2-barcode-generator-8-types/
But doesn't get it work. My gridView:
<?= GridView::widget([
'dataProvider' => new yii\data\ActiveDataProvider(['query' => $model->getLibBookMasters()]),
'summary' => '',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'lbm_book_no',
[
'attribute' => 'lbm_barcode_no',
//'type' => 'raw',
'value'=> function($model){
return \barcode\barcode\BarcodeGenerator::widget(
[
'elementId' => 'lbm_barcode_no',
'value'=> 'lbm_barcode_no',
'type'=>'ean13',
]);},
],
],
]); ?>
I need to pass elementId that do the trick but doesn't found it.
I just installed Barcode Generator and don't know how to play around with.
You need to pass different elementIds. As your code is currently your are passing the literal 'lbm_barcode_no' instead of the value of the lbm_barcode_no attribute of your models. In addition, you have to create the divs where the barcode is to be shown and set the format of the column to raw or html:
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'lbm_book_no',
[
'attribute' => 'lbm_barcode_no',
'format' => 'raw',
'value'=> function($model){
return yii\helpers\Html::tag('div', '', ['id' => 'barcode-'.$model->lbm_barcode_no]).
\barcode\barcode\BarcodeGenerator::widget([
'elementId' => 'barcode-'.$model->lbm_barcode_no,
'value'=> $model->lbm_barcode_no,
'type'=>'ean13',
]);
},
],
],
I prefixed the tags with barcode- to avoid collisions (you never know).

Categories