Yii Framework Forum: Assign Clistview Widget To Variable - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Assign Clistview Widget To Variable Rate Topic: -----

#1 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 30 July 2014 - 04:55 PM

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.
0

#2 User is offline   yiqing95 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 604
  • Joined: 27-December 10
  • Location:china

Posted 30 July 2014 - 07:41 PM

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 !
0

#3 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 17 August 2014 - 01:04 PM

View Postyiqing95, on 30 July 2014 - 07:41 PM, said:

this will only register the necessary assets files(js css image etc) but render nothing firstly


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.
0

#4 User is offline   Joblo 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 699
  • Joined: 12-September 10
  • Location:Austria

Posted 17 August 2014 - 03:33 PM

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();
...



0

#5 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 17 August 2014 - 06:10 PM

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.
0

#6 User is offline   Joblo 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 699
  • Joined: 12-September 10
  • Location:Austria

Posted 18 August 2014 - 02:40 AM

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();
...


0

#7 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 18 August 2014 - 05:01 AM

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?
0

#8 User is offline   Joblo 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 699
  • Joined: 12-September 10
  • Location:Austria

Posted 18 August 2014 - 05:12 AM

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.
0

#9 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 18 August 2014 - 02:33 PM

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>

0

#10 User is offline   Joblo 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 699
  • Joined: 12-September 10
  • Location:Austria

Posted 18 August 2014 - 03:04 PM

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
0

#11 User is offline   GSTAR 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,039
  • Joined: 30-October 09
  • Location:UK

Posted 18 August 2014 - 04:06 PM

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/y...yii/issues/3462
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users