On Stackoverflow I answered a question about the strange behaviour of builtin bootstrap pager of the Extension Library. Bryan Schmiedeler asked me to share my own renderer.
How it started
Before Boostrap4XPages was pulled into the Extension Library I already has a requirement of a Bootstrap styled pager in a XPages application. I gave Bootstrap4XPages a try, but noticed some strange side effects in my application. So I switched off Bootstrap4XPages in the application and switched back to plain vanilla Bootstrap, but I took a quik look at the source code on Github and took the BoostrapPagerRenderer code and added to my own application. I changed to code to my needs.
Java class
The base of the new Bootstrap pager look and feel starts with the Java class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | package nl.elstarit.renderers; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.context.ResponseWriter; import javax.faces.render.Renderer; import com.ibm.commons.util.StringUtil; import com.ibm.xsp.FacesExceptionEx; import com.ibm.xsp.component.UIPager; import com.ibm.xsp.component.UIPagerControl; import com.ibm.xsp.component.xp.XspPager; import com.ibm.xsp.component.xp.XspPagerControl; import com.ibm.xsp.context.FacesContextEx; import com.ibm.xsp.event.PagerEvent; import com.ibm.xsp.extlib.util.ExtLibUtil; import com.ibm.xsp.util.AjaxUtilEx; import com.ibm.xsp.util.FacesUtil; import com.ibm.xsp.util.JavaScriptUtil; import com.ibm.xsp.util.TypedUtil; public class BootstrapPagerRenderer extends Renderer { public static final String VAR_PAGE = "page" ; @Override public void decode(FacesContext context, UIComponent component) { super .decode(context, component); // check that this component cause the submit if (decodeCausedSubmit(context, component)) { PagerEvent pagerEvent = new PagerEvent(component); String hiddenValue = FacesUtil.getHiddenFieldValue(context); if (StringUtil.isNotEmpty(hiddenValue)) { int pos = hiddenValue.lastIndexOf( '_' ); if (pos > - 1 ) { hiddenValue = hiddenValue.substring(pos + 1 ); if (isFirst(hiddenValue)) { pagerEvent.setAction(PagerEvent.ACTION_FIRST); } else if (isLast(hiddenValue)) { pagerEvent.setAction(PagerEvent.ACTION_LAST); } else if (isNext(hiddenValue)) { pagerEvent.setAction(PagerEvent.ACTION_NEXT); } else if (isPrevious(hiddenValue)) { pagerEvent.setAction(PagerEvent.ACTION_PREVIOUS); } else { try { int value = Integer.parseInt(hiddenValue); pagerEvent.setAction(PagerEvent.ACTION_GOTOPAGE); pagerEvent.setPage(value); } catch (NumberFormatException nfe) { return ; // just don't queue the event } } } else { return ; } } ((UIPager) component).queueEvent(pagerEvent); } } private boolean decodeCausedSubmit(FacesContext context, UIComponent component) { String currentClientId = component.getClientId(context); String hiddenValue = FacesUtil.getHiddenFieldValue(context); if (currentClientId != null && hiddenValue != null ) { return StringUtil.indexOfIgnoreCase(hiddenValue, currentClientId) > - 1 ; } return false ; } @Override public boolean getRendersChildren() { return true ; } @Override public void encodeChildren(FacesContext context, UIComponent component) throws IOException { if (context == null || component == null ) { throw new IOException(); } XspPager pager = (XspPager) component; UIPager.PagerState st = ((UIPager) component).createPagerState(); if (st == null ) { return ; } ResponseWriter writer = context.getResponseWriter(); encodePagerContent(context, writer, st, pager); } protected void encodePagerContent(FacesContext context, ResponseWriter w, UIPager.PagerState st, XspPager pager) throws IOException { int pageCount = st.getPageCount(); int start = getStart(st, pageCount); int end = getEnd(st, pageCount, start); String pagerId = pager.getClientId(context); boolean RTL = false ; // bootstrap 2 boolean b2 = false ; // bootstrap 3 boolean b3 = true ; w.startElement( "div" , null ); if (b2) { w.writeAttribute( "class" , ExtLibUtil.concatStyleClasses( "pagination" , pager.getStyleClass()), null ); } if (b3) { String pgClass = pager.getStyleClass(); if (StringUtil.isNotEmpty(pgClass)) { w.writeAttribute( "class" , pgClass, null ); } } if (StringUtil.isNotEmpty(pagerId)) { w.writeAttribute( "id" , pagerId, null ); } w.startElement( "ul" , null ); if (b3) { w.writeAttribute( "class" , "pagination" , null ); } List<?> listControls = pager.getChildren(); if (listControls.isEmpty()) { return ; } Iterator<?> it = listControls.iterator(); while (it.hasNext()) { Object obj = it.next(); if (obj instanceof XspPagerControl) { XspPagerControl control = (XspPagerControl) obj; String type = control.getType(); if (StringUtil.isNotEmpty(type)) { if (isFirst(type) || isNext(type) || isPrevious(type) || (isLast(type) && pager.isAlwaysCalculateLast())) { encodeAction(context, pager, st, w, control, type, start, end, RTL); continue ; } else if (isLast(type) && !pager.isAlwaysCalculateLast()) { if (!st.hasMoreRows()) { encodeAction(context, pager, st, w, control, type, start, end, RTL); } else { w.startElement( "li" , null ); w.writeAttribute( "class" , "disabled" , null ); w.startElement( "a" , null ); w.writeText(getMayBeMorePages(), null ); w.endElement( "li" ); w.endElement( "a" ); } continue ; } else if (type.equalsIgnoreCase(UIPagerControl.TYPE_GROUP)) { encodeGroup(context, pager, st, w, control, start, end); continue ; } else if (type .equalsIgnoreCase(UIPagerControl.TYPE_STATUS)) { encodeStatus(context, st, w, pager, control, start, end); continue ; } else if (isSeparator(type)) { encodeSeparator(context, w, control, type); continue ; } else if (type.equalsIgnoreCase(UIPagerControl.TYPE_GOTO)) { encodeGoto(); continue ; } } String msg = StringUtil .format( "Unknown control type {0}" , type); throw new FacesExceptionEx(msg); } } w.endElement( "ul" ); w.endElement( "div" ); } private void encodeAction(FacesContext context, XspPager pager, UIPager.PagerState st, ResponseWriter writer, XspPagerControl control, String type, int start, int end, boolean RTL) throws IOException { String clientId = pager.getClientId(context); String controlId = clientId + "__" + type; String defaultText = "" ; boolean renderLink = true ; if (isFirst(type)) { renderLink = st.getCurrentPage() > start; defaultText = "\u00AB" ; // First } else if (isPrevious(type)) { renderLink = true ; defaultText = "\u2039" ; // Previous } else if (isNext(type)) { renderLink = true ; defaultText = "\u203A" ; // Next } else if (isLast(type)) { renderLink = st.getCurrentPage() < end - 1 ; defaultText = "\u00BB" ; // Last } writer.startElement( "li" , null ); if (!renderLink) { writer.writeAttribute( "class" , "active" , null ); } // Generate the image link String val = (String) control.getValue(); if (StringUtil.isEmpty(val)) { val = defaultText; } // Generate the text link if (StringUtil.isNotEmpty(val)) { writer.startElement( "a" , null ); writer.writeAttribute( "id" , controlId + "__lnk" , null ); writer.writeAttribute( "href" , "#" , null ); if ( "next" .equals(val)) { writer.startElement( "i" , null ); writer.writeAttribute( "class" , "fa fa-chevron-right" , null ); writer.endElement( "i" ); } else if ( "previous" .equals(val)) { writer.startElement( "i" , null ); writer.writeAttribute( "class" , "fa fa-chevron-left" , null ); writer.endElement( "i" ); } else { writer.writeText(val, null ); } writer.endElement( "a" ); if (renderLink) { setupSubmitOnClick(context, pager, st, controlId, controlId + "__lnk" ); } } writer.endElement( "li" ); } private void encodeGroup(FacesContext context, XspPager pager, UIPager.PagerState st, ResponseWriter writer, XspPagerControl control, int start, int end) throws IOException { // Save the old page value Map<String, Object> requestMap = TypedUtil.getRequestMap(context .getExternalContext()); Object oldPage = requestMap.get(VAR_PAGE); String clientId = pager.getClientId(context); String controlId = clientId + "__" + control.getType(); //$NON-NLS-1$ // Encode the pages for ( int i = start; i < end; i++) { // Push the page number requestMap.put(VAR_PAGE, i + 1 ); boolean renderLink = (i != st.getCurrentPage()); writer.startElement( "li" , null ); if (!renderLink) { writer.writeAttribute( "class" , "active" , null ); } String val = (String) control.getValue(); if (StringUtil.isEmpty(val)) { val = Integer.toString(i + 1 ); } // Generate the text link if (StringUtil.isNotEmpty(val)) { writer.startElement( "a" , control); writer.writeAttribute( "id" , controlId + "__lnk__" + i, null ); writer.writeText(val, null ); writer.endElement( "a" ); if (renderLink) { setupSubmitOnClick(context, pager, st, controlId + "__lnk__" + i, controlId + "__lnk__" + i); } } writer.endElement( "li" ); } // Encode after the pages if (!pager.isAlwaysCalculateLast()) { if (end < st.getLastPage() || st.hasMoreRows()) { writer.startElement( "li" , null ); writer.writeAttribute( "class" , "disabled" , null ); writer.startElement( "a" , control); writer.writeText(getMayBeMorePages(), null ); writer.endElement( "a" ); writer.endElement( "li" ); } } // Restore the old page value if (oldPage != null ) { requestMap.put(VAR_PAGE, oldPage); } else { requestMap.remove(VAR_PAGE); } } private void setupSubmitOnClick(FacesContext context, XspPager component, UIPager.PagerState st, String clientId, String sourceId) { boolean immediate = false ; UIComponent subTree = ((FacesContextEx) context).getSubTreeComponent(); boolean partialExec = component.isPartialExecute(); String execId = null ; if (partialExec) { execId = component.getClientId(context); immediate = true ; } else { if (subTree != null ) { partialExec = true ; execId = subTree.getClientId(context); immediate = true ; } } boolean partialRefresh = component.isPartialRefresh(); String refreshId = null ; if (partialRefresh) { UIComponent refreshComponent = component .findSharedDataPagerParent(); if ( null == refreshComponent) { refreshComponent = (UIComponent) st.getDataIterator(); } refreshId = AjaxUtilEx.getRefreshId(context, refreshComponent); } else { if (subTree != null ) { partialRefresh = true ; refreshId = subTree.getClientId(context); } } final String event = "onclick" ; StringBuilder buff = new StringBuilder(); if (partialRefresh) { JavaScriptUtil.appendAttachPartialRefreshEvent(buff, clientId, sourceId, execId, event, /* clientSideScriptName */ null , immediate ? JavaScriptUtil.VALIDATION_NONE : JavaScriptUtil.VALIDATION_FULL, /* refreshId */ refreshId, /* onstart */ getOnStart(component), /* oncomplete */ getOnComplete(component), /* onerror */ getOnError(component)); } else { JavaScriptUtil.appendAttachEvent(buff, clientId, sourceId, execId, event, /* clientSideScriptName */ null , /* submit */ true , immediate ? JavaScriptUtil.VALIDATION_NONE : JavaScriptUtil.VALIDATION_FULL); } String script = buff.toString(); // Add the script block we just generated. JavaScriptUtil.addScriptOnLoad(script); } protected String getOnStart(XspPager component) { return (String) component.getAttributes().get( "onStart" ); // $NON-NLS-1$ } protected String getOnComplete(XspPager component) { return (String) component.getAttributes().get( "onComplete" ); // $NON-NLS-1$ } protected String getOnError(XspPager component) { return (String) component.getAttributes().get( "onError" ); // $NON-NLS-1$ } private void encodeStatus(FacesContext context, UIPager.PagerState st, ResponseWriter writer, XspPager pager, XspPagerControl control, int start, int end) throws IOException { writer.startElement( "li" , null ); writer.writeAttribute( "class" , "disabled" , null ); String val = (String) control.getValue(); if (StringUtil.isEmpty(val)) { val = "{0}" ; } if (StringUtil.isNotEmpty(val) && st.getLastPage() > 0 ) { writer.startElement( "a" , null ); val = StringUtil.format(val, st.getCurrentPage() + 1 , st .getLastPage(), start, end); writer.writeText(val, null ); writer.endElement( "a" ); } writer.endElement( "li" ); } private void encodeSeparator(FacesContext context, ResponseWriter writer, XspPagerControl control, String type) throws IOException { String val = (String) control.getValue(); writer.startElement( "li" , null ); if (StringUtil.isEmpty(val)) { String defaultSeparator = "|" ; if (type.equalsIgnoreCase(UIPagerControl.TYPE_SEPARATORPAGE)) { defaultSeparator = "Page" ; } val = defaultSeparator; } // Generate the text link if (StringUtil.isNotEmpty(val)) { writer.startElement( "a" , null ); writer.writeText(val, null ); writer.endElement( "a" ); } writer.endElement( "li" ); } private void encodeGoto() { // Do not exists in core XPages yet.. } private int getStart(UIPager.PagerState st, int pageCount) { int start = (st.getFirst() / st.getRows()) - pageCount / 2 ; start = Math.min(Math.max( 0 , st.getLastPage() - pageCount), Math.max( 0 , start)); return start; } private int getEnd(UIPager.PagerState st, int pageCount, int start) { int sizeOfPageRange = Math.min(start + pageCount, st.getLastPage()) - start; int end = start + sizeOfPageRange; return end; } private boolean isFirst(String type) { return (type.equalsIgnoreCase(UIPagerControl.TYPE_FIRST) || type.equalsIgnoreCase(UIPagerControl.TYPE_FIRSTARROW) || type .equalsIgnoreCase(UIPagerControl.TYPE_FIRSTIMAGE)); } private boolean isNext(String type) { return (type.equalsIgnoreCase(UIPagerControl.TYPE_NEXT) || type.equalsIgnoreCase(UIPagerControl.TYPE_NEXTARROW) || type .equalsIgnoreCase(UIPagerControl.TYPE_NEXTIMAGE)); } private boolean isLast(String type) { return (type.equalsIgnoreCase(UIPagerControl.TYPE_LAST) || type.equalsIgnoreCase(UIPagerControl.TYPE_LASTARROW) || type .equalsIgnoreCase(UIPagerControl.TYPE_LASTIMAGE)); } private boolean isPrevious(String type) { return (type.equalsIgnoreCase(UIPagerControl.TYPE_PREVIOUS) || type.equalsIgnoreCase(UIPagerControl.TYPE_PREVIOUSARROW) || type .equalsIgnoreCase(UIPagerControl.TYPE_PREVIOUSIMAGE)); } private boolean isSeparator(String type) { return (type.equalsIgnoreCase(UIPagerControl.TYPE_SEPARATOR) || type .equalsIgnoreCase(UIPagerControl.TYPE_SEPARATORPAGE)); } private String getMayBeMorePages() { return "..." ; // $NLS-PagerRenderer.MayBeMorePages-1$ } } |
RenderType
In my custom theme I have specified a RenderType
1 2 3 4 5 6 7 8 | <!-- Theme pager --> < control > < name >Pager</ name > < property > < name >rendererType</ name > < value >nl.elstarit.renderers.type.BootstrapPagerRenderer</ value > </ property > </ control > |
Final step
To make it work everything comes together in the faces-config.xml
1 2 3 4 5 6 7 8 | <!-- Bootstrap render pager --> < render-kit > < renderer > < component-family >com.ibm.xsp.Pager</ component-family > < renderer-type >nl.elstarit.renderers.type.BootstrapPagerRenderer</ renderer-type > < renderer-class >nl.elstarit.renderers.BootstrapPagerRenderer</ renderer-class > </ renderer > </ render-kit > |
Result
The final result is a list, which can be put every where on the XPages. And can align by css to the right or left.
great! I was just about to ask you if you could tell more about it/share some code. thanks!
May be good to know, that I first added bootstrap manually to my app, but after that Bootstrap4XPages is part of the Extension Library I removed Bootstrap from my app and used the builtin version
Hi Frank (and readers)
There is also a xsnippet for rendering a pager via CSS but I notice in the snippet the … notation for next set of pages is not styled properly (read: not styled at all) so I can definitely recommend your approach.