Yii Framework Forum: CGridView::rowCssStyleExpression - a little improvement suggestion - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CGridView::rowCssStyleExpression - a little improvement suggestion css style for row Rate Topic: -----

#1 User is offline   kiaplayer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 16-February 11

Posted 19 June 2011 - 08:36 AM

Hello.

CGridView's property "rowCssClassExpression" can evaluate css class while rendering.
Sometimes it's useful to evaluate css style, not class.
Like this:

$this->widget('MyGridView', array(
  ...
  'rowCssStyleExpression' => '$data->login == "admin" ? "background-color:#FFC4D3;" : ""',
  ...
));


<?php

Yii::import('zii.widgets.grid.CGridView');

class MyGridView extends CGridView {

  /**
   * @var string a PHP expression that is evaluated for every table body row and whose result
   * is used as the CSS style for the row. In this expression, the variable <code>$row</code>
   * stands for the row number (zero-based), <code>$data</code> is the data model associated with
   * the row, and <code>$this</code> is the grid object.
   */
  public $rowCssStyleExpression;

  /**
   * Renders a table body row.
   * @param integer $row the row number (zero-based).
   */
  public function renderTableRow($row) {

    $data = $this->dataProvider->data[$row];

    $rowClass = '';
    if ($this->rowCssClassExpression !== null) {
      $rowClass = $this->evaluateExpression($this->rowCssClassExpression, array('row' => $row, 'data' => $data));
    }
    else if (is_array($this->rowCssClass) && ($n = count($this->rowCssClass)) > 0) {
      $rowClass = $this->rowCssClass[$row % $n];
    }

    $rowStyle = '';
    if ($this->rowCssStyleExpression !== null) {
      $rowStyle = $this->evaluateExpression($this->rowCssStyleExpression, array('row' => $row, 'data' => $data));
    }

    $rowAttributes = '';
    if ($rowClass) {
      $rowAttributes .= ' class="'.$rowClass.'" ';
    }
    if ($rowStyle) {
      $rowAttributes .= ' style="'.$rowStyle.'" ';
    }

    echo '<tr '.$rowAttributes.'>';

    foreach ($this->columns as $column) {
      $column->renderDataCell($row);
    }

    echo "</tr>\n";
    
  }

}

0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,691
  • Joined: 10-October 10
  • Location:Denmark

Posted 19 June 2011 - 08:54 AM

Why is that useful over a css class/id ?
Using a css class is so much more DRY and object oriented than hardcoding a style.
IMO.
"Less noise - more signal"
0

#3 User is offline   kiaplayer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 16-February 11

Posted 19 June 2011 - 01:35 PM

View Postjacmoe, on 19 June 2011 - 08:54 AM, said:

Why is that useful over a css class/id ?

For example, when color is data field.
Or you need some temp highlighting and you dont want to propagate css classes.
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,691
  • Joined: 10-October 10
  • Location:Denmark

Posted 19 June 2011 - 02:21 PM

You can pass htmlOptions to a column:
                	'htmlOptions'=>array('width'=>'6%','class'=>'progress'),

Another example:
                	'htmlOptions'=>array('width'=>'6%','class'=>Helper::classFromFieldName($data->name)),


And I believe you can also just pass a 'style' argument in the htmlOptions array, although I've never had the need to try it. :)

I just feel such a feature would be redundant.

<edit>
Judging from your new topic, I guess you are on to that solution already. ;)
</edit>
"Less noise - more signal"
0

#5 User is offline   kiaplayer 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 16-February 11

Posted 19 June 2011 - 11:25 PM

Thanks for your replies :)

View Postjacmoe, on 19 June 2011 - 02:21 PM, said:

You can pass htmlOptions to a column.
And I believe you can also just pass a 'style' argument in the htmlOptions array.

Yes, you can. But it will be a static style (not evaluated while rendering). Besides it will be applied for a single column only, not for a whole row.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users