Clistview (I Hope This Is More Than A Rant)

I love developing applications with Yii, and I love the way Gii (and Giix) help me in scaffolding the applications. What I have totally learned to absolutely hate is CListView – which Gii uses for views/index.php. I hope that this is more than a rant, but if my impression of CListView is correct, why on earth did the Yii developers create such a limited, inept, powerless, useless class as CListView. As I understand it

  • I cannot control which columns display in CListView. I can specify a “select” in the criteria for CActiveDataProvider, but even with that, CListView still includes the columns, they’re just blank.

  • I have no control whatsoever over column width in CListView. I for instance have a table that has columns for title, which has a max length of 255 characters, and item number, which has a max length of four characters. Yet, CListView gives both columns the same width. (Totally stupid!)

  • I have very limited control over the data displayed in a column. For instance, I’d like to display substr(‘title’, 20) instead of the full title, but can’t do that in CListView.

  • When you try to look up anything regarding CListView on Google, all the results it brings back talk about CGridView, further emphasizing the universal lack of interest/use/utility of CListView.

  • And on, and on, and on

If I’m correct in this, I can’t imagine why anyone would ever used CListView when CGridView is so much more flexible and powerful, nor can I understand why Gii uses it for views/index.php. Can anyone tell me? In addition, can any one tell me if there’s an extension out there that will modify Gii so that it uses CListView for what it’s worth – absolutely nothing?

I will try to explain the usage for CListView using a reference to an ecommerce application. Imagine you got a product listing. You allow clients to switch between two views:

  • one where every product is in a distinct row and maybe there is a price column,

  • second where products are in cells and there are few cells in each row.

For the first one you would use a CGridView, which is basically just a table.

The second one requires rendering an distinct element for each record, that could be a partial view. This is where CListView come in handy.

Other example is when you use CGridView but one column holds much more data than others and it just looks ugly. Let me visualize that:




x x x YYYYYY

      YYYYYY

      YYYYYY



For such cases it would look a little better to use CListView and render each record as, for example:




(some title)

x x x

YYYYYYYY

YYYYYYYY



I use CListView rarely, but it does it job when needed.

I think Nick did a great job explaining the application of CListView.

It is common in frameworks to have this sort of “repeater” widget. I have used it in the past to create something like twitter’s activity stream - where each tweet has a slew of info in it, but they all need to look the same. A dataprovider, partial view and CListView make repeating that sort of display simple - with some added functionality in terms of sorting…etc.

Many thanks for both your responses. I can see the very limited uses for CListView that you describe. What I cannot understand is why Gii and Giix both use CListView (instead of CGridView) for the views/index.php action. What I’ve begun doing to completely rewriting index.php for every model to use CGridView, like the admin action but without the update and delete buttons. At that point, where I must rewrite so much every time I generate the CRUD for a model, the value and time-savings of a framework seriously diminish, and the question becomes, “Am I willing to do all this work and spend all this time while accommodating all the constraints of Yii, or is it better to build my own framework?”

Errr why not just make your own template for gii? And I don’t really recall having to switch from CListView to CGridView in anything generated by gii. Maybe because I started using giix almost immediately and then started building my own templates.

IMHO, those views serve mainly as examples. They’re useful for who is starting with the framework, but are almost always replaced with something else specific to the project.

Also, I second nineinchnick’s advice for creating your own template for the code generator.