Package | zii.widgets.grid |
---|---|
Inheritance | class CDataColumn » CGridColumn » CComponent |
Since | 1.1 |
Source Code | framework/zii/widgets/grid/CDataColumn.php |
Property | Type | Description | Defined By |
---|---|---|---|
cssClassExpression | string | a PHP expression that is evaluated for every data cell and whose result is used as the CSS class name for the data cell. | CGridColumn |
filter | mixed | the HTML code representing a filter input (eg a text field, a dropdown list) that is used for this data column. | CDataColumn |
filterCellContent | string | Returns the filter cell content. | CDataColumn |
filterHtmlOptions | array | the HTML options for the filter cell tag. | CGridColumn |
footer | string | the footer cell text. | CGridColumn |
footerCellContent | string | Returns the footer cell content. | CGridColumn |
footerHtmlOptions | array | the HTML options for the footer cell tag. | CGridColumn |
grid | CGridView | the grid view object that owns this column. | CGridColumn |
hasFooter | boolean | whether this column has a footer cell. | CGridColumn |
header | string | the header cell text. | CGridColumn |
headerCellContent | string | Returns the header cell content. | CDataColumn |
headerHtmlOptions | array | the HTML options for the header cell tag. | CGridColumn |
htmlOptions | array | the HTML options for the data cell tags. | CGridColumn |
id | string | the ID of this column. | CGridColumn |
name | string | the attribute name of the data model. | CDataColumn |
sortable | boolean | whether the column is sortable. | CDataColumn |
type | string | the type of the attribute value. | CDataColumn |
value | string | a PHP expression that will be evaluated for every data cell using evaluateExpression and whose result will be rendered as the content of the data cell. | CDataColumn |
visible | boolean | whether this column is visible. | CGridColumn |
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__construct() | Constructor. | CGridColumn |
__get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
__isset() | Checks if a property value is null. | CComponent |
__set() | Sets value of a component property. | CComponent |
__unset() | Sets a component property to be null. | CComponent |
asa() | Returns the named behavior object. | CComponent |
attachBehavior() | Attaches a behavior to this component. | CComponent |
attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
attachEventHandler() | Attaches an event handler to an event. | CComponent |
canGetProperty() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
detachBehavior() | Detaches a behavior from the component. | CComponent |
detachBehaviors() | Detaches all behaviors from the component. | CComponent |
detachEventHandler() | Detaches an existing event handler. | CComponent |
disableBehavior() | Disables an attached behavior. | CComponent |
disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
enableBehavior() | Enables an attached behavior. | CComponent |
enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
evaluateExpression() | Evaluates a PHP expression or callback under the context of this component. | CComponent |
getDataCellContent() | Returns the data cell content. | CDataColumn |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
getFilterCellContent() | Returns the filter cell content. | CDataColumn |
getFooterCellContent() | Returns the footer cell content. | CGridColumn |
getHasFooter() | Returns whether this column has a footer cell. This is determined based on whether footer is set. | CGridColumn |
getHeaderCellContent() | Returns the header cell content. | CDataColumn |
hasEvent() | Determines whether an event is defined. | CComponent |
hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
hasProperty() | Determines whether a property is defined. | CComponent |
init() | Initializes the column. | CDataColumn |
raiseEvent() | Raises an event. | CComponent |
renderDataCell() | Renders a data cell. | CGridColumn |
renderFilterCell() | Renders the filter cell. | CGridColumn |
renderFooterCell() | Renders the footer cell. | CGridColumn |
renderHeaderCell() | Renders the header cell. | CGridColumn |
Method | Description | Defined By |
---|---|---|
renderDataCellContent() | Renders the data cell content. | CGridColumn |
renderFilterCellContent() | Renders the filter cell content. | CGridColumn |
renderFooterCellContent() | Renders the footer cell content. | CGridColumn |
renderHeaderCellContent() | Renders the header cell content. | CGridColumn |
the HTML code representing a filter input (eg a text field, a dropdown list) that is used for this data column. This property is effective only when CGridView::filter is set. If this property is not set, a text field will be generated as the filter input; If this property is an array, a dropdown list will be generated that uses this property value as the list options. If you don't want a filter for this data column, set this value to false.
Returns the filter cell content. This method will return the filter as is if it is a string. If filter is an array, it is assumed to be a list of options, and a dropdown selector will be rendered. Otherwise if filter is not false, a text field is rendered.
Returns the header cell content. This method will render a link that can trigger the sorting if the column is sortable.
the attribute name of the data model. Used for column sorting, filtering and to render the corresponding attribute value in each data cell. If value is specified it will be used to rendered the data cell instead of the attribute value.
whether the column is sortable. If so, the header cell will contain a link that may trigger the sorting. Defaults to true. Note that if name is not set, or if name is not allowed by CSort, this property will be treated as false.
the type of the attribute value. This determines how the attribute value is formatted for display. Valid values include those recognizable by CGridView::formatter, such as: raw, text, ntext, html, date, time, datetime, boolean, number, email, image, url. For more details, please refer to CFormatter. Defaults to 'text' which means the attribute value will be HTML-encoded.
a PHP expression that will be evaluated for every data cell using evaluateExpression and whose result will be rendered as the content of the data cell. In this expression, you can use the following variables:
$row
the row number (zero-based).$data
the value provided by grid view object for the row.$this
the column object.$data
depends on data provider which is passed to the
grid view object. In case of CActiveDataProvider, $data
will have
object type and its values are accessed like $data->property
. In case of
CArrayDataProvider or CSqlDataProvider, it will have array type and its values must be
accessed like $data['property']
.
public string getDataCellContent(integer $row)
| ||
$row | integer | the row number (zero-based) |
{return} | string | the data cell content. |
public function getDataCellContent($row)
{
$data=$this->grid->dataProvider->data[$row];
if($this->value!==null)
$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
elseif($this->name!==null)
$value=CHtml::value($data,$this->name);
return $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
}
Returns the data cell content. This method evaluates value or name and renders the result.
public string getFilterCellContent()
| ||
{return} | string | the filter cell content |
public function getFilterCellContent()
{
if(is_string($this->filter))
return $this->filter;
elseif($this->filter!==false && $this->grid->filter!==null && $this->name!==null && strpos($this->name,'.')===false)
{
if(is_array($this->filter))
return CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));
elseif($this->filter===null)
return CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));
}
else
return parent::getFilterCellContent();
}
Returns the filter cell content. This method will return the filter as is if it is a string. If filter is an array, it is assumed to be a list of options, and a dropdown selector will be rendered. Otherwise if filter is not false, a text field is rendered.
public string getHeaderCellContent()
| ||
{return} | string | the header cell content. |
public function getHeaderCellContent()
{
if($this->grid->enableSorting && $this->sortable && $this->name!==null)
return $this->grid->dataProvider->getSort()->link($this->name,$this->header,array('class'=>'sort-link'));
elseif($this->name!==null && $this->header===null)
{
if($this->grid->dataProvider instanceof CActiveDataProvider)
return CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
else
return CHtml::encode($this->name);
}
else
return parent::getHeaderCellContent();
}
Returns the header cell content. This method will render a link that can trigger the sorting if the column is sortable.
public void init()
|
public function init()
{
parent::init();
if($this->name===null)
$this->sortable=false;
if($this->name===null && $this->value===null)
throw new CException(Yii::t('zii','Either "name" or "value" must be specified for CDataColumn.'));
}
Initializes the column.
Adding any yii widget to CDataColumn
This is my method to adding any yii widget to the CDataColumn. Example:
... CGridView declarations ... array( 'name'=>'userId', 'value'=>'Controller::createWidget("zii.widgets.jui.CJuiAutoComplete",array( "model"=>$data, "attribute"=>"userId", "sourceUrl"=>array("/user/jsonData"), ))->run()', ),
I don't know if this look ok, but i change one line in CJuiAutoComplete from
$js = "$('#{$id}').autocomplete($options);";
to
$js = "$('#{$id}').livequery(function(){ $(this).autocomplete($options); });";
and registering jquery.livequery script (on layout or other view related files)
Yii::app()->clientScript->registerScriptFile('/jquery.livequery.js');
We can download jquery.livequery from http://plugins.jquery.com/project/livequery
Passing php function to value
It should be noted that in value, we can also pass functions, such as:
array( 'name'=>'something', 'value'=>function($data,$row){ // declare signature so that we can use $data, and $row within this function return $data->something." in ".$row;// echo also works } )
To use an outside variable within the anonymous function we use the
use
keyword:array( 'name'=>'something', 'value'=>function($data,$row) use ($outsideVariable){ // declare signature so that we can use $data, and $row within this function // also allows us to use outside (external) variables, that are not defined within grid, return $data->something." in ".$row." with ".$outsideVariable;// echo also works } )
Row numbering
I used the following to show row number in the data provider's sequence:
$controller->widget( 'zii.widgets.grid.CGridView', array( 'columns' => array( array( 'header' => 'Rank', 'value' => '$row + $this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize', ),
Not very pretty, I admit.
Add Yes/No filter to column
Short and simple code to generate a dropdownlist with "Yes" and "No" options:
array( 'name' => 'isPaid', 'type' => 'boolean', 'filter' => Yii::app()->format->booleanFormat, ),
Signup or Login in order to comment.