GridView Character Limit - 2 Methods - Which is better?

I need to limit the characters of the email addresses in the GridView (zii.widgets.grid.CGridView), but unsure as to which is more “Yii” and generally better… I whacked in SUBSTR which works fine, but that feels like a hack… and Yii’s AFTERFIND function feels like its wasting processor effort or somthing…

Which is better?

Is it better to limit the characters via the Model, or the View? See below


'columns'=>array(

  'id',

  'id',

  'id',    

  array(

    'name'=>'client_email',

    'value'=>'substr($data->client_email, 0, 15)',

  ),

),


[code]protected function afterFind ()

  {

    parent::afterFind ();

    $this->client_email = substr($this->client_email, 0, 20);

  }

[/code]

Was reading an article over at Dana’s website: http://danaluther.blogspot.com/2012/02/leveraging-widgets-widget-factory-and.html

At almost the very bottom, she has some code that shows the below code:




<?php

        ‘htmlOptions’=>array(‘class’=>’post-grid-view minimal’),

        ‘columns’=>array(    

            ‘title’,

            ‘description:truncatedText:Desc.’,  <--  code of interest

        ),

    ),

);

?>



She uses a custom formatter (truncatedText) to handle that. I’m gathering that she overrode CFormatter and then added in her own methods. Haven’t yet done this / looked into it, but looks powerful!

Yeah, it’s interesting.

I use a helper function of my own to truncate a text.

It truncates a text if it is longer than the specified length, and appends ‘…’ at the end of the string if it has been truncated.




class SaHtml extends CComponent

{

	...

	public static function TruncateText($text, $max_len)

	{

		$len = mb_strlen($text, 'UTF-8');

		if ($len <= $max_len)

			return $text;

		else

			return mb_substr($text, 0, $max_len - 1, 'UTF-8') . '...';

	}

	...

}


...


        'value' => 'SaHtml::TrunctateText($data->text, 10)',




It’s for multi-byte characters.

Probably there are many among us that have their own version of this helper.

P.S.

I don’t think it’s a good idea to truncate text in ‘afterFind’ of the model. You want that text in full length when you display it in the detail view, don’t you?

I’ve just noticed this actually, afterFind() is placing ‘…’ in an empty create field, and also, when you view the update field the email is truncated too… so, i guess for my usage, I should apply to the views… :lol:

I’ll take alook at the code above, thanks Softark, Fr0d0z…

When I use Sofark’s Helper, I get an error: Fatal error: Call to undefined method Helpers::trunctateText() in… But my other Helpers work… I also added extends CComponent, but no improvement.

I’ve changed it to suit my Helper class

Helpers.php Class Helpers


  public static function truncateText($text, $max_len)

  {

    $len = strlen($text, 'UTF-8');

    if ($len <= $max_len)

      return $text;

    else

      return substr($text, 0, $max_len - 1, 'UTF-8') . '...';

  }




<?php $this->widget('zii.widgets.grid.CGridView', array(

   'id'=>'leads-grid',

   'dataProvider'=>$model->search(),

   'filter'=>$model,

   'columns'=>array(

   ...

   array(

      'name'=>'client_email',

      //'value'=>'substr($data->client_email, 0, 14)',

      'value' => 'Helpers::trunctateText($data->client_email, 14)',

   ),

   ...




Just another example:




// in model

function GetTruncatedClientEmail()

{

    if (strlen($this->client_email) <= 14)

        return $this->client_email;

    else

        return substr($this->client_email, 0, 14) . '...';

}


// in view

array(

  'name'=>'client_email',

  'value'=>'$data->GetTruncatedClientEmail()',

),



This works, great, thank you.

I will use that for now, but i prefer the single function approach withthe helper, as I will need to create a function for every field that might need truncating…