Assign Clistview Widget To Variable

I want to assign the CListView widget to a variable so that I can render the individual elements separately.

Currently when I try to do this, it just echo’s / runs the widget instead. Here is some code:


<?php $listView = $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_item',

	'id'=>'items-grid',

	'template'=>'{items}',

)); ?>




<?php $listView->renderItems(); ?>

<?php $listView->renderPager(); ?>

I want to run renderPager() before the $listView instance. By the way I know I can do this using the ‘template’ option but I have a custom layout so it needs to be done this way.

the template can be a empty string :




  'template'=>'',



this will only register the necessary assets files(js css image etc) but render nothing firstly . then you can use $listView variable do a separate render !

this post Render Cgridview Pager Separately may give you some light !

that is partly correct. I noticed it actually creates an empty items container, i.e. in my case it generates the following:


<div id="items-grid" class="list-view">

    <div class="keys" style="display:none">

    ...

    </div>

</div>

The problem with this is that I need to generate the pager before the items container.

Use controllers createWidget method.

The init() method called on createWidget only registers the clientscripts without any other output.




$listView = $this->createWidget('CListView',array(

        'dataProvider'=>$dataProvider,

        'itemView'=>'_item',

        'id'=>'items-grid',

        'template'=>'{items}',

));


//call the methods you need:

...

$listView->renderPager(); 

...

$listView->renderItems();

...






Hi Joblo, I tried that and although the output is correct it does not seem to be registering the CListView JS file. So unfortunately AJAX actions do not work.

Ok, you have only take a look at the sources.

The init() called by createWidget publishes only the assests.

So you have to call registerClientScript() on rendering like in the method run() of the CBaseListView().




$listView = $this->createWidget('CListView',array(

        'dataProvider'=>$dataProvider,

        'itemView'=>'_item',

        'id'=>'items-grid',

        'template'=>'{items}',

));


$listView->registerClientScript(); //<-------------

...

$listView->renderPager(); 

...

$listView->renderItems();

...




Cheers for that, I will give that a try shortly.

One more thing I noticed was that the items container did not contain the ‘keys’ element. Is this important / do I need this for everything to work properly?

What’s about ‘renderKeys()’ -> again: study the source of method run()

[EDIT]

You don’t call run(), because you don’t $this->widget(…).

So you have to execute the necessary methods manually.

The method ‘renderContent()’ only parses the template and calls the assoziated method if they exists.

template=’{items} {pager} {nothing}’

will call renderItems() and renderPages() but not renderNothing() because this method not exists in CListView.

Thanks Joblo. This is the fully working code:




<?php

	$listView = $this->createWidget('zii.widgets.CListView', array(

		'dataProvider'=>$dataProvider,

		'itemView'=>'_item',

		'id'=>'items-grid',

		'template'=>'{items}',

	));

	

	$listView->registerClientScript();

?>


<div class="custom-container">

	<?php $listView->renderPager(); ?>

</div>


<?php

	echo CHtml::openTag($listView->tagName,$listView->htmlOptions)."\n";

	

	$listView->renderItems();

	

	$listView->renderKeys();

	

	echo CHtml::closeTag($listView->tagName);

?>


<div class="custom-container">

	<?php $listView->renderPager(); ?>

</div>



Will not this produce the same output?




...

template = '<div class="custom-container">{pager}</div>{items}<div class="custom-container">{pager}</div>',

..



And mybe wrap {items} with the list tagName and htmlOptions

Hi Joblo,

That is just a very basic example of my code - I did that to make it easy to understand.

In my ‘custom-container’ I have some other stuff too - such as a sorter and some dynamic content.

I have proposed this as a possible solution to this problem: https://github.com/yiisoft/yii/issues/3462