1
votes

I am using yii2 gridview, and i have a field named description which contains all rich html text with style and format.

Now i know how to display that data in gridview and I can limit the character to be displayed in grid for that column as well

But i have 2 problems now

1) I used a function in value property to truncate data so i lost ability to search from description. So how can I truncate text and be able to search in that column as well Here is code I used

[
    'format' => 'html',
    'label' => 'Description',
    'value' => function ($dataProvider) {
        $url = $dataProvider->description;
        return BaseStringHelper::truncateWords($url,30,null,true);
    },
],

2)This does solve the primary problem but after i use that method i have problem with displaying images in the gridview column. I want to trim the img tag if any from description data and display data in gridview and be able to search as well

I hope you guys understand my question. Let me know what can i do

Thank you

1

1 Answers

1
votes

1) cant you just make one extra data column in your DB table, then do:

$formattedDesc = BaseStringHelper::truncateWords($url,30,null,true);
$model->formattedDescription = $formattedDesc;

when you are importing data to your db, to fetch data. This you would normally display data from that cell into your gridView.

///

to be able to search again, just add

'attribute' => 'your column name',

You will probably need to bind your new attribute into your search model

  ->andFilterWhere(['like or = /// etc.','columnt that you want to search   in//',
$this->getAttribute('attribute name')]);

/* be aware that the search will be made with data from "db column", and that db column data will be displayed ( with your formatted value )

To add custom attributes to your search model:

public function attributes() {
    return array_merge(parent::attributes(), ['customAttribute']);
}

public function rules(){
    return [
           [['customAttribute'], 'safe'],
           ];
 }