Search box directive

You use the sfSearchBox to enable the visitors to search your website. The directive provides interface for user input used by the searching functionality.

The sfSearchBox is a directive with an isolated scope that is defined in a module with the same name sfSearchBox. You can use the directive as an attribute or as an element.
For more information, see Isolating the Scope of a Directive.


The search box directive exposes the following attributes:

Attribute Description
This attribute accepts a delegate to a function with one argument defined in the current scope. The search box invokes this delegate when the typed text is to be applied.
This attribute is an optional parameter. It is an integer specifying what is the minimum length of user input, so that the sfSearchBox directive calls sfAction. For example, if you set this attribute to -1, then the directive invokes sfAction on any change in the search box, independently of the length of the input.
This attribute is an optional parameter. It enables you to specify an HTML <input> placeholder Attribute for the input without changing the template.
This attribute activates the suggestions functionality. To use the functionality, you must combine the sf-enable-suggestions attribute with the sfGetSuggestions attribute below.
When you use the sfEnableSuggestions attribute, the sfGetSuggestions attribute accepts a delegate to a function with one argument defined in the current scope. This function must always return an array of strings that present the appropriate suggestions for the query provided in the arguments.
This attribute clears the text typed in search box. The value of the attribute must be a property in your scope.

Add a search box directive

The following example demonstrates how to add a search box directive in a widget designer's view.

To enable AngularJS to link the sfSearchBox directive in your custom designer view, you must load the script of the directive and add a dependency to the module.
Perform the following:

  1. Sitefinity CMS automatically registers the scripts you need and, if no other designer view with explicitly set priority exists, Sitefinity CMS sets your designer view priority 1. In case you need to have full control over the scripts that are loaded or you want to set custom priority, you can alternatively create your own DesignerView.YourView.json file. If you have a JSON file that matches the convention (even if empty), this automatic scripts registration will not occur. In the DesignerView.YourView.json file, add a scripts array. As a result, the file content should be similar to the following:
  2. In the designerview-yourview.js file, right before the definition of your custom view controller, place the following code snippet:
  3. In your DesignerView.YourView.cshtml file, place the following tag where you want to render the sfSearchBox directive:

    The following example passes a delegate to the search box to let the directive choose when the query needs to be applied, based on the attributes. In addition, you enable the suggestions functionality and define a suggest function. The function calls a service that gets items based on the current query. The resulting promise is returned after extracting the titles of the items.

  4. In your designer's controller, add the following code:

Want to learn more?

Sign up for our free beginner training. Boost your credentials through advanced courses and certification.
Register for Sitefinity training and certification.


Was this article helpful?

Next article

Sort box directive