Limit The Number Of Caracters Displayed In Cgridview Cells


It would be great to have an option for a column in CgridView in order to give a maximum number of characters displayed in a cell. Moreover, if we can add “…” when this number is bigger, it’s better.

Today, it’s necessary to write a function which does the work.

Why does a function not work? For a normal data column you can use an expression like

'value' => 'strlen($data->columnName) > 5 ? substr($data->columnName, 0, 5)."..." : $data->columnName'

yep, you can indeed write a function like you said, and that’s what I’m doing actually… but do it for every column is very boring… if there was an option in cgridview which took in account the number maximum of characters to display, it would be quicker and nicer than write array(name=>’…’,‘value’=>’…’) for every column.

You can extend CGridView, CDataColumn classes yourself and rewrite CGridView::initColumns(), CDataColumn::renderDataCellContent() methods. There is line in the CGridView::initColumns() method where CDataColumn instantiation happens. Text shortening can be made in the CDataColumn::renderDataCellContent() method.