How can we help you today?

How to Set Default Values for the Filter Widget of the Search and Filter Toolbar

When you first add the Filter widget to a Design Element, there is no option to set a default value. As a result, the Design Element displays all content until you use the filter widget to refine the view.


with no default value


This article shows how to set a default value for the Filter widget with JavaScript. When you refresh your page, the Filter widget will display your preset default value:


with default value


NOTEThis solution is most useful for connected Design Elements where you retrieve a large amount of data from a source you want to filter. However, you can also use this for static Design Elements.


TABLE OF CONTENTS


Prerequisites


Step-by-step Tutorial

The steps have been divided into two parts. Follow them closely to learn how to set default Filter values.


Part 1: Activate the Filter Widget & Assign Class Name

Part 1 will focus on activating the filter widget and assigning a class name.


Step 1: Edit the ShortPoint Web Part

Go to the SharePoint page you want to use and click Edit:


Edit


Close the Toolbox:


close


Edit the ShortPoint web part:


Edit properties icon


Step 2: Edit the Design Element


NOTEBefore proceeding, you must already have a connected Design Element that allows you to use the Search & Filter Toolbar. The Search & Filter Toolbar is currently only available for these Design Elements: Date List, File List, Icon List, Image List, Events, Image Titles, Simple List, and Tiles. 

We recommend using this solution for connected Design Elements where you retrieve a large amount of data. We will be referring to a connected Image List Design Element in our example. However, this solution can also be used for static Design Elements.

connected image list


Click the Easypass tag of the connected Design Element. For this guide, we will use the Image List Design Element:


Easypass tag


Select the cogwheel icon:


Cogwheel icon


Step 3: Activate Filter

Switch to the Toolbar tab:


Toolbar tab


Select Filter:


Filter


Click the cogwheel icon:


Cogwheel icon


Add a Filter Title and remove the default content inside Filter In:


NOTEMake sure to take note of the Filter Title. You will need it for Part 2.


add title and delete default content


Select the Design Element property you want to target for the Filter. In our case, we want to filter by department, so we used the Department property:


NOTEYou can only see the properties in green if you have a connected Design Element. The Image List we are using is connected to People Search.


select design element property


Click Save:


Save


Then, click Activate:

Activate


Step 4: Add a Class Name

Click the next icon:


next icon


Select the Custom CSS tab:


Custom CSS tab


In the Class field, assign a class name. You can assign any name you want. Just make sure to take note of it for Part 2. In our case, we will use ‘apply-default-filter-1’:


add class name


Finally, apply your changes:


apply your changes


You can now proceed to Part 2 of this tutorial.


Part 2: Add JavaScript Code

This part will focus on preparing and adding the JavaScript code to your page.


Step 1: Copy and Prepare the JavaScript Code

Copy the code below:

setTimeout(() => {
/* CONFIGURE BELOW 3 LINES ONLY */
const filterTitle = "FilterTitle";
const defaultValue = "DesignElementProperty";
const filteredElement = "Class Name";
/* CONFIGURE ABOVE 3 LINES ONLY */

const targetedToolbar = `div:has(> .${filteredElement}) > .sp-smartoolbar`;
document.querySelector(`${targetedToolbar} [data-test="sp-toolbar-filter-btn"]`)?.click();
document.querySelector(`${targetedToolbar} [data-test="sp-filter-clear-btn`)?.click();
document.querySelector(`${targetedToolbar} [data-test="sp-toolbar-select-input"]:has(label[title="${filterTitle}"]`)?.click();
document.querySelector(`${targetedToolbar} [data-test="sp-toolbar-options-wrapper"]`)?.click();
document.querySelector(`${targetedToolbar} [data-test="sp-toolbar-options"][data-sp-value="${defaultValue}"]`)?.click();
document.querySelector(`${targetedToolbar}`).click();
}, 1500);

Replace the following values:

  • FilterTitle - replace with the title of your filter;
  • DesignElementProperty- replace with the design element property you set in Part 1;
  • ClassName - replace with the Class name you assigned in Part 1.


In our case, our FilterTitle is Department, our DesignElementProperty is Marketing, and our ClassName is apply-default-filter-1. As a result, our code now looks like this:

setTimeout(() => {
/* CONFIGURE BELOW 3 LINES ONLY */
const filterTitle = "Department";
const defaultValue = "Marketing";
const filteredElement = "apply-default-filter-1";
/* CONFIGURE ABOVE 3 LINES ONLY */

const targetedToolbar = `div:has(> .${filteredElement}) > .sp-smartoolbar`;
document.querySelector(`${targetedToolbar} [data-test="sp-toolbar-filter-btn"]`)?.click();
document.querySelector(`${targetedToolbar} [data-test="sp-filter-clear-btn`)?.click();
document.querySelector(`${targetedToolbar} [data-test="sp-toolbar-select-input"]:has(label[title="${filterTitle}"]`)?.click();
document.querySelector(`${targetedToolbar} [data-test="sp-toolbar-options-wrapper"]`)?.click();
document.querySelector(`${targetedToolbar} [data-test="sp-toolbar-options"][data-sp-value="${defaultValue}"]`)?.click();
document.querySelector(`${targetedToolbar}`).click();
}, 1500);

Step 2: Insert the Code Design Element

Click the plus icon to open the library of Design Elements:


plus icon


Use the search bar to look for Code and select it:


Code


Step 3: Paste the JavaScript Code

Paste your prepared JavaScript Code in the JavaScript field:


paste code


Step 4: Disable Sandbox Mode

Switch to the Design tab:


Design tab


Make sure to disable Sandbox Mode:


disable Sandbox Mode


Click apply:


apply


Step 5: Save

You will notice that the default value has already been applied. Once satisfied, click Save:


Save


That’s it! You have successfully set a default filter value. Every time you visit the page, this default value will automatically be shown.

NOTEYour users can still use the filter widget to narrow down other properties. The default value will be shown again when you reload the page.

with default value


Related articles:

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.

World's best intranet sites are designed using ShortPoint

Get started today! Learn more
See all 6 topics

Start a trial

Ignite your vision. Install ShortPoint directly on your site, or play in sandbox mode. No credit card required.

Get started today

World’s best intranet sites are designed using ShortPoint

Thousands of companies using ShortPoint everyday to design, brand and build award winning intranet sites.

Get started Learn more