V
V
VitaliyKaliuzhyn2017-03-21 10:44:31
Yii
VitaliyKaliuzhyn, 2017-03-21 10:44:31

Formatting output in Gridview yii2?

There is such a code

<?= GridView::widget([
        'filterUrl' => Url::toRoute('index'),
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'attribute'=> 'body',
                'format'=> 'text'],
            ],
    ]); ?>

which displays a fragment of the page, with all html tags. If you change format => 'html', then it displays a beautiful html page block.
How to make it so that only text would be displayed without any html code.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2017-03-21
@VitaliyKaliuzhyn

[
                'attribute'=> 'body',
               'value' => function($model){return strip_tags($model->body);},
            ],

but I would put in the model:
public function getPureBody(){
return strip_tags($this->body); //тут же можно добавить обрезку по длинный, если это длинный текст
}

and then in gridView:
'columns' => [
[
                'attribute'=> 'body',
               'value' => 'pureBody'},
            ],
]

or even:
'columns' => [
'pureBody'
]

C
Crash, 2017-03-21
@Bandicoot

[
     'attribute'=> 'body',
     'format'=> 'raw',
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question