MODX Community

Can EventsX be paginated?

Hello,

I am trying to set up EventsX and I have most things working except pagination. There is no example of pagination in the docs. I thought I could combine with getResources and getPage, but I have had no success. getResources isn’t working for me. I am more of a front end person, so as soon as I start looking at snippets, I’m a bit out of my league. Any help would be greatly appreciated!

Here is my code that outputs but does not paginate:

[[!EventsX? &tpl=`evxEventTpl` &limit=`2`]]

[[!getPage?
&elementClass=`modSnippet`
&tpl=`evxEventTpl`
&element=`EventsX`
&limit=`3`
&debug=`1`
&pageLimit=`5`
&includeContent=`1`
&includeTVs=`1`
&processTVs=`1`
&pageNavVar=`page.nav`
]]
[[!+page.nav]]

A snippet that works with getPage has to accept the limit and offset properties for limiting the data set, and set a placeholder with the total number of items.

The snippet EventsX only accepts a limit property and doesn’t set the total placeholder. So you have to adjust the code to make it work. Duplicate the snippet EventsX and call it CustomEventsX. Then make these changes according to this post.

Snippet CustomEventsX:

//The first 19 lines of the snippet remain unchanged
...
$c = $modx->newQuery('evxEvent');
$c->andCondition(array('active' => 1, "enddate >= '".date('Y-m-d')."'"));

//These next 5 lines of code is the new stuff
$total = $modx->getCount('evxEvent',$c);
$totalVar = $modx->getOption('totalVar', $scriptProperties, 'total');
$modx->setPlaceholder($totalVar,$total);

$offset = $modx->getOption('offset',$scriptProperties,0);
$c->limit($limit,$offset);

//delete line 20 = $c->limit($limit);

//keep the rest of the code from line 21 to the end
$c->sortby('startdate', 'ASC');
$events = $modx->getCollection('evxEvent', $c);
...

Then call getPage with the new snippet:

[[!getPage?
&elementClass=`modSnippet`
&element=`CustomEventsX`
...
]]
1 Like

Wow, it works! I can’t thank you enough! I’ll try to study this… Maybe one day I can get to this level. Cheers! :smiley: