Skip to content

Reuters tutorial: step 3

jpmckinney edited this page Apr 11, 2013 · 14 revisions

Table of Contents

AJAX Solr comes with a good jQuery pager widget, based on the will_paginate plugin for Ruby on Rails, so we need only override some of its properties and methods to get the functionality we want.

First, add the JavaScript file for the distributed widget:

<script src="../../lib/widgets/jquery/PagerWidget.js"></script>

Now, add an instance of the widget to the Manager in reuters.js:

Manager.addWidget(new AjaxSolr.PagerWidget({
  id: 'pager',
  target: '#pager',
  prevLabel: '&lt;',
  nextLabel: '&gt;',
  innerWindow: 1,
  renderHeader: function (perPage, offset, total) {
    $('#pager-header').html($('<span></span>').text('displaying ' + Math.min(total, offset + 1) + ' to ' + Math.min(total, offset + perPage) + ' of ' + total));
  }
}));

In addition to the basic id and target properties, PagerWidget exposes some of its own properties. We implement the abstract method renderHeader to display the number of results found.

In this iteration, we showed how to implement a widget by overriding an existing widget’s properties. If we were adding a significant amount of functionality, we should have created a new widget, as we did with the results widget. In this case, we had little code to write, so overriding the pager widget’s properties and implementing its abstract methods during instantiation makes sense.

[What we have so far]

Let’s introduce faceting with a tagcloud widget.