1

I have an application that allows users to search for artwork and build their own portfolios. I'd like to show whether a piece of artwork that is returned in an algolia search is contained within the users portfolio, but I can't seem to find a way to make a callback once results are displayed to the user.

is it possible to add a callback to the search results?

taylor01
  • 53
  • 1
  • 5
  • You need to provide more details, what language are you using on what platform? Can you show a code sample? – Morgoth Mar 08 '17 at 14:07
  • yes, I'm sorry. First time ever posting on SO. Been a long-time reader. I'll take note and do better next time. – taylor01 Mar 10 '17 at 00:08
  • Does this answer your question? [Algolia instantsearch.js callback function](https://stackoverflow.com/questions/36640641/algolia-instantsearch-js-callback-function) – AKd Feb 24 '22 at 18:51

1 Answers1

3

I suppose you use instantsearch.js library for displaying the results.

Then there are two ways on how to achieve what you want.

1) Use transformData parameter of hits widget

The parameter takes callback and passes results there. So you can modify the results before it gets displayed - you can add a new parameter based on if the record is the one which should be highlighted.

Example:

search.addWidget(
  instantsearch.widgets.hits({
    container: '#hits-container',
    templates: {
      item: itemTemplate
    }, 
    transformData: {
      allItems: function (results) {
        console.log(results);

        // Modify results

        return results;
      }
    }
  })
);

2) Use custom widget to hook a render callback

You can write a simple custom widget where you specify only the render option:

search.addWidget({
  render: function(data) {
    console.log(data);
  }
});

In data parameter you'll get all information about the search, inluding helper, last state and the latest results.

Both approaches you can see live in small jsFiddle: https://jsfiddle.net/JanPetr/g54hzrzp/

Jan Petr
  • 685
  • 5
  • 11
  • 1
    This was perfect. The transformData parameter was exactly what I was looking for. It allowed me to update the results so I could process it in the view. – taylor01 Mar 10 '17 at 00:10
  • In v2 of instant-search, use 'item' instead of 'allItems'. https://community.algolia.com/instantsearch.js/v2/widgets-common-api.html#transformdata – Keyul Dec 22 '18 at 02:44
  • An alternative to #2: `search.on('render', function() { console.log('Algolia has just rendered the DOM.'); myCallback(); });` The only caveat is that no data is passed to it. – danemacmillan Mar 19 '19 at 19:32