GSTAR
(Omzy83)
July 30, 2014, 9:55pm
1
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.
yiqing95
(Yiqing 95)
July 31, 2014, 12:41am
2
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 !
GSTAR
(Omzy83)
August 17, 2014, 6:04pm
3
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.
joblo
(Joe)
August 17, 2014, 8:33pm
4
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();
...
GSTAR
(Omzy83)
August 17, 2014, 11:10pm
5
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.
joblo
(Joe)
August 18, 2014, 7:40am
6
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();
...
GSTAR
(Omzy83)
August 18, 2014, 10:01am
7
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?
joblo
(Joe)
August 18, 2014, 10:12am
8
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.
GSTAR
(Omzy83)
August 18, 2014, 7:33pm
9
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>
joblo
(Joe)
August 18, 2014, 8:04pm
10
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
GSTAR
(Omzy83)
August 18, 2014, 9:06pm
11
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