2012-07-07 2 views
1

CDbActiveRecord 설정이 있고 위젯으로 CGridView 클래스 설치 인스턴스가 있습니다.Yii, CGridView의 각 요소에 하나씩 unquie ids 생성

기본적으로 내 게임은 테이블이 필요하지만 각 행은 활성 레코드와 연결된 행의 기본 키를 포함해야합니다.

예를 들면 :

내가 찾고 행의 특정의
<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> 
+0

지금은 작동하는 해킹 솔루션을 발견 : http://stackoverflow.com/a/11096827 –

+0

CGridView를 확장해야합니다. 고유 한 행 ID가 필요한 이유는 무엇입니까? 다른 해결 방법이있을 수 있습니다 .. –

답변

1

나 자신을 배우는 것 뿐이지 만이 점은 분명해 보입니까? 위젯을 이렇게 확장하십시오.

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에 익숙는 아직 아니지만, 당신은 확장으로이를 등록해야하고 확장 디렉토리에 추가 ... 혹은 수의 더 많은 컴포넌트가 있다면이 답변을 편집 할 것이라는 것을 알게되면 기존 위젯을 대체하는 코드를 어디에 두어야하는지 명확하지 않습니다.

관련 문제