1
0

Added new (clean) yii boilerplate

This commit is contained in:
2014-05-13 12:40:42 +02:00
parent 1d6d975a16
commit 99d29b432b
1983 changed files with 653465 additions and 17 deletions

View File

@@ -0,0 +1,349 @@
<?php
/**
* CButtonColumn class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
Yii::import('zii.widgets.grid.CGridColumn');
/**
* CButtonColumn represents a grid view column that renders one or several buttons.
*
* By default, it will display three buttons, "view", "update" and "delete", which triggers the corresponding
* actions on the model of the row.
*
* By configuring {@link buttons} and {@link template} properties, the column can display other buttons
* and customize the display order of the buttons.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package zii.widgets.grid
* @since 1.1
*/
class CButtonColumn extends CGridColumn
{
/**
* @var array the HTML options for the data cell tags.
*/
public $htmlOptions=array('class'=>'button-column');
/**
* @var array the HTML options for the header cell tag.
*/
public $headerHtmlOptions=array('class'=>'button-column');
/**
* @var array the HTML options for the footer cell tag.
*/
public $footerHtmlOptions=array('class'=>'button-column');
/**
* @var string the template that is used to render the content in each data cell.
* These default tokens are recognized: {view}, {update} and {delete}. If the {@link buttons} property
* defines additional buttons, their IDs are also recognized here. For example, if a button named 'preview'
* is declared in {@link buttons}, we can use the token '{preview}' here to specify where to display the button.
*/
public $template='{view} {update} {delete}';
/**
* @var string the label for the view button. Defaults to "View".
* Note that the label will not be HTML-encoded when rendering.
*/
public $viewButtonLabel;
/**
* @var string the image URL for the view button. If not set, an integrated image will be used.
* You may set this property to be false to render a text link instead.
*/
public $viewButtonImageUrl;
/**
* @var string a PHP expression that is evaluated for every view button and whose result is used
* as the URL for the view button. In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the column object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*/
public $viewButtonUrl='Yii::app()->controller->createUrl("view",array("id"=>$data->primaryKey))';
/**
* @var array the HTML options for the view button tag.
*/
public $viewButtonOptions=array('class'=>'view');
/**
* @var string the label for the update button. Defaults to "Update".
* Note that the label will not be HTML-encoded when rendering.
*/
public $updateButtonLabel;
/**
* @var string the image URL for the update button. If not set, an integrated image will be used.
* You may set this property to be false to render a text link instead.
*/
public $updateButtonImageUrl;
/**
* @var string a PHP expression that is evaluated for every update button and whose result is used
* as the URL for the update button. In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the column object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*/
public $updateButtonUrl='Yii::app()->controller->createUrl("update",array("id"=>$data->primaryKey))';
/**
* @var array the HTML options for the update button tag.
*/
public $updateButtonOptions=array('class'=>'update');
/**
* @var string the label for the delete button. Defaults to "Delete".
* Note that the label will not be HTML-encoded when rendering.
*/
public $deleteButtonLabel;
/**
* @var string the image URL for the delete button. If not set, an integrated image will be used.
* You may set this property to be false to render a text link instead.
*/
public $deleteButtonImageUrl;
/**
* @var string a PHP expression that is evaluated for every delete button and whose result is used
* as the URL for the delete button. In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the column object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*/
public $deleteButtonUrl='Yii::app()->controller->createUrl("delete",array("id"=>$data->primaryKey))';
/**
* @var array the HTML options for the delete button tag.
*/
public $deleteButtonOptions=array('class'=>'delete');
/**
* @var string the confirmation message to be displayed when delete button is clicked.
* By setting this property to be false, no confirmation message will be displayed.
* This property is used only if <code>$this->buttons['delete']['click']</code> is not set.
*/
public $deleteConfirmation;
/**
* @var string a javascript function that will be invoked after the delete ajax call.
* This property is used only if <code>$this->buttons['delete']['click']</code> is not set.
*
* The function signature is <code>function(link, success, data)</code>
* <ul>
* <li><code>link</code> references the delete link.</li>
* <li><code>success</code> status of the ajax call, true if the ajax call was successful, false if the ajax call failed.
* <li><code>data</code> the data returned by the server in case of a successful call or XHR object in case of error.
* </ul>
* Note that if success is true it does not mean that the delete was successful, it only means that the ajax call was successful.
*
* Example:
* <pre>
* array(
* class'=>'CButtonColumn',
* 'afterDelete'=>'function(link,success,data){ if(success) alert("Delete completed successfuly"); }',
* ),
* </pre>
*/
public $afterDelete;
/**
* @var array the configuration for buttons. Each array element specifies a single button
* which has the following format:
* <pre>
* 'buttonID' => array(
* 'label'=>'...', // text label of the button
* 'url'=>'...', // a PHP expression for generating the URL of the button
* 'imageUrl'=>'...', // image URL of the button. If not set or false, a text link is used
* 'options'=>array(...), // HTML options for the button tag
* 'click'=>'...', // a JS function to be invoked when the button is clicked
* 'visible'=>'...', // a PHP expression for determining whether the button is visible
* )
* </pre>
*
* In the PHP expression for the 'url' option and/or 'visible' option, the variable <code>$row</code>
* refers to the current row number (zero-based), and <code>$data</code> refers to the data model for
* the row.
* The PHP expression will be evaluated using {@link evaluateExpression}.
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*
* If the 'buttonID' is 'view', 'update' or 'delete' the options will be applied to the default buttons.
*
* Note that in order to display non-default buttons, the {@link template} property needs to
* be configured so that the corresponding button IDs appear as tokens in the template.
*/
public $buttons=array();
/**
* Initializes the column.
* This method registers necessary client script for the button column.
*/
public function init()
{
$this->initDefaultButtons();
foreach($this->buttons as $id=>$button)
{
if(strpos($this->template,'{'.$id.'}')===false)
unset($this->buttons[$id]);
elseif(isset($button['click']))
{
if(!isset($button['options']['class']))
$this->buttons[$id]['options']['class']=$id;
if(!($button['click'] instanceof CJavaScriptExpression))
$this->buttons[$id]['click']=new CJavaScriptExpression($button['click']);
}
}
$this->registerClientScript();
}
/**
* Initializes the default buttons (view, update and delete).
*/
protected function initDefaultButtons()
{
if($this->viewButtonLabel===null)
$this->viewButtonLabel=Yii::t('zii','View');
if($this->updateButtonLabel===null)
$this->updateButtonLabel=Yii::t('zii','Update');
if($this->deleteButtonLabel===null)
$this->deleteButtonLabel=Yii::t('zii','Delete');
if($this->viewButtonImageUrl===null)
$this->viewButtonImageUrl=$this->grid->baseScriptUrl.'/view.png';
if($this->updateButtonImageUrl===null)
$this->updateButtonImageUrl=$this->grid->baseScriptUrl.'/update.png';
if($this->deleteButtonImageUrl===null)
$this->deleteButtonImageUrl=$this->grid->baseScriptUrl.'/delete.png';
if($this->deleteConfirmation===null)
$this->deleteConfirmation=Yii::t('zii','Are you sure you want to delete this item?');
foreach(array('view','update','delete') as $id)
{
$button=array(
'label'=>$this->{$id.'ButtonLabel'},
'url'=>$this->{$id.'ButtonUrl'},
'imageUrl'=>$this->{$id.'ButtonImageUrl'},
'options'=>$this->{$id.'ButtonOptions'},
);
if(isset($this->buttons[$id]))
$this->buttons[$id]=array_merge($button,$this->buttons[$id]);
else
$this->buttons[$id]=$button;
}
if(!isset($this->buttons['delete']['click']))
{
if(is_string($this->deleteConfirmation))
$confirmation="if(!confirm(".CJavaScript::encode($this->deleteConfirmation).")) return false;";
else
$confirmation='';
if(Yii::app()->request->enableCsrfValidation)
{
$csrfTokenName = Yii::app()->request->csrfTokenName;
$csrfToken = Yii::app()->request->csrfToken;
$csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
}
else
$csrf = '';
if($this->afterDelete===null)
$this->afterDelete='function(){}';
$this->buttons['delete']['click']=<<<EOD
function() {
$confirmation
var th = this,
afterDelete = $this->afterDelete;
jQuery('#{$this->grid->id}').yiiGridView('update', {
type: 'POST',
url: jQuery(this).attr('href'),$csrf
success: function(data) {
jQuery('#{$this->grid->id}').yiiGridView('update');
afterDelete(th, true, data);
},
error: function(XHR) {
return afterDelete(th, false, XHR);
}
});
return false;
}
EOD;
}
}
/**
* Registers the client scripts for the button column.
*/
protected function registerClientScript()
{
$js=array();
foreach($this->buttons as $id=>$button)
{
if(isset($button['click']))
{
$function=CJavaScript::encode($button['click']);
$class=preg_replace('/\s+/','.',$button['options']['class']);
$js[]="jQuery(document).on('click','#{$this->grid->id} a.{$class}',$function);";
}
}
if($js!==array())
Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id, implode("\n",$js));
}
/**
* Renders the data cell content.
* This method renders the view, update and delete buttons in the data cell.
* @param integer $row the row number (zero-based)
* @param mixed $data the data associated with the row
*/
protected function renderDataCellContent($row,$data)
{
$tr=array();
ob_start();
foreach($this->buttons as $id=>$button)
{
$this->renderButton($id,$button,$row,$data);
$tr['{'.$id.'}']=ob_get_contents();
ob_clean();
}
ob_end_clean();
echo strtr($this->template,$tr);
}
/**
* Renders a link button.
* @param string $id the ID of the button
* @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.
* See {@link buttons} for more details.
* @param integer $row the row number (zero-based)
* @param mixed $data the data object associated with the row
*/
protected function renderButton($id,$button,$row,$data)
{
if (isset($button['visible']) && !$this->evaluateExpression($button['visible'],array('row'=>$row,'data'=>$data)))
return;
$label=isset($button['label']) ? $button['label'] : $id;
$url=isset($button['url']) ? $this->evaluateExpression($button['url'],array('data'=>$data,'row'=>$row)) : '#';
$options=isset($button['options']) ? $button['options'] : array();
if(!isset($options['title']))
$options['title']=$label;
if(isset($button['imageUrl']) && is_string($button['imageUrl']))
echo CHtml::link(CHtml::image($button['imageUrl'],$label),$url,$options);
else
echo CHtml::link($label,$url,$options);
}
}

View File

@@ -0,0 +1,248 @@
<?php
/**
* CCheckBoxColumn class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
Yii::import('zii.widgets.grid.CGridColumn');
/**
* CCheckBoxColumn represents a grid view column of checkboxes.
*
* CCheckBoxColumn supports no checking (read-only), single check and multiple checking.
* The mode is determined according to {@link selectableRows}. When in multiple checking mode, the header cell will display
* an additional checkbox, clicking on which will check or uncheck all of the checkboxes in the data cells.
* The header cell can be customized by {@link headerTemplate}.
*
* Additionally selecting a checkbox can select a grid view row (depending on {@link CGridView::selectableRows} value) if
* {@link selectableRows} is null (default).
*
* By default, the checkboxes rendered in data cells will have the values that are the same as
* the key values of the data model. One may change this by setting either {@link name} or
* {@link value}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package zii.widgets.grid
* @since 1.1
*/
class CCheckBoxColumn extends CGridColumn
{
/**
* @var string the attribute name of the data model. The corresponding attribute value will be rendered
* in each data cell as the checkbox value. Note that if {@link value} is specified, this property will be ignored.
* @see value
*/
public $name;
/**
* @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
* in each data cell as the checkbox value. In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the column object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*/
public $value;
/**
* @var string a PHP expression that will be evaluated for every data cell and whose result will
* determine if checkbox for each data cell is checked. In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the column object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
* @since 1.1.4
*/
public $checked;
/**
* @var string a PHP expression that will be evaluated for every data cell and whose result will
* determine if checkbox for each data cell is disabled. In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the column object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*
* Note that expression result will overwrite value set with <code>checkBoxHtmlOptions['disabled']</code>.
* @since 1.1.13
*/
public $disabled;
/**
* @var array the HTML options for the data cell tags.
*/
public $htmlOptions=array('class'=>'checkbox-column');
/**
* @var array the HTML options for the header cell tag.
*/
public $headerHtmlOptions=array('class'=>'checkbox-column');
/**
* @var array the HTML options for the footer cell tag.
*/
public $footerHtmlOptions=array('class'=>'checkbox-column');
/**
* @var array the HTML options for the checkboxes.
*/
public $checkBoxHtmlOptions=array();
/**
* @var integer the number of rows that can be checked.
* Possible values:
* <ul>
* <li>0 - the state of the checkbox cannot be changed (read-only mode)</li>
* <li>1 - only one row can be checked. Checking a checkbox has nothing to do with selecting the row</li>
* <li>2 or more - multiple checkboxes can be checked. Checking a checkbox has nothing to do with selecting the row</li>
* <li>null - {@link CGridView::selectableRows} is used to control how many checkboxes can be checked.
* Checking a checkbox will also select the row.</li>
* </ul>
* You may also call the JavaScript function <code>$(gridID).yiiGridView('getChecked', columnID)</code>
* to retrieve the key values of the checked rows.
* @since 1.1.6
*/
public $selectableRows=null;
/**
* @var string the template to be used to control the layout of the header cell.
* The token "{item}" is recognized and it will be replaced with a "check all" checkbox.
* By default if in multiple checking mode, the header cell will display an additional checkbox,
* clicking on which will check or uncheck all of the checkboxes in the data cells.
* See {@link selectableRows} for more details.
* @since 1.1.11
*/
public $headerTemplate='{item}';
/**
* Initializes the column.
* This method registers necessary client script for the checkbox column.
*/
public function init()
{
if(isset($this->checkBoxHtmlOptions['name']))
$name=$this->checkBoxHtmlOptions['name'];
else
{
$name=$this->id;
if(substr($name,-2)!=='[]')
$name.='[]';
$this->checkBoxHtmlOptions['name']=$name;
}
$name=strtr($name,array('['=>"\\[",']'=>"\\]"));
if($this->selectableRows===null)
{
if(isset($this->checkBoxHtmlOptions['class']))
$this->checkBoxHtmlOptions['class'].=' select-on-check';
else
$this->checkBoxHtmlOptions['class']='select-on-check';
return;
}
$cball=$cbcode='';
if($this->selectableRows==0)
{
//.. read only
$cbcode="return false;";
}
elseif($this->selectableRows==1)
{
//.. only one can be checked, uncheck all other
$cbcode="jQuery(\"input:not(#\"+this.id+\")[name='$name']\").prop('checked',false);";
}
elseif(strpos($this->headerTemplate,'{item}')!==false)
{
//.. process check/uncheck all
$cball=<<<CBALL
jQuery(document).on('click','#{$this->id}_all',function() {
var checked=this.checked;
jQuery("input[name='$name']:enabled").each(function() {this.checked=checked;});
});
CBALL;
$cbcode="jQuery('#{$this->id}_all').prop('checked', jQuery(\"input[name='$name']\").length==jQuery(\"input[name='$name']:checked\").length);";
}
if($cbcode!=='')
{
$js=$cball;
$js.=<<<EOD
jQuery(document).on('click', "input[name='$name']", function() {
$cbcode
});
EOD;
Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->id,$js);
}
}
/**
* Renders the header cell content.
* This method will render a checkbox in the header when {@link selectableRows} is greater than 1
* or in case {@link selectableRows} is null when {@link CGridView::selectableRows} is greater than 1.
*/
protected function renderHeaderCellContent()
{
if(trim($this->headerTemplate)==='')
{
echo $this->grid->blankDisplay;
return;
}
$item = '';
if($this->selectableRows===null && $this->grid->selectableRows>1)
$item = CHtml::checkBox($this->id.'_all',false,array('class'=>'select-on-check-all'));
elseif($this->selectableRows>1)
$item = CHtml::checkBox($this->id.'_all',false);
else
{
ob_start();
parent::renderHeaderCellContent();
$item = ob_get_clean();
}
echo strtr($this->headerTemplate,array(
'{item}'=>$item,
));
}
/**
* Renders the data cell content.
* This method renders a checkbox in the data cell.
* @param integer $row the row number (zero-based)
* @param mixed $data the data associated with the row
*/
protected function renderDataCellContent($row,$data)
{
if($this->value!==null)
$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
elseif($this->name!==null)
$value=CHtml::value($data,$this->name);
else
$value=$this->grid->dataProvider->keys[$row];
$checked = false;
if($this->checked!==null)
$checked=$this->evaluateExpression($this->checked,array('data'=>$data,'row'=>$row));
$options=$this->checkBoxHtmlOptions;
if($this->disabled!==null)
$options['disabled']=$this->evaluateExpression($this->disabled,array('data'=>$data,'row'=>$row));
$name=$options['name'];
unset($options['name']);
$options['value']=$value;
$options['id']=$this->id.'_'.$row;
echo CHtml::checkBox($name,$checked,$options);
}
}

View File

@@ -0,0 +1,142 @@
<?php
/**
* CDataColumn class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
Yii::import('zii.widgets.grid.CGridColumn');
/**
* CDataColumn represents a grid view column that is associated with a data attribute or PHP expression.
*
* Either {@link name} or {@link value} should be specified. The former specifies
* a data attribute name, while the latter a PHP expression whose value should be rendered instead.
*
* The property {@link sortable} determines whether the grid view can be sorted according to this column.
* Note that the {@link name} should always be set if the column needs to be sortable. The {@link name}
* value will be used by {@link CSort} to render a clickable link in the header cell to trigger the sorting.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package zii.widgets.grid
* @since 1.1
*/
class CDataColumn extends CGridColumn
{
/**
* @var string the attribute name of the data model. Used for column sorting, filtering and to render the corresponding
* attribute value in each data cell. If {@link value} is specified it will be used to rendered the data cell instead of the attribute value.
* @see value
* @see sortable
*/
public $name;
/**
* @var string a PHP expression that will be evaluated for every data cell using {@link evaluateExpression} and whose result will be rendered
* as the content of the data cell.
* In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based).</li>
* <li><code>$data</code> the data model for the row.</li>
* <li><code>$this</code> the column object.</li>
* </ul>
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*/
public $value;
/**
* @var string the type of the attribute value. This determines how the attribute value is formatted for display.
* Valid values include those recognizable by {@link CGridView::formatter}, such as: raw, text, ntext, html, date, time,
* datetime, boolean, number, email, image, url. For more details, please refer to {@link CFormatter}.
* Defaults to 'text' which means the attribute value will be HTML-encoded.
*/
public $type='text';
/**
* @var boolean 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 {@link name} is not set, or if {@link name} is not allowed by {@link CSort},
* this property will be treated as false.
* @see name
*/
public $sortable=true;
/**
* @var mixed 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
* {@link 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.
* @since 1.1.1
*/
public $filter;
/**
* Initializes the column.
*/
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.'));
}
/**
* Renders the filter cell content.
* This method will render the {@link filter} as is if it is a string.
* If {@link filter} is an array, it is assumed to be a list of options, and a dropdown selector will be rendered.
* Otherwise if {@link filter} is not false, a text field is rendered.
* @since 1.1.1
*/
protected function renderFilterCellContent()
{
if(is_string($this->filter))
echo $this->filter;
elseif($this->filter!==false && $this->grid->filter!==null && $this->name!==null && strpos($this->name,'.')===false)
{
if(is_array($this->filter))
echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));
elseif($this->filter===null)
echo CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));
}
else
parent::renderFilterCellContent();
}
/**
* Renders the header cell content.
* This method will render a link that can trigger the sorting if the column is sortable.
*/
protected function renderHeaderCellContent()
{
if($this->grid->enableSorting && $this->sortable && $this->name!==null)
echo $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)
echo CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
else
echo CHtml::encode($this->name);
}
else
parent::renderHeaderCellContent();
}
/**
* Renders the data cell content.
* This method evaluates {@link value} or {@link name} and renders the result.
* @param integer $row the row number (zero-based)
* @param mixed $data the data associated with the row
*/
protected function renderDataCellContent($row,$data)
{
if($this->value!==null)
$value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
elseif($this->name!==null)
$value=CHtml::value($data,$this->name);
echo $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
}
}

View File

@@ -0,0 +1,206 @@
<?php
/**
* CGridColumn class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CGridColumn is the base class for all grid view column classes.
*
* A CGridColumn object represents the specification for rendering the cells in
* a particular grid view column.
*
* In a column, there is one header cell, multiple data cells, and an optional footer cell.
* Child classes may override {@link renderHeaderCellContent}, {@link renderDataCellContent}
* and {@link renderFooterCellContent} to customize how these cells are rendered.
*
* @property boolean $hasFooter Whether this column has a footer cell.
* This is determined based on whether {@link footer} is set.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package zii.widgets.grid
* @since 1.1
*/
abstract class CGridColumn extends CComponent
{
/**
* @var string the ID of this column. This value should be unique among all grid view columns.
* If this is not set, it will be assigned one automatically.
*/
public $id;
/**
* @var CGridView the grid view object that owns this column.
*/
public $grid;
/**
* @var string the header cell text. Note that it will not be HTML-encoded.
*/
public $header;
/**
* @var string the footer cell text. Note that it will not be HTML-encoded.
*/
public $footer;
/**
* @var boolean whether this column is visible. Defaults to true.
*/
public $visible=true;
/**
* @var 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. In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the column object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*/
public $cssClassExpression;
/**
* @var array the HTML options for the data cell tags.
*/
public $htmlOptions=array();
/**
* @var array the HTML options for the filter cell tag.
*/
public $filterHtmlOptions=array();
/**
* @var array the HTML options for the header cell tag.
*/
public $headerHtmlOptions=array();
/**
* @var array the HTML options for the footer cell tag.
*/
public $footerHtmlOptions=array();
/**
* Constructor.
* @param CGridView $grid the grid view that owns this column.
*/
public function __construct($grid)
{
$this->grid=$grid;
}
/**
* Initializes the column.
* This method is invoked by the grid view when it initializes itself before rendering.
* You may override this method to prepare the column for rendering.
*/
public function init()
{
}
/**
* @return boolean whether this column has a footer cell.
* This is determined based on whether {@link footer} is set.
*/
public function getHasFooter()
{
return $this->footer!==null;
}
/**
* Renders the filter cell.
* @since 1.1.1
*/
public function renderFilterCell()
{
echo CHtml::openTag('td',$this->filterHtmlOptions);
$this->renderFilterCellContent();
echo "</td>";
}
/**
* Renders the header cell.
*/
public function renderHeaderCell()
{
$this->headerHtmlOptions['id']=$this->id;
echo CHtml::openTag('th',$this->headerHtmlOptions);
$this->renderHeaderCellContent();
echo "</th>";
}
/**
* Renders a data cell.
* @param integer $row the row number (zero-based)
*/
public function renderDataCell($row)
{
$data=$this->grid->dataProvider->data[$row];
$options=$this->htmlOptions;
if($this->cssClassExpression!==null)
{
$class=$this->evaluateExpression($this->cssClassExpression,array('row'=>$row,'data'=>$data));
if(!empty($class))
{
if(isset($options['class']))
$options['class'].=' '.$class;
else
$options['class']=$class;
}
}
echo CHtml::openTag('td',$options);
$this->renderDataCellContent($row,$data);
echo '</td>';
}
/**
* Renders the footer cell.
*/
public function renderFooterCell()
{
echo CHtml::openTag('td',$this->footerHtmlOptions);
$this->renderFooterCellContent();
echo '</td>';
}
/**
* Renders the header cell content.
* The default implementation simply renders {@link header}.
* This method may be overridden to customize the rendering of the header cell.
*/
protected function renderHeaderCellContent()
{
echo trim($this->header)!=='' ? $this->header : $this->grid->blankDisplay;
}
/**
* Renders the footer cell content.
* The default implementation simply renders {@link footer}.
* This method may be overridden to customize the rendering of the footer cell.
*/
protected function renderFooterCellContent()
{
echo trim($this->footer)!=='' ? $this->footer : $this->grid->blankDisplay;
}
/**
* Renders the data cell content.
* This method SHOULD be overridden to customize the rendering of the data cell.
* @param integer $row the row number (zero-based)
* @param mixed $data the data associated with the row
*/
protected function renderDataCellContent($row,$data)
{
echo $this->grid->blankDisplay;
}
/**
* Renders the filter cell content.
* The default implementation simply renders a space.
* This method may be overridden to customize the rendering of the filter cell (if any).
* @since 1.1.1
*/
protected function renderFilterCellContent()
{
echo $this->grid->blankDisplay;
}
}

View File

@@ -0,0 +1,649 @@
<?php
/**
* CGridView class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
Yii::import('zii.widgets.CBaseListView');
Yii::import('zii.widgets.grid.CDataColumn');
Yii::import('zii.widgets.grid.CLinkColumn');
Yii::import('zii.widgets.grid.CButtonColumn');
Yii::import('zii.widgets.grid.CCheckBoxColumn');
/**
* CGridView displays a list of data items in terms of a table.
*
* Each row of the table represents the data of a single data item, and a column usually represents
* an attribute of the item (some columns may correspond to complex expression of attributes or static text).
*
* CGridView supports both sorting and pagination of the data items. The sorting
* and pagination can be done in AJAX mode or normal page request. A benefit of using CGridView is that
* when the user browser disables JavaScript, the sorting and pagination automatically degenerate
* to normal page requests and are still functioning as expected.
*
* CGridView should be used together with a {@link IDataProvider data provider}, preferrably a
* {@link CActiveDataProvider}.
*
* The minimal code needed to use CGridView is as follows:
*
* <pre>
* $dataProvider=new CActiveDataProvider('Post');
*
* $this->widget('zii.widgets.grid.CGridView', array(
* 'dataProvider'=>$dataProvider,
* ));
* </pre>
*
* The above code first creates a data provider for the <code>Post</code> ActiveRecord class.
* It then uses CGridView to display every attribute in every <code>Post</code> instance.
* The displayed table is equiped with sorting and pagination functionality.
*
* In order to selectively display attributes with different formats, we may configure the
* {@link CGridView::columns} property. For example, we may specify only the <code>title</code>
* and <code>create_time</code> attributes to be displayed, and the <code>create_time</code>
* should be properly formatted to show as a time. We may also display the attributes of the related
* objects using the dot-syntax as shown below:
*
* <pre>
* $this->widget('zii.widgets.grid.CGridView', array(
* 'dataProvider'=>$dataProvider,
* 'columns'=>array(
* 'title', // display the 'title' attribute
* 'category.name', // display the 'name' attribute of the 'category' relation
* 'content:html', // display the 'content' attribute as purified HTML
* array( // display 'create_time' using an expression
* 'name'=>'create_time',
* 'value'=>'date("M j, Y", $data->create_time)',
* ),
* array( // display 'author.username' using an expression
* 'name'=>'authorName',
* 'value'=>'$data->author->username',
* ),
* array( // display a column with "view", "update" and "delete" buttons
* 'class'=>'CButtonColumn',
* ),
* ),
* ));
* </pre>
*
* Please refer to {@link columns} for more details about how to configure this property.
*
* @property boolean $hasFooter Whether the table should render a footer.
* This is true if any of the {@link columns} has a true {@link CGridColumn::hasFooter} value.
* @property CFormatter $formatter The formatter instance. Defaults to the 'format' application component.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package zii.widgets.grid
* @since 1.1
*/
class CGridView extends CBaseListView
{
const FILTER_POS_HEADER='header';
const FILTER_POS_FOOTER='footer';
const FILTER_POS_BODY='body';
private $_formatter;
/**
* @var array grid column configuration. Each array element represents the configuration
* for one particular grid column which can be either a string or an array.
*
* When a column is specified as a string, it should be in the format of "name:type:header",
* where "type" and "header" are optional. A {@link CDataColumn} instance will be created in this case,
* whose {@link CDataColumn::name}, {@link CDataColumn::type} and {@link CDataColumn::header}
* properties will be initialized accordingly.
*
* When a column is specified as an array, it will be used to create a grid column instance, where
* the 'class' element specifies the column class name (defaults to {@link CDataColumn} if absent).
* Currently, these official column classes are provided: {@link CDataColumn},
* {@link CLinkColumn}, {@link CButtonColumn} and {@link CCheckBoxColumn}.
*/
public $columns=array();
/**
* @var array the CSS class names for the table body rows. If multiple CSS class names are given,
* they will be assigned to the rows sequentially and repeatedly. This property is ignored
* if {@link rowCssClassExpression} is set. Defaults to <code>array('odd', 'even')</code>.
* @see rowCssClassExpression
*/
public $rowCssClass=array('odd','even');
/**
* @var string a PHP expression that is evaluated for every table body row and whose result
* is used as the CSS class name for the row. In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the grid view object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
* @see rowCssClass
* @deprecated in 1.1.13 in favor of {@link rowHtmlOptionsExpression}
*/
public $rowCssClassExpression;
/**
* @var string a PHP expression that is evaluated for every table body row and whose result
* is used as additional HTML attributes for the row. The expression should return an
* array whose key value pairs correspond to html attribute and value.
* In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based)</li>
* <li><code>$data</code> the data model for the row</li>
* <li><code>$this</code> the grid view object</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
* @since 1.1.13
*/
public $rowHtmlOptionsExpression;
/**
* @var boolean whether to display the table even when there is no data. Defaults to true.
* The {@link emptyText} will be displayed to indicate there is no data.
*/
public $showTableOnEmpty=true;
/**
* @var mixed the ID of the container whose content may be updated with an AJAX response.
* Defaults to null, meaning the container for this grid view instance.
* If it is set false, it means sorting and pagination will be performed in normal page requests
* instead of AJAX requests. If the sorting and pagination should trigger the update of multiple
* containers' content in AJAX fashion, these container IDs may be listed here (separated with comma).
*/
public $ajaxUpdate;
/**
* @var string the jQuery selector of the HTML elements that may trigger AJAX updates when they are clicked.
* These tokens are recognized: {page} and {sort}. They will be replaced with the pagination and sorting links selectors.
* Defaults to '{page}, {sort}', that means that the pagination links and the sorting links will trigger AJAX updates.
* Tokens are available from 1.1.11
*
* Note: if this value is empty an exception will be thrown.
*
* Example (adding a custom selector to the default ones):
* <pre>
* ...
* 'updateSelector'=>'{page}, {sort}, #mybutton',
* ...
* </pre>
* @since 1.1.7
*/
public $updateSelector='{page}, {sort}';
/**
* @var string a javascript function that will be invoked if an AJAX update error occurs.
*
* The function signature is <code>function(xhr, textStatus, errorThrown, errorMessage)</code>
* <ul>
* <li><code>xhr</code> is the XMLHttpRequest object.</li>
* <li><code>textStatus</code> is a string describing the type of error that occurred.
* Possible values (besides null) are "timeout", "error", "notmodified" and "parsererror"</li>
* <li><code>errorThrown</code> is an optional exception object, if one occurred.</li>
* <li><code>errorMessage</code> is the CGridView default error message derived from xhr and errorThrown.
* Usefull if you just want to display this error differently. CGridView by default displays this error with an javascript.alert()</li>
* </ul>
* Note: This handler is not called for JSONP requests, because they do not use an XMLHttpRequest.
*
* Example (add in a call to CGridView):
* <pre>
* ...
* 'ajaxUpdateError'=>'function(xhr,ts,et,err){ $("#myerrordiv").text(err); }',
* ...
* </pre>
*/
public $ajaxUpdateError;
/**
* @var string the name of the GET variable that indicates the request is an AJAX request triggered
* by this widget. Defaults to 'ajax'. This is effective only when {@link ajaxUpdate} is not false.
*/
public $ajaxVar='ajax';
/**
* @var mixed the URL for the AJAX requests should be sent to. {@link CHtml::normalizeUrl()} will be
* called on this property. If not set, the current page URL will be used for AJAX requests.
* @since 1.1.8
*/
public $ajaxUrl;
/**
* @var string the type ('GET' or 'POST') of the AJAX requests. If not set, 'GET' will be used.
* You can set this to 'POST' if you are filtering by many fields at once and have a problem with GET query string length.
* Note that in POST mode direct links and {@link enableHistory} feature may not work correctly!
* @since 1.1.14
*/
public $ajaxType;
/**
* @var string a javascript function that will be invoked before an AJAX update occurs.
* The function signature is <code>function(id,options)</code> where 'id' refers to the ID of the grid view,
* 'options' the AJAX request options (see jQuery.ajax api manual).
*/
public $beforeAjaxUpdate;
/**
* @var string a javascript function that will be invoked after a successful AJAX response is received.
* The function signature is <code>function(id, data)</code> where 'id' refers to the ID of the grid view,
* 'data' the received ajax response data.
*/
public $afterAjaxUpdate;
/**
* @var string a javascript function that will be invoked after the row selection is changed.
* The function signature is <code>function(id)</code> where 'id' refers to the ID of the grid view.
* In this function, you may use <code>$(gridID).yiiGridView('getSelection')</code> to get the key values
* of the currently selected rows (gridID is the DOM selector of the grid).
* @see selectableRows
*/
public $selectionChanged;
/**
* @var integer the number of table body rows that can be selected. If 0, it means rows cannot be selected.
* If 1, only one row can be selected. If 2 or any other number, it means multiple rows can be selected.
* A selected row will have a CSS class named 'selected'. You may also call the JavaScript function
* <code>$(gridID).yiiGridView('getSelection')</code> to retrieve the key values of the currently selected
* rows (gridID is the DOM selector of the grid).
*/
public $selectableRows=1;
/**
* @var string the base script URL for all grid view resources (eg javascript, CSS file, images).
* Defaults to null, meaning using the integrated grid view resources (which are published as assets).
*/
public $baseScriptUrl;
/**
* @var string the URL of the CSS file used by this grid view. Defaults to null, meaning using the integrated
* CSS file. If this is set false, you are responsible to explicitly include the necessary CSS file in your page.
*/
public $cssFile;
/**
* @var string the text to be displayed in a data cell when a data value is null. This property will NOT be HTML-encoded
* when rendering. Defaults to an HTML blank.
*/
public $nullDisplay='&nbsp;';
/**
* @var string the text to be displayed in an empty grid cell. This property will NOT be HTML-encoded when rendering. Defaults to an HTML blank.
* This differs from {@link nullDisplay} in that {@link nullDisplay} is only used by {@link CDataColumn} to render
* null data values.
* @since 1.1.7
*/
public $blankDisplay='&nbsp;';
/**
* @var string the CSS class name that will be assigned to the widget container element
* when the widget is updating its content via AJAX. Defaults to 'grid-view-loading'.
* @since 1.1.1
*/
public $loadingCssClass='grid-view-loading';
/**
* @var string the jQuery selector of filter input fields.
* The token '{filter}' is recognized and it will be replaced with the grid filters selector.
* Defaults to '{filter}'.
*
* Note: if this value is empty an exception will be thrown.
*
* Example (adding a custom selector to the default one):
* <pre>
* ...
* 'filterSelector'=>'{filter}, #myfilter',
* ...
* </pre>
* @since 1.1.13
*/
public $filterSelector='{filter}';
/**
* @var string the CSS class name for the table row element containing all filter input fields. Defaults to 'filters'.
* @see filter
* @since 1.1.1
*/
public $filterCssClass='filters';
/**
* @var string whether the filters should be displayed in the grid view. Valid values include:
* <ul>
* <li>header: the filters will be displayed on top of each column's header cell.</li>
* <li>body: the filters will be displayed right below each column's header cell.</li>
* <li>footer: the filters will be displayed below each column's footer cell.</li>
* </ul>
* @see filter
* @since 1.1.1
*/
public $filterPosition='body';
/**
* @var CModel the model instance that keeps the user-entered filter data. When this property is set,
* the grid view will enable column-based filtering. Each data column by default will display a text field
* at the top that users can fill in to filter the data.
* Note that in order to show an input field for filtering, a column must have its {@link CDataColumn::name}
* property set or have {@link CDataColumn::filter} as the HTML code for the input field.
* When this property is not set (null) the filtering is disabled.
* @since 1.1.1
*/
public $filter;
/**
* @var boolean whether to hide the header cells of the grid. When this is true, header cells
* will not be rendered, which means the grid cannot be sorted anymore since the sort links are located
* in the header. Defaults to false.
* @since 1.1.1
*/
public $hideHeader=false;
/**
* @var boolean whether to leverage the {@link https://developer.mozilla.org/en/DOM/window.history DOM history object}. Set this property to true
* to persist state of grid across page revisits. Note, there are two limitations for this feature:
* <ul>
* <li>this feature is only compatible with browsers that support HTML5.</li>
* <li>expect unexpected functionality (e.g. multiple ajax calls) if there is more than one grid/list on a single page with enableHistory turned on.</li>
* </ul>
* @since 1.1.11
*/
public $enableHistory=false;
/**
* Initializes the grid view.
* This method will initialize required property values and instantiate {@link columns} objects.
*/
public function init()
{
parent::init();
if(empty($this->updateSelector))
throw new CException(Yii::t('zii','The property updateSelector should be defined.'));
if(empty($this->filterSelector))
throw new CException(Yii::t('zii','The property filterSelector should be defined.'));
if(!isset($this->htmlOptions['class']))
$this->htmlOptions['class']='grid-view';
if($this->baseScriptUrl===null)
$this->baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/gridview';
if($this->cssFile!==false)
{
if($this->cssFile===null)
$this->cssFile=$this->baseScriptUrl.'/styles.css';
Yii::app()->getClientScript()->registerCssFile($this->cssFile);
}
$this->initColumns();
}
/**
* Creates column objects and initializes them.
*/
protected function initColumns()
{
if($this->columns===array())
{
if($this->dataProvider instanceof CActiveDataProvider)
$this->columns=$this->dataProvider->model->attributeNames();
elseif($this->dataProvider instanceof IDataProvider)
{
// use the keys of the first row of data as the default columns
$data=$this->dataProvider->getData();
if(isset($data[0]) && is_array($data[0]))
$this->columns=array_keys($data[0]);
}
}
$id=$this->getId();
foreach($this->columns as $i=>$column)
{
if(is_string($column))
$column=$this->createDataColumn($column);
else
{
if(!isset($column['class']))
$column['class']='CDataColumn';
$column=Yii::createComponent($column, $this);
}
if(!$column->visible)
{
unset($this->columns[$i]);
continue;
}
if($column->id===null)
$column->id=$id.'_c'.$i;
$this->columns[$i]=$column;
}
foreach($this->columns as $column)
$column->init();
}
/**
* Creates a {@link CDataColumn} based on a shortcut column specification string.
* @param string $text the column specification string
* @return CDataColumn the column instance
*/
protected function createDataColumn($text)
{
if(!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/',$text,$matches))
throw new CException(Yii::t('zii','The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
$column=new CDataColumn($this);
$column->name=$matches[1];
if(isset($matches[3]) && $matches[3]!=='')
$column->type=$matches[3];
if(isset($matches[5]))
$column->header=$matches[5];
return $column;
}
/**
* Registers necessary client scripts.
*/
public function registerClientScript()
{
$id=$this->getId();
if($this->ajaxUpdate===false)
$ajaxUpdate=false;
else
$ajaxUpdate=array_unique(preg_split('/\s*,\s*/',$this->ajaxUpdate.','.$id,-1,PREG_SPLIT_NO_EMPTY));
$options=array(
'ajaxUpdate'=>$ajaxUpdate,
'ajaxVar'=>$this->ajaxVar,
'pagerClass'=>$this->pagerCssClass,
'loadingClass'=>$this->loadingCssClass,
'filterClass'=>$this->filterCssClass,
'tableClass'=>$this->itemsCssClass,
'selectableRows'=>$this->selectableRows,
'enableHistory'=>$this->enableHistory,
'updateSelector'=>$this->updateSelector,
'filterSelector'=>$this->filterSelector
);
if($this->ajaxUrl!==null)
$options['url']=CHtml::normalizeUrl($this->ajaxUrl);
if($this->ajaxType!==null)
$options['ajaxType']=strtoupper($this->ajaxType);
if($this->enablePagination)
$options['pageVar']=$this->dataProvider->getPagination()->pageVar;
foreach(array('beforeAjaxUpdate', 'afterAjaxUpdate', 'ajaxUpdateError', 'selectionChanged') as $event)
{
if($this->$event!==null)
{
if($this->$event instanceof CJavaScriptExpression)
$options[$event]=$this->$event;
else
$options[$event]=new CJavaScriptExpression($this->$event);
}
}
$options=CJavaScript::encode($options);
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerCoreScript('bbq');
if($this->enableHistory)
$cs->registerCoreScript('history');
$cs->registerScriptFile($this->baseScriptUrl.'/jquery.yiigridview.js',CClientScript::POS_END);
$cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiGridView($options);");
}
/**
* Renders the data items for the grid view.
*/
public function renderItems()
{
if($this->dataProvider->getItemCount()>0 || $this->showTableOnEmpty)
{
echo "<table class=\"{$this->itemsCssClass}\">\n";
$this->renderTableHeader();
ob_start();
$this->renderTableBody();
$body=ob_get_clean();
$this->renderTableFooter();
echo $body; // TFOOT must appear before TBODY according to the standard.
echo "</table>";
}
else
$this->renderEmptyText();
}
/**
* Renders the table header.
*/
public function renderTableHeader()
{
if(!$this->hideHeader)
{
echo "<thead>\n";
if($this->filterPosition===self::FILTER_POS_HEADER)
$this->renderFilter();
echo "<tr>\n";
foreach($this->columns as $column)
$column->renderHeaderCell();
echo "</tr>\n";
if($this->filterPosition===self::FILTER_POS_BODY)
$this->renderFilter();
echo "</thead>\n";
}
elseif($this->filter!==null && ($this->filterPosition===self::FILTER_POS_HEADER || $this->filterPosition===self::FILTER_POS_BODY))
{
echo "<thead>\n";
$this->renderFilter();
echo "</thead>\n";
}
}
/**
* Renders the filter.
* @since 1.1.1
*/
public function renderFilter()
{
if($this->filter!==null)
{
echo "<tr class=\"{$this->filterCssClass}\">\n";
foreach($this->columns as $column)
$column->renderFilterCell();
echo "</tr>\n";
}
}
/**
* Renders the table footer.
*/
public function renderTableFooter()
{
$hasFilter=$this->filter!==null && $this->filterPosition===self::FILTER_POS_FOOTER;
$hasFooter=$this->getHasFooter();
if($hasFilter || $hasFooter)
{
echo "<tfoot>\n";
if($hasFooter)
{
echo "<tr>\n";
foreach($this->columns as $column)
$column->renderFooterCell();
echo "</tr>\n";
}
if($hasFilter)
$this->renderFilter();
echo "</tfoot>\n";
}
}
/**
* Renders the table body.
*/
public function renderTableBody()
{
$data=$this->dataProvider->getData();
$n=count($data);
echo "<tbody>\n";
if($n>0)
{
for($row=0;$row<$n;++$row)
$this->renderTableRow($row);
}
else
{
echo '<tr><td colspan="'.count($this->columns).'" class="empty">';
$this->renderEmptyText();
echo "</td></tr>\n";
}
echo "</tbody>\n";
}
/**
* Renders a table body row.
* @param integer $row the row number (zero-based).
*/
public function renderTableRow($row)
{
$htmlOptions=array();
if($this->rowHtmlOptionsExpression!==null)
{
$data=$this->dataProvider->data[$row];
$options=$this->evaluateExpression($this->rowHtmlOptionsExpression,array('row'=>$row,'data'=>$data));
if(is_array($options))
$htmlOptions = $options;
}
if($this->rowCssClassExpression!==null)
{
$data=$this->dataProvider->data[$row];
$class=$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$data));
}
elseif(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)
$class=$this->rowCssClass[$row%$n];
if(!empty($class))
{
if(isset($htmlOptions['class']))
$htmlOptions['class'].=' '.$class;
else
$htmlOptions['class']=$class;
}
echo CHtml::openTag('tr', $htmlOptions)."\n";
foreach($this->columns as $column)
$column->renderDataCell($row);
echo "</tr>\n";
}
/**
* @return boolean whether the table should render a footer.
* This is true if any of the {@link columns} has a true {@link CGridColumn::hasFooter} value.
*/
public function getHasFooter()
{
foreach($this->columns as $column)
if($column->getHasFooter())
return true;
return false;
}
/**
* @return CFormatter the formatter instance. Defaults to the 'format' application component.
*/
public function getFormatter()
{
if($this->_formatter===null)
$this->_formatter=Yii::app()->format;
return $this->_formatter;
}
/**
* @param CFormatter $value the formatter instance
*/
public function setFormatter($value)
{
$this->_formatter=$value;
}
}

View File

@@ -0,0 +1,111 @@
<?php
/**
* CLinkColumn class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
Yii::import('zii.widgets.grid.CGridColumn');
/**
* CLinkColumn represents a grid view column that renders a hyperlink in each of its data cells.
*
* The {@link label} and {@link url} properties determine how each hyperlink will be rendered.
* The {@link labelExpression}, {@link urlExpression} properties may be used instead if they are available.
* In addition, if {@link imageUrl} is set, an image link will be rendered.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package zii.widgets.grid
* @since 1.1
*/
class CLinkColumn extends CGridColumn
{
/**
* @var string the label to the hyperlinks in the data cells. Note that the label will not
* be HTML-encoded when rendering. This property is ignored if {@link labelExpression} is set.
* @see labelExpression
*/
public $label='Link';
/**
* @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
* as the label of the hyperlink of the data cell.
* In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based).</li>
* <li><code>$data</code> the data model for the row.</li>
* <li><code>$this</code> the column object.</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*/
public $labelExpression;
/**
* @var string the URL to the image. If this is set, an image link will be rendered.
*/
public $imageUrl;
/**
* @var string the URL of the hyperlinks in the data cells.
* This property is ignored if {@link urlExpression} is set.
* @see urlExpression
*/
public $url='javascript:void(0)';
/**
* @var string a PHP expression that will be evaluated for every data cell and whose result will be rendered
* as the URL of the hyperlink of the data cells.
* In this expression, you can use the following variables:
* <ul>
* <li><code>$row</code> the row number (zero-based).</li>
* <li><code>$data</code> the data model for the row.</li>
* <li><code>$this</code> the column object.</li>
* </ul>
* The PHP expression will be evaluated using {@link evaluateExpression}.
*
* A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
* please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
*/
public $urlExpression;
/**
* @var array the HTML options for the data cell tags.
*/
public $htmlOptions=array('class'=>'link-column');
/**
* @var array the HTML options for the header cell tag.
*/
public $headerHtmlOptions=array('class'=>'link-column');
/**
* @var array the HTML options for the footer cell tag.
*/
public $footerHtmlOptions=array('class'=>'link-column');
/**
* @var array the HTML options for the hyperlinks
*/
public $linkHtmlOptions=array();
/**
* Renders the data cell content.
* This method renders a hyperlink in the data cell.
* @param integer $row the row number (zero-based)
* @param mixed $data the data associated with the row
*/
protected function renderDataCellContent($row,$data)
{
if($this->urlExpression!==null)
$url=$this->evaluateExpression($this->urlExpression,array('data'=>$data,'row'=>$row));
else
$url=$this->url;
if($this->labelExpression!==null)
$label=$this->evaluateExpression($this->labelExpression,array('data'=>$data,'row'=>$row));
else
$label=$this->label;
$options=$this->linkHtmlOptions;
if(is_string($this->imageUrl))
echo CHtml::link(CHtml::image($this->imageUrl,$label),$url,$options);
else
echo CHtml::link($label,$url,$options);
}
}