Gravatar for sal.salvador@meau.com

Question by salpsx, Aug 12, 2015 5:54 PM

search.cshtml javascript framework appending text and applying formulas

how would you recommend I append text to coveo values or apply formulas (divison/multiplication) to numeric results as shown below.

In the html below, I am looking for the result to show 346KB:

<span class="CoveoFieldValue" data-field="@@syssize"></span> 

The syssize value returns bytes so I would like to convert that to KB, not sure of the approach. Also looking to append "KB" after the value but assuming the CoveoFieldValue css is what is making the file size and the text of 'KB' be off center with each other.

thanks

3 Replies
Gravatar for mlaporte@coveo.com

Answer by Martin Laporte, Aug 13, 2015 3:19 AM

If you have access to the raw template (unsure in what environment you're using the framework) you can do something like this:

<%=raw.myfield + 'KB'%>

Gravatar for jflheureux@coveo.com

Answer by Jean-François L'Heureux, Aug 13, 2015 9:55 AM

mlaporte's answer gives a good starting point. Since you mention "search.cshtml", I guess you are using Coveo for Sitecore in MVC.

If you analyze the response of a REST search query, you'll notice that each search result object has a "raw" property that contains all the fields of the result and that "raw.syssize" type is a number.

So instead of using a "CoveoFieldValue" element, you could use mlaporte's code like this:

{{= Math.round(raw.syssize / 1024) }} KB

Note that I use double brackets {{ }} instead of <% %> template style because this is the style used in Coveo for Sitecore underscore templates.

Gravatar for colema18@gmail.com

Answer by colema18, Aug 13, 2015 12:46 PM

Another way I would approach it, which may or may not be the best way, so I welcome feedback from @mlaporte, @jflheureux and the community because I am still new to the JS Framework way of doing things. You can add a data-attribute so the value is easily available in javascript and then you can tap into the newResultsDisplayed event to manipulate the presentation of that value. This would allow you to do something like rounding up or changing it to say MB instead of KB.

In your cshtml, within your CoveoResultList node you can do something like:

<span class="CoveoFieldValue" data-field="@@syssize" data-fieldtype="fileSize"></span>

In a referenced custom .js file you do something like:

$(document).ready(function () {
       Coveo.$('#searchResultList').on("newResultsDisplayed", postProcessing);
    });

var postProcessing = function (e, args) {
    $('span[data-fieldType="fileSize"]').each(function (i, obj) {
        var size = parseInt(obj.innerText / 1000);
        if (size < 1028) {
            obj.innerText = size + " KB";
        } else if (size > 1028) {
            obj.innerText = parseInt(size / 1000) + " MB";
        }
    });
};
Gravatar for colema18@gmail.com

Comment by colema18, Aug 13, 2015 1:19 PM

*1024 not 1028

Gravatar for colema18@gmail.com

Comment by colema18, Aug 13, 2015 1:20 PM

first / 1000 should be 1024 and the 1028 should be 1000 in the code above. Sorry wasn't thinking about the calculations as much as the approach when I wrote up the sample code.

Ask a question