【问题标题】:How can I display a single record in 2 rows by using Yii CGridView?如何使用 Yii CGridView 在 2 行中显示单个记录?
【发布时间】:2012-03-22 06:45:14
【问题描述】:

我在 Yii 中使用 CGridView,如何在 2 行中显示单个记录?

基本上我想在表格的第一行显示记录详细信息,在另一行我想显示它的摘要,我用 div 和 css 尝试过,但无法得到正确的结果,有没有人可以帮助我这种情况?

我是这样使用的:

<?php 

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bidding-grid',
'itemsCssClass' => 'data-default',
'dataProvider'=>$model,
'summaryText' => '',
'columns'=>array(
    'people_detail_for_bid.Person' => array(
                        'type'=>'raw',
                        'name'=>'people_detail_for_bid.Person',
                        'value'=>'Yii::app()->Controller->createUserNameLink($data->people_detail_for_bid->PeopleFirstName." ".$data->people_detail_for_bid->PeopleLastName, $data->people_detail_for_bid->PeopleId).
                        "<br><span class=decriptionText>".$data->people_detail_for_bid->PeopleDesignation."</span>".
                        "<br><span class=decriptionText>".$data->people_detail_for_bid->PeopleEmail."</span>"',
                        'htmlOptions'=>array('width'=>200),
    ),
    'timeAgo' => array(
                        'type'=>'raw',
                        'name'=>'timeAgo',
                        'value'=>'"<span class=decriptionText>".Yii::app()->Controller->_ago($data->PBPostedOn)."</sapn>"',
                        'htmlOptions'=>array('width'=>150),
    ),

),

)); 

?>

【问题讨论】:

    标签: yii frameworks


    【解决方案1】:

    我认为最好和最干净的方法是创建一个新的扩展并将其扩展到 CGridView,像这样覆盖 renderTableRow 函数:

    /**
     * Renders a table body row.
     * @param integer $row the row number (zero-based).
     */
    public function renderTableRow($row)
    {
        if($this->rowCssClassExpression!==null)
        {
            $data=$this->dataProvider->data[$row];
            echo '<tr class="'.$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$data)).'">';
        }
        else if(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)
            echo '<tr class="'.$this->rowCssClass[$row%$n].'">';
        else
            echo '<tr>';
    
        $colCtr = 0;
        foreach($this->columns as $column){
            $column->renderDataCell($row);
            $colCtr++;
        }
    
        echo "</tr>\n";
        echo "<tr><td colspan=\"$colCtr\">This is the summary row. </td></tr>";
    }
    

    【讨论】:

      【解决方案2】:

      您可以自定义在列中呈现的内容,因此如果您想在同一行中显示表格的两个不同字段,则必须在模型中创建一个函数:

      public function customColumn()
      {
          return $this->PeopleDesignation.'<br/>'.$this->PeopleEmail;
      }
      

      然后将方法分配给你的列的值:

      array(
          'type'=>'html',
          'value'=>'$data->customColumn()'
      ),
      

      干杯,巴勃罗。

      【讨论】:

      • 问题是@Shadman 需要两个自定义行,而不是 2 个自定义列。而且我想他想从多条记录中提取数据,因此 CGridView 需要为每对行交替分配 customColumn 分配,这可能是不可能的。
      • 是的,我需要在一条记录中包含 2 行,最后一行应包含该记录的摘要,并且必须涵盖所有列 .. 像
        1A 1B
        1C
        2A 2B
        2C
      【解决方案3】:

      经过太多的搜索..我尝试了不同的方法,现在终于找到了解决方案..该解决方案基本上是一种不实际的方式做任何事情的第二种方法..但它对我有用..

      .....
      .....
      
          'timeAgo' => array(
                              'type'=>'raw',
                              'name'=>'timeAgo',
                              'value'=>'"<span class=decriptionText>".Yii::app()->Controller->_ago($data->PBPostedOn)."</sapn></td></tr><tr><td colspan=\"6\"><span class=decriptionText>".$data->PBSummary."</span>"',
                              'htmlOptions'=>array('width'=>150),
          ),
      
      .....
      .....
      

      在最后一列添加一些表格标签以关闭行并添加另一个。

      希望它适用于所有人..

      谢谢..

      【讨论】:

        【解决方案4】:

        听起来像是 2 个交错的 CGridViews。所以你可以尝试一下,但我无法想象它会成功:

        将一个CGridView 覆盖在另一个之上,每一行都有足够的透明空间让另一个显示它的行,并且...

        a. one `CGridView` table offset vertically by half a row height, *OR*
        b. one table's row's text vertically top-aligned & the other bottom-aligned
        

        但不知何故,我怀疑这是 CGridView 的能力;)保持两者同步,以便它们一起分页和滚动几乎是不可能的。听起来您需要增强 CGridView 或从 CGridView 派生新的小部件。我想知道 JQuery 是否有办法做到这一点?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多