Widget Pagination - Global Solution

TL;DR: Global solution to setting dataprovider pagination in widgets. Click for source code


A recurring thread among developers in the community, is typically around how to change the default pagination in widgets.

The issue is that, while any widgets with paging support generally allow you to specify pagination pagesize, this setting is typically ignored when CDataProvider is initialized, as it creates it’s own pagination object with default settings (which all occurs before the widget has a chance to apply it’s pagination default settings).

Solutions to this issue are generally one of:

  • Update every location where a DataProvider is being initialized, to specify it’s pagination settings

  • -OR - Extend each DataProvider class that needs custom pagination defaults, and then update your code everywhere to use the new DataProviders

  • -OR - Extend each widget that provide pagination, to customize how it handles setting pagination settings

This is an obvious issue, for which the solutions generally involve customizing multiple classes or every instance of the affected classes.

My solution, is instead to extend CWidgetFactory, add 2 new event handlers surrounding the creation of widgets, and then use the event handlers to check the type of the created widget, and set the desired default pagesize value.

[color="#FF0000"]You can see my approach here (source code):[/color] https://gist.github.com/4513672

Obviously this solution is one that you can easily customize to fit your own logic as needed, due to the use of event handlers.

One possible issue with my usage of the onAfterCreateWidget event, is that by setting pagesize at that point, I may be overriding a pagesize parameter passed via widget configuration properties. A solution to that possible issue, would be to check the $event->params for variables that are instances of CDataProvider, CPagination, CBasePager, or check for array keys that would define the config for creation of the pagination details, and if a custom pagesize value is specified, simply ensure it’s applied where my code is already applying the $defaultPageSize value.

That’s up to someone else to pursue though :slight_smile:

Feel free to fork my gist if you have any improvements/enhancements that you would like to provide.