Just How Big Is My Content View?


Fri 26 August 2016 By Josh Fletcher

I’ve worked with quite a few data-oriented systems over the years. Coming from other, smaller scale systems I’ve come to expect them to display the total result count when viewing lists of data. Somewhat naively, I expected Nuxeo to provide me with the same information. I have since found that there are good reasons why this is not the case (see Limitations below). Nonetheless this has remained a bit of an unresolved pet peeve.

What I want is to display a simple result count like this:

Result Count

It turns out everything needed to achieve this already exists and, Even better, this can all be configured via Nuxeo Studio.

Widget Template

First I take advantage of the Paginable.getResultsCount() method via a Widget Template:

<f:subview xmlns:f="http://java.sun.com/jsf/core">
  <div class="label">Total Items: #{contentView.getCurrentPageProvider().getResultsCount()}</div>
</f:subview>

The “label” CSS class is used just to make things look nice. The main point is that I want access to the “getResultsCount()” method from Paginable. This comes via the Page Provider for the Content View, which is available via the “contentView” object using the “getCurrentPageProvider()” method. But why is the “contentView” object in scope? Read on.

Content View Action

To make things easy the item count is displayed as a Content View Action. This means I already have a Content View defined in the layout and thus I can use the “contentView” object. Here is the action definition:

<extension point="actions" target="org.nuxeo.ecm.platform.actions.ActionService">
  <action id="totalDocuments" label="Total Documents" order="-10" type="template">
    <category>CONTENT_VIEW_ACTIONS</category>
    <properties>
      <property name="template">
        /widgets/content-view-count-widget.xhtml
      </property>
    </properties>
  </action>
</extension>

The action definition is pretty simple since there is no actual “action” to take place. The category to get the Action to appear in Content Views is “CONTENT_VIEW_ACTIONS”. I want the result to be the left-most action, so the “order” is set to -10.

Limitations

Note that it is possible for the Paginable.getResultsCount() method to return negative values. Generally this happens if the underlying system (e.g. some sort of SQL database) cannot determine the total result count of a query, usually for optimization reasons.

One easy way to deal with this is simply hide the element if the value is less than 0 with some CSS. Here is an updated Widget Template:

<f:subview xmlns:f="http://java.sun.com/jsf/core">

  <style>
    .count[count^="-"],
    .count[count="0"] {
      display: none;
    }
  </style>

  <div class="count label"
      count="#{contentView.getCurrentPageProvider().getResultsCount()}">Total Items: #{contentView.getCurrentPageProvider().getResultsCount()}</div>

</f:subview>

Note that CSS attribute selectors really only deal with strings, not numbers, so count^="-" is looking for a value starting with “-” and not technically a negative number.

Conclusion

With two quick contributions to Nuxeo Studio it’s super easy to display the total result count in any Content View!


Tagged: Nuxeo Studio, How to