I have the following problem,
I created a widget, that has the main function to show me all of my news-pieces. The news-piece has a field named "priority" which can be an integer that ranges from 0 to 100.
The main thing i want to do is to sort by "priority".
The following HTML-Code is my news-widgets/views/widget.html
{% for piece in data.widget._pieces %}
<div class="commercial_widget_wrapper">
<a href="{{ piece.detaillink }}">
<div class="commercial_widget_inner row">
<div class="commercial_widget_thumbnail">
{% set image = apos.images.first(piece.thumbnail) %}
{% if image %}
<img class="bild" src="{{ apos.attachments.url(image, { size: 'full' }) }}"/>
{% endif %}
{% if image == null %}
<div class="bild">
{{apos.area(piece, 'thumbnail', {edit:false, size: 'full'})}}
</div>
{% endif %}
</div>
<div class="commercial_widget_label">
<b>{{piece.title}}</b><br>
{{piece.text}}
</div>
</div>
</a>
<div style="clear:both;"></div>
</div>
{% endfor %}
I added the widget in the show.html of a page with
{{apos.singleton(data.page, 'news', 'news', {})}}
My Question is, how and where can i sort my results? I tried to find something with the help of apostrophe-cursors but the docs lack a good example.
This SO question illustrates where you can interact with each widget's data before it makes it to your template
How-to access widget data in helper
By hooking into the load
method of your widget you will have access to all the associated pieces. You can then use something like lodash's sortBy
to reorder the array based on a property within each item.