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?

3
  • You need to provide more details, what language are you using on what platform? Can you show a code sample?
    – Morgoth
    Mar 8, 2017 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, 2017 at 0:08
  • Does this answer your question? Algolia instantsearch.js callback function
    – AKd
    Feb 24, 2022 at 18:51

1 Answer 1

4

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/

3
  • 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, 2017 at 0:10
  • In v2 of instant-search, use 'item' instead of 'allItems'. community.algolia.com/instantsearch.js/v2/…
    – Keyul
    Dec 22, 2018 at 2: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. Mar 19, 2019 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.