Yii Framework v1.1.10 绫诲弬鑰

CButtonColumn

zii.widgets.grid
缁ф壙 class CButtonColumn » CGridColumn » CComponent
婧愯嚜 1.1
鐗堟湰 $Id: CButtonColumn.php 3424 2011-10-24 20:13:19Z mdomba $
婧愮爜 framework/zii/widgets/grid/CButtonColumn.php
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 buttons and template properties, the column can display other buttons and customize the display order of the buttons.

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
afterDelete string a javascript function that will be invoked after the delete ajax call. CButtonColumn
buttons array the configuration for additional buttons. CButtonColumn
cssClassExpression string 璁$畻PHP琛ㄨ揪寮忎腑姣忎竴涓暟鎹厓浠讹紝 鍏剁粨鏋滀綔涓鸿繖涓暟鎹厓浠剁殑CSS绫诲悕銆傚湪杩欎釜琛ㄨ揪寮忎腑锛 鍙橀噺$row鏄鍙凤紙闆跺熀纭锛夛紱 $data鏄琛岀殑鏁版嵁妯″瀷锛 $this鏄垪鐨勫璞° CGridColumn
deleteButtonImageUrl string the image URL for the delete button. CButtonColumn
deleteButtonLabel string the label for the delete button. CButtonColumn
deleteButtonOptions array the HTML options for the view button tag. CButtonColumn
deleteButtonUrl string a PHP expression that is evaluated for every delete button and whose result is used as the URL for the delete button. CButtonColumn
deleteConfirmation string the confirmation message to be displayed when delete button is clicked. CButtonColumn
footerHtmlOptions array the HTML options for the footer cell tag. CButtonColumn
grid CGridView 鎷ユ湁姝ゅ垪鐨勮〃鏍艰鍥惧璞° CGridColumn
hasFooter boolean 纭畾姝ゅ垪鏄惁鏈夊簳閮ㄥ厓浠躲 杩欏喅瀹氬熀浜庢槸鍚footer宸茶缃 CGridColumn
headerHtmlOptions array the HTML options for the header cell tag. CButtonColumn
htmlOptions array the HTML options for the data cell tags. CButtonColumn
id string the ID of this column. CGridColumn
template string the template that is used to render the content in each data cell. CButtonColumn
updateButtonImageUrl string the image URL for the update button. CButtonColumn
updateButtonLabel string the label for the update button. CButtonColumn
updateButtonOptions array the HTML options for the update button tag. CButtonColumn
updateButtonUrl string a PHP expression that is evaluated for every update button and whose result is used as the URL for the update button. CButtonColumn
viewButtonImageUrl string the image URL for the view button. CButtonColumn
viewButtonLabel string the label for the view button. CButtonColumn
viewButtonOptions array the HTML options for the view button tag. CButtonColumn
viewButtonUrl string a PHP expression that is evaluated for every view button and whose result is used as the URL for the view button. CButtonColumn
visible boolean 姝ゅ垪鏄惁鍙銆傞粯璁や负true銆 CGridColumn

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__construct() 鏋勯犳柟娉曘 CGridColumn
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getHasFooter() 杩斿洖纭畾姝ゅ垪鏄惁鏈夊簳閮ㄥ厓浠躲 杩欏喅瀹氬熀浜庢槸鍚footer宸茶缃 CGridColumn
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
init() Initializes the column. CButtonColumn
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
renderDataCell() 娓叉煋涓涓暟鎹厓浠躲 CGridColumn
renderFilterCell() 娓叉煋杩囨护鍣ㄥ厓浠躲 CGridColumn
renderFooterCell() 娓叉煋搴曢儴鍏冧欢銆 CGridColumn
renderHeaderCell() 娓叉煋澶撮儴鍏冧欢銆 CGridColumn

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
initDefaultButtons() Initializes the default buttons (view, update and delete). CButtonColumn
registerClientScript() Registers the client scripts for the button column. CButtonColumn
renderButton() Renders a link button. CButtonColumn
renderDataCellContent() Renders the data cell content. CButtonColumn
renderFilterCellContent() Renders the filter cell content. CGridColumn
renderFooterCellContent() Renders the footer cell content. CGridColumn
renderHeaderCellContent() Renders the header cell content. CGridColumn

灞炴ц缁

afterDelete 灞炴
public string $afterDelete;

a javascript function that will be invoked after the delete ajax call. This property is used only if $this->buttons['delete']['click'] is not set.

The function signature is function(link, success, data)

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:
 array(
    class'=>'CButtonColumn',
    'afterDelete'=>'function(link,success,data){ if(success) alert("Delete completed successfuly"); }',
 ),

buttons 灞炴
public array $buttons;

the configuration for additional buttons. Each array element specifies a single button which has the following format:

'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
)
In the PHP expression for the 'url' option and/or 'visible' option, the variable $row refers to the current row number (zero-based), and $data refers to the data model for the row.

Note that in order to display these additional buttons, the template property needs to be configured so that the corresponding button IDs appear as tokens in the template.

deleteButtonImageUrl 灞炴
public string $deleteButtonImageUrl;

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.

deleteButtonLabel 灞炴
public string $deleteButtonLabel;

the label for the delete button. Defaults to "Delete". Note that the label will not be HTML-encoded when rendering.

deleteButtonOptions 灞炴
public array $deleteButtonOptions;

the HTML options for the view button tag.

deleteButtonUrl 灞炴
public string $deleteButtonUrl;

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, the variable $row the row number (zero-based); $data the data model for the row; and $this the column object.

deleteConfirmation 灞炴
public string $deleteConfirmation;

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 $this->buttons['delete']['click'] is not set.

footerHtmlOptions 灞炴
public array $footerHtmlOptions;

the HTML options for the footer cell tag.

headerHtmlOptions 灞炴
public array $headerHtmlOptions;

the HTML options for the header cell tag.

htmlOptions 灞炴
public array $htmlOptions;

the HTML options for the data cell tags.

template 灞炴
public string $template;

the template that is used to render the content in each data cell. These default tokens are recognized: {view}, {update} and {delete}. If the buttons property defines additional buttons, their IDs are also recognized here. For example, if a button named 'preview' is declared in buttons, we can use the token '{preview}' here to specify where to display the button.

updateButtonImageUrl 灞炴
public string $updateButtonImageUrl;

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.

updateButtonLabel 灞炴
public string $updateButtonLabel;

the label for the update button. Defaults to "Update". Note that the label will not be HTML-encoded when rendering.

updateButtonOptions 灞炴
public array $updateButtonOptions;

the HTML options for the update button tag.

updateButtonUrl 灞炴
public string $updateButtonUrl;

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, the variable $row the row number (zero-based); $data the data model for the row; and $this the column object.

viewButtonImageUrl 灞炴
public string $viewButtonImageUrl;

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.

viewButtonLabel 灞炴
public string $viewButtonLabel;

the label for the view button. Defaults to "View". Note that the label will not be HTML-encoded when rendering.

viewButtonOptions 灞炴
public array $viewButtonOptions;

the HTML options for the view button tag.

viewButtonUrl 灞炴
public string $viewButtonUrl;

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, the variable $row the row number (zero-based); $data the data model for the row; and $this the column object.

鏂规硶璇︾粏

init() 鏂规硶
public void init()
婧愮爜锛 framework/zii/widgets/grid/CButtonColumn.php#166 (鏄剧ず)
public function init()
{
    
$this->initDefaultButtons();

    foreach(
$this->buttons as $id=>$button)
    {
        if(
strpos($this->template,'{'.$id.'}')===false)
            unset(
$this->buttons[$id]);
        else if(isset(
$button['click']))
        {
            if(!isset(
$button['options']['class']))
                
$this->buttons[$id]['options']['class']=$id;
            if(
strpos($button['click'],'js:')!==0)
                
$this->buttons[$id]['click']='js:'.$button['click'];
        }
    }

    
$this->registerClientScript();
}

Initializes the column. This method registers necessary client script for the button column.

initDefaultButtons() 鏂规硶
protected void initDefaultButtons()
婧愮爜锛 framework/zii/widgets/grid/CButtonColumn.php#189 (鏄剧ず)
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;
var afterDelete=
$this->afterDelete;
$.fn.yiiGridView.update('
{$this->grid->id}', {
    type:'POST',
    url:$(this).attr('href'),
$csrf
    success:function(data) {
        $.fn.yiiGridView.update('
{$this->grid->id}');
        afterDelete(th,true,data);
    },
    error:function(XHR) {
        return afterDelete(th,false,XHR);
    }
});
return false;
}
EOD;
    }
}

Initializes the default buttons (view, update and delete).

registerClientScript() 鏂规硶
protected void registerClientScript()
婧愮爜锛 framework/zii/widgets/grid/CButtonColumn.php#264 (鏄剧ず)
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('#{$this->grid->id} a.{$class}').live('click',$function);";
        }
    }

    if(
$js!==array())
        
Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$this->idimplode("\n",$js));
}

Registers the client scripts for the button column.

renderButton() 鏂规硶
protected void renderButton(string $id, array $button, integer $row, mixed $data)
$id string the ID of the button
$button array the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements. See buttons for more details.
$row integer the row number (zero-based)
$data mixed the data object associated with the row
婧愮爜锛 framework/zii/widgets/grid/CButtonColumn.php#309 (鏄剧ず)
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);
}

Renders a link button.

renderDataCellContent() 鏂规硶
protected void renderDataCellContent(integer $row, mixed $data)
$row integer the row number (zero-based)
$data mixed the data associated with the row
婧愮爜锛 framework/zii/widgets/grid/CButtonColumn.php#287 (鏄剧ず)
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 the data cell content. This method renders the view, update and delete buttons in the data cell.

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.