在CListView中,有一个名为“ itemsCssClass”的属性,该属性基本上将HTML类属性添加到Itemwrapper。

如果我想添加ID或任何其他htmlOptions,该怎么办?

我的代码是:

    <?php $this->widget('zii.widgets.CListView', array(
    'dataProvider'=>$model->search(),
    'viewData'=>array('x'=>''),
    'itemView'=>'_classifieds',
    'id'=>'boa_ads',
    'itemsCssClass'=>'test'
));


此代码将产生以下HTML:

<div id="boa_ads" class="list-view">
 <div class="summary">Displaying 1-4 of 4 results.</div>

   <div class="items"><!-- HERE ID LIKE TO ADD AN ID-->
     ----ITEMS GOES HERE ----
   </div>

 </div>
</div>


谢谢您的帮助

最佳答案

从Yii的来源:

public function renderItems()
    {
            echo CHtml::openTag($this->itemsTagName,array('class'=>$this->itemsCssClass))."\n";
             .....
            echo CHtml::closeTag($this->itemsTagName);
    }


我只是看到class属性被传递给itemsTagName,所以您可能必须扩展CListView来实现。

您可以创建一个从CListView扩展的CCustomListView类(在application/widgets文件夹内)并覆盖renderItems()函数。例如:

<?php

Yii::import("zii.widgets.CListView");

class CCustomListView extends CListView
{

    public $itemsHtmlOptions;
    /**
     * Renders the data item list.
     */
    public function renderItems()
    {
        echo CHtml::openTag($this->itemsTagName, array_merge(array('class'=>$this->itemsCssClass), $this->itemsHtmlOptions))."\n";
        $data=$this->dataProvider->getData();
        if(($n=count($data))>0)
        {
            $owner=$this->getOwner();
            $viewFile=$owner->getViewFile($this->itemView);
            $j=0;
            foreach($data as $i=>$item)
            {
                $data=$this->viewData;
                $data['index']=$i;
                $data['data']=$item;
                $data['widget']=$this;
                $owner->renderFile($viewFile,$data);
                if($j++ < $n-1)
                    echo $this->separator;
            }
        }
        else
            $this->renderEmptyText();
        echo CHtml::closeTag($this->itemsTagName);
    }
}


在您看来,可以像这样使用它:

<?php $this->widget('application.widgets.CCustomListView', array(
    'dataProvider'=> 'your_data_provider',
    'itemsHtmlOptions' => array('style' => 'color:blue', 'id' => 'your_id'),
    'itemView'=>'your_item_view',
    'template'=>'your_template',
)); ?>


因此,itemsHtmlOptions中的样式将应用于listview

该链接对您也有用:How to extend CListView in order to remove extra yii added markup?

10-08 08:00