1

我有一个 CDbActiveRecord 设置,并且我有一个 CGridView 类设置的实例作为小部件。

基本上我的最终结果是我需要一个表,但每一行都包含与 Active Record 关联的行的主键。

如:

<tr id="123"> <td> Column value 1 </td> <td> Col 2 </td> <td> Col 3 </td> </tr> 

那是我正在寻找的特定行。

这是我到目前为止生成表格的代码。(设置 json 变量是因为它位于控制器内部,并且小部件以 json 形式返回。)

// get the content id for the version list
$contentID_v = Yii::app()->request->getParam("id"); 

// setup the criteria to fetch related items 
$versionCdbCriteria = new CDbCriteria;
$versionCdbCriteria->compare("contentID",$contentID_v); 

// setting up the active data provider for the version 
$vActiveDP = new CActiveDataProvider("FactsheetcontentVersion", array(
    "criteria" => $versionCdbCriteria,
    'pagination' => array('PageSize' => $this->paginationSize),
    'keyAttribute'=>'vID',
));

$json_data .= $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => $vActiveDP,
        'columns' => array(
        'title',
        'created',
        'createdBy'
        ),
    'showTableOnEmpty' => 'false', 
),true);

这就是它为我的活动记录所产生的。

 <div class="grid-view" id="yw0">
<div class="summary">Displaying 1-1 of 1 result(s).</div>
<table class="items"><thead>
    <tr><th id="yw0_c0">Factsheettitle</th>
        <th id="yw0_c1"><a href="jq/work/admin/index.php?r=factsheetManager/Editor
        &amp;id=25601&amp;getV=true&amp;_=1341694154760&amp;FactsheetcontentVersion_sort=created">Created</a>
        </th>
        <th id="yw0_c2"><a href="jq/work/admin/index.php?r=factsheetManager/
        Editor&amp;id=25601&amp;getV=true&amp;_=1341694154760&amp;FactsheetcontentVersion_sort=createdBy">Created By</a>
        </th>
    </tr></thead>
<tbody><tr class="odd"><td>Distribution</td><td>0000-00-00 00:00:00</td><td>NULL</td></tr></tbody>
</table>
<div title="jq/work/admin/index.php?r=factsheetManager/Editor&amp;id=12&amp;id=25601&amp;getV=true&amp;_=1341694154760" style="display:none" class="keys"><span>8</span></div>
</div>
4

1 回答 1

1

只是自己学习 Yii,但这似乎很明显?像这样扩展小部件。

    Yii::import('zii.widgets.grid.CGridView');

    class MyGridView extends CGridView {
    //This does depend on your data structure
    $data=$this->dataProvider->data[$row];
    $id = $data['id']; //(This may be an object I have not checked is so: $data->id)

        public function renderTableRow($row)
        {
            if($this->rowCssClassExpression!==null)
            {
echo '<tr id="'.$id.'" class="'.$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$data)).'">';
            }
            else if(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)
                echo '<tr id="'.$id.'"  class="'.$this->rowCssClass[$row%$n].'">';
            else
                echo '<tr id="'.$id.'" >';
            foreach($this->columns as $column)
                $column->renderDataCell($row);
            echo "</tr>\n";
        }

    }

现在使用这个:

$json_data .= $this->widget('zii.widgets.grid.MyGridView', array(
        'dataProvider' => $vActiveDP,
        'columns' => array(
        'title',
        'created',
        'createdBy'
        ),
    'showTableOnEmpty' => 'false', 
),true)

我还不熟悉 YII,但是您可能必须将其注册为扩展并将其添加到您的扩展目录中......或者它可能更多的是一个组件,我不清楚您将代码放置在哪里覆盖现有的小部件,一旦我知道我将编辑这个答案。

于 2012-07-08T03:42:04.163 回答