当你需要在一个GridView渲染某一个复杂元素的时候(特别是在这种情况下,这是一个小部件),这就需要你在控制器中调用一个动作。例如你给一个GridView的定义这样的一列:
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'id' => 'folder-grid',
'dataProvider' => $model->search($folder_id),
'template' => "{items}{pager}",
'rowHtmlOptionsExpression' => 'array("file_id"=>$data->file_id, "file_name"=>rtrim($data->file_name,".".$data->file_ext), "file_ext"=>$data->file_ext)',
'columns' => array(
array(
'selectableRows' => 2,
'class' => 'CCheckBoxColumn',
'headerHtmlOptions' => array('width' => '10px'),
'htmlOptions' => array('class' => 'notopen'),
'checkBoxHtmlOptions' => array('name' => 'ids[]'),
),
array(
'name' => 'folder_id',
'headerHtmlOptions' => array('style' => 'display:none'),
'htmlOptions' => array('style' => 'display:none;'),
),
array(
'name' => 'file_name',
'header' => '文件名',
'type' => 'raw',
'headerHtmlOptions' => array('style' => 'width:100%;'),
'htmlOptions' => array('class' => 'ellipsis'),
'value' => 'CHtml::openTag("img", array("src" => TFileUtil::getFileTypeIcon($data->file_name,true),"style"=>"margin-right:4px")) . rtrim($data->file_name,".".$data->file_ext)',
),
array(
'name' => 'more_opt',
'header' => '',
'value' => array($this, 'renderButtons'),
'sortable' => false,
'htmlOptions' => array('class' => 'more notopen'),
'headerHtmlOptions' => array('style' => 'width:40px;'),
),
array(
'name' => 'file_size',
'header' => '大小',
'value' => 'TFileUtil::getFileSize($data->file_size)',
'headerHtmlOptions' => array('style' => 'width:80px;'),
),
array(
'name' => 'update_time',
'header' => '修改时间',
'value' => 'date("Y-m-d H:i:s", $data->update_time)',
'headerHtmlOptions' => array('style' => 'width:160px;'),
),
array(
'class' => 'bootstrap.widgets.TbButtonColumn',
'template' => '{update}{download}{remove}',
'buttons' => array(
'update' => array(
'label' => '',
'options' => array(
'class' => 'icon-update td-link-icon',
'data-toggle' => 'modal',
'data-target' => '#rename_file',
'title' => '修改',
),
'click' => 'js:function(){
var $row = $(this).parents("tr");
var $column = $(this).parent("td").siblings();
var id = $column.eq(0).children("input").val();
$("#rename_file #FilePrivate_id").val(id);
$("#rename_file #FilePrivate_file_name").val($row.attr("file_name"))
.next().text("." + $row.attr("file_ext"));
}',
),
'download' => array(
'label' => '',
'url' => 'Yii::app()->controller->createUrl("/file/private/download",array(
"id"=>$data->id,"fileId"=>$data->file_id,"fileName"=>FilePrivate::getFileName($data->id,$data->file_id), "createTime"=>$data->create_time))',
'options' => array(
'class' => 'download td-link-icon icon-download-2',
'title' => '下载',
),
),
'remove' => array(
'label' => '',
'options' => array(
'class' => 'icon-remove td-link-icon',
'data-toggle' => 'modal',
'data-target' => '#remove',
'title' => '删除',
),
'click' => 'js:function(){
var $column = $(this).parent("td").siblings();
var id = $column.eq(0).children("input").val();
$("#remove #FilePrivate_id").val(id);
$("#remove h4").text("删除文件");
$("#remove .note span:eq(1)").text("确定要删除所选的文件吗?");
$("#remove #removefiles").hide();
$("#remove #removefile").show();
}',
),
),
'header' => '操作',
'htmlOptions' => array('style' => 'width:80px;text-align:center;', 'class' => 'notopen'),
'headerHtmlOptions' => array('style' => 'width:80px;text-align:center;'),
),
)
));
?>
然后在控制器里面这么渲染
/**
* 显示更多操作的按钮
*/
public function renderButtons() {
$this->widget('bootstrap.widgets.TbButtonGroup', array(
'size' => 'small',
'buttons' => array(
array('label' => '', 'items' => array(
array('label' => '重命名', 'url' => 'javascript:;', 'linkOptions' => array('id' => 'renameFile')),
array('label' => '移动到', 'url' => 'javascript:;', 'linkOptions' => array('id' => 'moveFile')),
array('label' => '复制到', 'url' => 'javascript:;', 'linkOptions' => array('id' => 'copyFile'))
)),
),
));
}
http://*.com/questions/16352998/yii-bootstrap-widget-tbbuttoncolumn-widget-tbbuttongroup