Krajee

Explicit Cell Formats

Thankful to Krajee! BUY A COFFEEor to get more out of us.
Allow to set and override explicit cell formats by setting the cellFormat within the column's headerOptions, contentOptions, or footerOptions. You can also set cellFormat for each cell configured within ExportMenu::contentBefore and ExportMenu::contentAfter. The value for cellFormat must be one of the PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_ constants. This setting will override any other autoFormat setting.

Tip

Not seeing the updated content on this page! Hard refresh your browser to clean cache for this page (e.g. SHIFT-F5 on Windows Chrome)


Showing 1-5 of 5 items.
#Lot IDFruitQuantityValue
110032460001Apples100$213,288.32
210032460002Oranges60$84,343.68
310032460003Bananas160$49,343.65
410032460004Pineapples90$72,631.02
510032460005Grapes290$627,653.65
use kartik\export\ExportMenu;
use kartik\grid\GridView;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use yii\data\ArrayDataProvider;

$dataProvider = new ArrayDataProvider(['allModels' => [
    ['id' => '10032460001', 'fruit' => 'Apples', 'quantity' => '100', 'value' => 213288.32],
    ['id' => '10032460002', 'fruit' => 'Oranges', 'quantity' => '60', 'value' => 84343.68],
    ['id' => '10032460003', 'fruit' => 'Bananas', 'quantity' => '160', 'value' => 49343.65],
    ['id' => '10032460004', 'fruit' => 'Pineapples', 'quantity' => '90', 'value' => 72631.02],
    ['id' => '10032460005', 'fruit' => 'Grapes', 'quantity' => '290', 'value' => 627653.65],
]]);
echo ExportMenu::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        [
            'class' => 'kartik\grid\SerialColumn'
        ],
        [
            'attribute' => 'id',
            'header' => 'Lot ID',
            'width' => '260px',
            'hAlign' => GridView::ALIGN_CENTER,
            'contentOptions' => ['cellFormat' => DataType::TYPE_STRING], // set an explicit format
            //'headerOptions' => ['cellFormat' => DataType::TYPE_STRING], // can set at header level
            //'footerOptions' => ['cellFormat' => DataType::TYPE_STRING], // can set at footer level
            //also can be set for each cell config within `contentBefore` and `contentAfter`
        ],
        'fruit',
        [
            'attribute'=>'quantity',
            'hAlign' => GridView::ALIGN_RIGHT,
            'width' => '160px',
            'format' => 'decimal',
            'contentOptions' => ['cellFormat' => DataType::TYPE_NUMERIC], // set an explicit format
        ],
        [
            'attribute'=>'value',
            'hAlign' => GridView::ALIGN_RIGHT,
            'width' => '160px',
            'format' => 'currency'
        ],
    ],
    'dropdownOptions' => [
        'label' => 'Export All',
        'class' => 'btn btn-outline-secondary btn-default'
    ]
]) . "<hr>\n".
GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'kartik\grid\SerialColumn'],
        [
            'attribute' => 'id',
            'header' => 'Lot ID',
            'width' => '260px',
            'hAlign' => GridView::ALIGN_CENTER
        ],
        'fruit',
        [
            'attribute'=>'quantity',  
            'hAlign' => GridView::ALIGN_RIGHT,
            'width' => '160px',
            'format' => 'decimal'
        ],
        [
            'attribute'=>'value', 
            'hAlign' => GridView::ALIGN_RIGHT,
            'width' => '160px',
            'format' => 'currency'
        ],
    ],
]);
$gridColumns = [
    ['class' => 'kartik\grid\SerialColumn'],
    'id',
    'name',
    [
        'attribute'=>'author_id',
        'label'=>'Author',
        'vAlign'=>'middle',
        'width'=>'190px',
        'value'=>function ($model, $key, $index, $widget) { 
            return Html::a($model->author->name, '#', []);
        },
        'format'=>'raw'
    ],
    'color',
    'publish_date',
    'status',
    ['attribute'=>'buy_amount','format'=>['decimal',2], 'hAlign'=>'right', 'width'=>'110px'],
    ['attribute'=>'sell_amount','format'=>['decimal',2], 'hAlign'=>'right', 'width'=>'110px'],
    ['class' => 'kartik\grid\ActionColumn', 'urlCreator'=>function(){return '#';}]
];

Note

You can now visit the Krajee Webtips Q & A forum for searching OR asking questions OR helping programmers with answers on these extensions and plugins. For asking a question click here. Select the appropriate question category (i.e. Krajee Plugins) and choose this current page plugin in the question related to field.

The comments and discussion section below are intended for generic discussions or feedback for this plugin. Developers may not be able to search or lookup here specific questions or tips on usage for this plugin.

 
visitors to Krajee Yii2 Demos since 22-May-2017