Title Back Colour Keyoti Title Line Title Curve
Blue Box Top

Using JavaScript functions in results template - SearchUnit - Forum

Welcome Guest Search | Active Topics | Log In | Register

Options
DMacy
#1 Posted : Thursday, September 8, 2022 1:27:10 PM
Rank: Advanced Member

Groups: Registered

Joined: 9/1/2010
Posts: 136
Using the following link from the sew_resultItemTEMPLATE as an example, I want to make the href value ("${UriStringWithKeywords}") and the link text ("${Title}") the return values of JavaScript functions, using these values as input parameters. In other words, I want to modify the values that come from the Keyoti index for each item. How do I do this?

<a href="${UriStringWithKeywords}">${Title}</a>
Jim
#2 Posted : Thursday, September 8, 2022 7:08:57 PM
Rank: Advanced Member

Groups: Administrators, Registered

Joined: 8/13/2004
Posts: 2,669
Location: Canada
SearchUnit uses json2html (https://json2html.com/) and in theory you can use an inline function, though I've never tried it. You would for example set

keyotiResultViewer.resultItemTransform

to a template object, as described here https://json2html.com/ex...s/#example-basic-inline note that this is different to the html template string, instead it uses their object notation to define the template. I would get into more detail but I honestly don't know much about it.


The alternative, that may be simpler is to preprocess the results before display, you can do that by overriding (in javascript)

keyotiSearch.presentResults

eg.

Code:

<script type='text/javascript'>
var basePresentResults = keyotiSearch.presentResults;
keyotiSearch.presentResults = function(){
        //process results
        for (var i = 0; i < keyotiSearch.resultObject.Results.length; i++) {
                 //look here at     keyotiSearch.resultObject.Results[i] and see what data there is, and change as necessary     

        }
        basePresentResults();
}
</script>


I haven't tried that, but I think it will work. Let me know what you choose and how it goes, posting your attempt if it doesn't work.

Jim
-your feedback is helpful to other users, thank you!


DMacy
#3 Posted : Wednesday, September 14, 2022 11:12:06 PM
Rank: Advanced Member

Groups: Registered

Joined: 9/1/2010
Posts: 136
Hi Jim! Using the code sample shown in your alternative reply above, I'm getting the following JavaScript error:

this.highlightResultItems is not a function

Any ideas? Thanks!
Jim
#4 Posted : Friday, September 16, 2022 4:40:04 PM
Rank: Advanced Member

Groups: Administrators, Registered

Joined: 8/13/2004
Posts: 2,669
Location: Canada
Sorry, I think it should have been

Code:

<script type='text/javascript'>
keyotiSearch.basePresentResults = keyotiSearch.presentResults;
keyotiSearch.presentResults = function(){
        //process results
        for (var i = 0; i < keyotiSearch.resultObject.Results.length; i++) {
                 //look here at     keyotiSearch.resultObject.Results[i] and see what data there is, and change as necessary     

        }
        keyotiSearch.basePresentResults();
}
</script>
-your feedback is helpful to other users, thank you!


DMacy
#5 Posted : Friday, September 16, 2022 5:03:45 PM
Rank: Advanced Member

Groups: Registered

Joined: 9/1/2010
Posts: 136
Now it's working. Thanks!
Jim
#6 Posted : Monday, September 19, 2022 5:49:43 PM
Rank: Advanced Member

Groups: Administrators, Registered

Joined: 8/13/2004
Posts: 2,669
Location: Canada
Actually there is a possibility for stackoverflow, so please change to

Code:

<script type='text/javascript'>
if(typeof keyotiSearch.basePresentResults == 'undefined'){
    keyotiSearch.basePresentResults = keyotiSearch.presentResults;
}
keyotiSearch.presentResults = function(){
        //process results
        for (var i = 0; i < keyotiSearch.resultObject.Results.length; i++) {
                 //look here at     keyotiSearch.resultObject.Results[i] and see what data there is, and change as necessary     

        }
        keyotiSearch.basePresentResults();
}
</script>
-your feedback is helpful to other users, thank you!


Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.




About | Contact | Site Map | Privacy Policy

Copyright © 2002- Keyoti Inc.