2015-02-12 53 views
0

创建链接我在Yii框架初学者,我想用一个字段idAccounts.name链接CGridView与CGridView

$post= Sheduale::model()->search(); 
    $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'users-grid', 
    'dataProvider'=>$post, 
    'columns'=>array(
     'idAccounts.TypeId', 
     'idAccounts.name', 
       'start', 
       'end', 
     array(
      'class'=>'CButtonColumn', 
     ), 
    ), 
    )); 

回答

0

你可以这样做:

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid', 
'dataProvider'=>$post, 
'columns'=>array(
    'idAccounts.TypeId', 
    array(
      "header"=>"The column header", 
      "value"=>function($data, $row){ 
        echo "<a></a>" //you can set everything you want here 
       //$data refers to each data row in the grid. you can use $data->attribute_name for access attributes of your model 
       } 
      ) 
      'start', 
      'end', 
    array(
     'class'=>'CButtonColumn', 
    ), 
), 
)); 

我认为这将帮你。

+0

非常感谢,我希望 – Muzaffarich 2015-02-12 05:42:02

0

一个选项是使用CLinkColumn。您可以将其label属性设置为您要在链接中显示的任何文本,并使用其urlExpression属性为每个链接生成URL。 urlExpression必须是一个包含PHP的字符串(将对其进行评估以确定应该用于链接URL的内容)。例如:

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'users-grid', 
    'dataProvider' => $post, 
    'columns' => array(
     'idAccounts.TypeId', 
     array(
      'class' => 'CLinkColumn', 
      'label' => 'View details' 
      'labelExpression' => '$data->idAccounts->name', 
      'urlExpression' => '\Yii::app()->createUrl(' 
       . '"controller/action", ' 
       . 'array("id" => $data->idAccounts->id)' 
      . ')', 
      'header' => 'Some Column Header', 
     ), 
     'start', 
     'end', 
     array(
      'class'=>'CButtonColumn', 
     ), 
    ), 
)); 

您需要用你想要的链接,进入到实际的Yii的路由替代controller/action(在我的样本urlExpression字符串),同样在这个例子中使用的样本路径参数。