Auto Collapse Search Refinement Panel in SharePoint 2013

A recent client desired to have selected refiners automatically collapsed on their search pages. This is because the resulting search results could have between 5 and 15 refiners, each showing up to 15 individual refinement options. The end result was a page that had a refiner list falling below the core results, resulting in a lot of scrolling to find a specific refiner. The client wanted to see if some of the refiners that were known to have longer refinement options could be collapsed by default to reduce the amount of scrolling needed.

This was relatively easy to implement. This leads back to the Srch object that comes along with display templates. Attached to this object is a Refinement object that is used by the default refiner display template (Filter_Default.html). There is a method off this object that is used by the display template to determine if the filter has been collapsed at some point by the user. Below is the original code in the template that is used to determine if the filter is collapsed.

        else
        {
            var isExpanded = Srch.Refinement.getExpanded(ctx.RefinementControl.propertyName);
            var iconClass = (isExpanded == "true"? "ms-ref-uparrow" : "ms-ref-downarrow");
            var displayStyle = (isExpanded == "true"? "" : "none");
            var refinerCatTitle = Srch.Refinement.getRefinementTitle(ctx.RefinementControl);

Exploring the Srch.Refinement object, another method was found that can set the expanded status: Srch.Refinement.setExpanded(property name, true/false). I modified the template by making a call to this method:

        else
        {
            Srch.Refinement.setExpanded(ctx.RefinementControl.propertyName, false);
            var isExpanded = Srch.Refinement.getExpanded(ctx.RefinementControl.propertyName);
            var iconClass = (isExpanded == "true"? "ms-ref-uparrow" : "ms-ref-downarrow");
            var displayStyle = (isExpanded == "true"? "" : "none");
            var refinerCatTitle = Srch.Refinement.getRefinementTitle(ctx.RefinementControl);

The template was then uploaded into the master page gallery and set as a Filter Display Template. This adds an additional filter that can be selected when setting up refiners in the refinement panel web part. Admittedly, there might be more logic needed for a better user experience as this will always collapse the refiner.

Advertisements

13 thoughts on “Auto Collapse Search Refinement Panel in SharePoint 2013

  1. I made a copy of the filter_default.html and inserted the code you suggested but how do I set as a Filter Display Template? Tried the right mouse click to see if it would give me the choice to set is as a default but not there. I went into the refiner web part to see if the selection was there, ans was not.

  2. Hi Chris,

    Thanks for your tutorial. I have a requirement to show the refinement panel as accordion. Kindly share your thoughts if you come across anything like this

  3. Hey, thank you for you Tutorial. It helped a a lot for my current requirement!

    For everyone who is interested I just wanted to mention that is is not enough for the Multi-Value Refinement.

    This is because the Multi-Value Refinement has the Style Property of the Container not implemented dynamic:

    In Filter_Default.html you have those lines:

    In Filter_MultiValue_Body.html the same lines look like this:

    That means if you want to use this for the MultiValue Refinement it will work if you add to the Filter_MultiValue_Body.html Code this line:
    var displayStyle = (isExpanded == “true”? “” : “none”);

    and adjust the html-lines with the dynamic style property like in Filter_Default.html

    And don’t forget to refer to the new file Filter_MultiValue_Body.html in Filter_MultiValue.html.

    1. Melanie,

      Thank you for this great tip. I apologize I wasn’t able to respond before you found the solution. Seems my blog gets a bit neglected when I’m busy with client work. 🙂

      Chris

    2. I am not sure where the lines are in the Filter_MultiValue_Body.html. Does the var displayStyle line appear above or below the line added to the Filter_Default.html? I am just not clear where the lines go?

    3. I am trying this solution fol the multi-value refiners, but I am missing the Style Property of the Container. Is it this one: iconClass = “ms-core-listMenu-item “; I need to amend?

  4. Hi Chris,

    I’ve been looking into doing this with the multiple item refinement template; it presents checkboxes so users can select multiple values within a refinement category. The problem is that it doesn’t seem possible to collapse a refiner and its values when allowing checkboxes. I’ve gone through your changes above in the correct display template, but haven’t had any luck. Have you done this or have any ideas?

  5. Thank for this Chris, I needed to do exactly the same thing because of the sheer number of refiners my client needs in place, its unlikely they would ever need to use more than a couple at any given time so this worked a treat to tidy up the page. I also find that a wall of refiners is pretty confusing to most users too, with them hidden they can take in the headings and then explore the values from there. Thanks again, would have taken ages to fathom!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s