[EXTENSION] AlphaPager

I’m glad to inform that lower case with forceCaseInsensitive now works fine! :)

Would be nice now to integrate it with CListView! :)

CoLT

Hey,

  1. Gii

Well, i don’t think there is any need to provide a template for gii implementing AlphaPager. Everyone can create a custom template (as explained here) and add the code for AlphaPager nearly the same as it would be done when adding it to an existing controller and view file.

  1. Adding AlphaPager to CListView

This needs a little bit more to be done. Therefore i’ve created two files derived from CActiveDataProvider and CListView which adds AlphaPager to the ListView widget.

Here is an example for using this with the blog demo app. The posts are selected by the first letter of their title (well it’s just an example :D):

In PostController.php


    public function actionIndex()

    {

        $criteria=new CDbCriteria(array(

            'condition'=>'status='.Post::STATUS_PUBLISHED,

            'order'=>'update_time DESC',

            'with'=>'commentCount',

        ));

        if(isset($_GET['tag']))

            $criteria->addSearchCondition('tags',$_GET['tag']);


        // This is just for disabling buttons which have no results

        $acCriteria=new CDbCriteria;

        $acCriteria->select='DISTINCT(SUBSTR(`title`,1,1)) AS `title`'; 

        $chars = Post::model()->findAll($acCriteria);

        foreach($chars as $char)

            $activeChars[]=$char->title;


        $dataProvider=new ApActiveDataProvider('Post', array(

            'alphapagination'=>array(

                'attribute'=>'title',

                'activeCharSet'=>$activeChars,

            ),

            'pagination'=>array(

                'pageSize'=>Yii::app()->params['postsPerPage'],

            ),

            'criteria'=>$criteria,

        ));


        $this->render('index',array(

            'dataProvider'=>$dataProvider,

        ));

    }

and in posts index-view (views/post/index.php):


<?php $this->widget('ApListView', array(

    'dataProvider'=>$dataProvider,

    'itemView'=>'_view',

    'template'=>"{items}\n{pager}\n{alphapager}",

)); ?>

You’ll need ApActiveDataProvider.php and ApListView.php which are attached to this post.

Regards

Hello,

Nice! A bit dirty but working implementation!

:) But following questions goes here:

  1. After implementing AlphaPager with CListView - default Filtered values, Records count is no longer showed.

Using in Index View:


<?php $this->widget('ApListView', array(

    'dataProvider'=>$dataProvider,

    'itemView'=>'_view',

    'template'=>"{items}\n{pager}\n{alphapager}",

    'sortableAttributes'=>array(

        'ClLName'=>'Last name',

        'ClFName'=>'First name',

),

)); ?>

  1. How to place AlphaPager in the top of Index View?

CoLT

Yes, quick and dirty! :D But hey - it’s just an example.

Please have a look at CListViews docu for those things. E.g. to make record count visible you’ll need to add {summary} to the template property and to make the pager appear at the head you have to place their tags before {items}.


'template'=>"{summary}\n{alphapager}\n{pager}\n{items}",

Regards

Super! Thanks!

This extension really made my day! :D

AlphaPager is a must have extension ;D

Best regards,

CoLT

Somehow I don’t see ‘sortableAttributes’ with ‘template’=>"{summary}\n{alphapager}\n{pager}\n{items}\n{pager}",

Where is the problem?:)

CoLT

Hi,

add {sorter} to your template and set sortableAttributes:




<?php $this->widget('ApListView', array(

    'dataProvider'=>$dataProvider,

    'itemView'=>'_view',

    'template'=>"{sorter}\n{summary}\n{alphapager}\n{pager}\n{items}",

    'sortableAttributes'=>array(

          'title',

          'create_time'=>'Post Time',

    ),

)); ?>

Regards

Thanks… How did I missed that :D guess need to have a nap :D

Best regards,

CoLT

Small tip to all who are "fighting" with i18n. Just add collation to your select and all letters will be highlighted correctly in alphapager




$c->select='DISTINCT(substr(`lastname`,1,1)) COLLATE utf8_polish_ci AS `lastname` ';



Thanks for this useful extension with good enough documentation not to check its source code when you need something.

The only drawback is that its classes are not prefixed with E like EAlphaLinkPager that can possibly lead to naming conflicts.

Hi,

Thank you!

Yes, that’s unfortunately right. But changing the classnames now or in a next version is bad for everyone who is already using AlphaPager…

Best regards

Well, since it’s not placed under version control and can’t be used via externals, developers will not have unexpected problems with it. I think mentioning it in changelog or upgrade.txt will be enough.

That’s right, there wouldn’t be unexpected problems. It’s in fact pretty simple, because you would only need to do a search/replace on filepath for corresponding import or widget function calls, but i think i for myself wouldn’t normally expect such a change when i’m updating an extension from someone else and to be honest this would piss me off :rolleyes:

In general i agree that it would be better to prefix the classnames - my fault - and i’ll think about how to achieve this in a gently way. Cause i don’t wanna code Microsoft-style with keeping all failures and problems just for backward compatibility :D

Regards

@samdark:

There are more extensions "breaking the law" already. How about having some required checkboxes on the "Create new extension" page, that all need to be checked? Something like:




Please read and "sign" these conditions:


[ ] My extension doesn't break any existing licenses...

[ ] The classes in my extension don't use the "C" prefix, which is reserved for framework files

...



So no one should miss these rules anymore.

EDIT: And sorry for OT. Further discussion should be put into a new thread.

I need to get this working with GridView and was thinking about creating an ApGridView class that extends CGridView. Before I did that, I thought I’d check to see if you (yoshi) had any plans on doing so already. Thanks for everything you’ve already done!

Hi larry,

a few posts up is an (quick 'n dirty) example i’ve done to implement AlphaPager into a GridView by extending the ListView and the DataProvider.

But at the moment i’ve no further plans on making a clean extension of CGridView.

So you’re really welcome to do so! :)

Best regards

Hello Yoshi,

Thanks for your reply and, again, for your work. I may be the daftest person around, though, as I don’t see where you’ve posted a quick 'n dirty example to implement AlphaPager into GridView. I see your code for extending the ListView and the DataProvider (ApListView and ApDataProvider, respectively), but don’t see any reference to GridView. I suspect I’m missing something obvious. Are you just then editing the _view.php template so that it works like a table row?

I’ve muddled about trying to make an ApGridView class that extends CGridView, based upon what you did with CListView, but I get an error about CPagination not having a getCharSet method. I’ve figured out that this is happening because $this->pages (in AlphaLinkPager) ends up being a CPagination object, not an AlphaPagination one. I’m currently trying to hunt down why that’s happening.

Any insights into what I’m missing would be much appreciated. Thanks!

Hi larry,

no, i’m the jerk :D Of course you’re right the example is just for extending the ListView.

But i tried to directly port it to a GridView without having any problems.

Here is what i did:

[list=1][]Extending CGridView (exactly the same as with CListView, just change the classname etc.). The file is attached to this comment.[]Change the DataProvider inside your models search() function (using the ApDataProvider from previous post). This way there is no need to touch the controller.

E.g.:


$dataprovider = new ApActiveDataProvider('MyModel', array(

                'alphapagination'=>array(

                    'attribute'=>'Name',

                    //'activeCharSet'=>$activeChars,

                ),

                'pagination'=>array(

                    'pageSize'=>10,

                ),

                // ...

        ));

[*]Change the GridView inside your view file:


$this->widget('ApGridView', array(

    'dataProvider'=>$model->search(),

    'filter'=>$model,

    'template'=>"{alphapager}\n{pager}\n{items}",

// ...

));



[/list]

That’s it and it’s working like a charm over here :)

Hopefully this helps you otherwise just let me know!

Regards

No, absolutely not a jerk! Thank you very much for the help and I’m glad I wasn’t missing something obvious. Yes, no problem getting that working.

One thing I discovered is that it’s best to add a defaultScope() to the model adding an order that uses the same column as the AlphaPager. That way results are restricted by letter (thanks to the AlphaPager) but also in alphabetical order, which I think is more logical. You (yoshi) know this, of course, but others might appreciate the extra info.

A thousand thanks!

[size="4"]NEW VERSION 1.3 RELEASED[/size]


With everything needed to include AlphaPager with Grid-/ListView (including AJAX support). ActiveDataProvider, ArrayDataProvider and extended versions of GridView and ListView.

These are reworked versions of those already published within this topic.

Take care when updating from a previous version of Ap, because as mentioned by samdark i’ve updated the file- and classnames to avoid potential naming-conflicts. See README.txt from the package!

The documentation was also redesigned and a demopage will follow.

Best regards