[英]Yii add filter to a virtual attribute in CGridView and make it sortable
[英]Yii - CGridView - add own attribute
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'title', // display the 'title' attribute
'category.name', // display the 'name' attribute of the 'category' relation
'content:html', // display the 'content' attribute as purified HTML
array( // display 'create_time' using an expression
'name'=>'create_time',
'value'=>'date("M j, Y", $data->create_time)',
),
array( // display 'author.username' using an expression
'name'=>'authorName',
'value'=>'$data->author->username',
//HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'htmlOptions'=>array('class'=>'$data->author->username', 'secondAttribute' => $data->author->id),
//HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
),
array( // display a column with "view", "update" and "delete" buttons
'class'=>'CButtonColumn',
),
),
));
在選項value
我可以從PHP添加變量,但是對於選項htmlOptions
這是不可能的。 為什么?
如何使用PHP變量創建屬性?
在columns
集合中添加數組而不指定class
屬性時,正在創建的列的類型是CDataColumn
。 屬性CDataColumn::value
明確記錄為
將為每個數據單元計算的PHP表達式,其結果將呈現為數據單元格的內容。
因此, value
具有為每行進行eval
'的特殊屬性,這就是為什么你可以“動態”設置它。 然而,這是一個例外,幾乎沒有其他任何東西支持相同的功能。
但是,你很幸運,因為屬性cssClassExpression
是另一個特殊的例外,它恰好涵蓋了這個用例。 所以你可以這樣做:
array(
'name'=>'authorName',
'value'=>'$data->author->username',
'cssClassExpression' => '$data->author->username',
),
編輯:我從你的例子中復制/粘貼時犯了一個錯誤,並沒有注意到你試圖對htmlOptions
其他屬性做同樣的事情(我現在已經刪除了代碼的相關部分)。
如果您需要為動態值添加更多選項,您別無選擇,只能子類化CDataColumn
並覆蓋renderDataCell
方法( 庫存實現在此處 )。
不知道這是否仍然適用(假設有一個已接受的答案),但是以“rowHtmlOptionsExpression”的形式有一個稍好的解決方案。 這指定了將為每一行計算的表達式。 如果eval()調用的結果是一個數組,它將被用作<tr>標記的htmlOptions。 所以你現在基本上可以使用這樣的東西:
$this->widget('zii.widgets.grid.CGridView', array
(
...
'rowHtmlOptionsExpression' => 'array("id" => $data->id)',
...
你的所有標簽都有一個帶有記錄'PK的id屬性。 只需稍微修改jQuery以從該標記獲取id而不是額外的列,您應該進行設置。
擴展類CDataColumn
在protected / components /下,使用以下內容創建DataColumn.php文件:
/**
* DataColumn class file.
* Extends {@link CDataColumn}
*/
class DataColumn extends CDataColumn
{
/**
* @var boolean whether the htmlOptions values should be evaluated.
*/
public $evaluateHtmlOptions = false;
/**
* Renders a data cell.
* @param integer $row the row number (zero-based)
* Overrides the method 'renderDataCell()' of the abstract class CGridColumn
*/
public function renderDataCell($row)
{
$data=$this->grid->dataProvider->data[$row];
if($this->evaluateHtmlOptions) {
foreach($this->htmlOptions as $key=>$value) {
$options[$key] = $this->evaluateExpression($value,array('row'=>$row,'data'=>$data));
}
}
else $options=$this->htmlOptions;
if($this->cssClassExpression!==null)
{
$class=$this->evaluateExpression($this->cssClassExpression,array('row'=>$row,'data'=>$data));
if(isset($options['class']))
$options['class'].=' '.$class;
else
$options['class']=$class;
}
echo CHtml::openTag('td',$options);
$this->renderDataCellContent($row,$data);
echo '</td>';
}
}
我們可以像這樣使用這個新類:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'article-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'title',
array(
'name' => 'author',
'value' => '$data->author->username'
),
array(
'class' => 'DataColumn',
'name' => 'sortOrder',
'evaluateHtmlOptions' => true,
'htmlOptions' => array('id' => '"ordering_{$data->id}"'),
),
array(
'class' => 'CButtonColumn',
),
),
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.