简体   繁体   中英

YII CListView How to add id to itemwrapper

In CListView there is a property called "itemsCssClass" which basically adds HTML class attribute to the Itemwrapper.

What if I like to add an ID or any other htmlOptions how will I do it on that wrapper..?

My code is :

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

This code will produce this 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>

Thanks for your help in advance

From the Yii's source:

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

I just see class attribute is passed to the itemsTagName so you probably have to extend CListView to do it.

You can create a CCustomListView class (inside application/widgets folder) which extends from CListView and overwrite renderItems() function. For example:

<?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);
    }
}

In your view, you can use it like:

<?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',
)); ?>

So the style which in itemsHtmlOptions will be applied for the listview .

This link is also useful for you: How to extend CListView in order to remove extra yii added markup?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM