Example: Place a custom label with search response data

Goal

Display any field from the search results as a custom label. This label can be used in any of the components and could fetch all field data associated with the current set parameters. For visualizing data in the layout, the CustomResultsLabel component is used. It will be placed in the extended template of the designated parent component. For this example - the Results component.

Steps to place the label

  1. Navigate to the directory of the currently active widget template (e.g. Mvc\Views\Hawksearch\Hawksearch.Default.cshtml or the relevant resource package).

  2. Copy the file of the base template and name it accordingly.

  3. Edit the file adding or replacing the template override for the Results component:

    <script id="vue-hawksearch-results" type="x-template"> <div class="hawk-results" v-on:click="onClick"> <autocorrect-suggestions /> <search-results-label /> <banner></banner> <selections /> <template v-if="searchError"> <span>{{ $t('response_error_generic') }}</span> </template> <template v-else-if="searchOutput && searchOutput.Results && searchOutput.Results.length == 0"> <span>{{ $t('No Results') }}</span> </template> <template v-else-if="!waitingForInitialSearch"> <tabs></tabs> <div class="hawk-results__top-tool-row"> <tool-row /> </div> <result-listing /> <div class="hawk-results__bottom-tool-row"> <tool-row /> </div> </template> </div> </script>
  4. Add the <custom-results-label /> where the response field should be visualized (ln.7). In this example, the number of results is displayed after the search results label. The name of the field is passed to the results-field prop (i.e. Pagination.NofResults). These values follow the same convention as the one in the result item:

    <script id="vue-hawksearch-results" type="x-template"> <div class="hawk-results" v-on:click="onClick"> <autocorrect-suggestions /> <search-results-label /> <custom-results-label results-field="Pagination.NofResults"></custom-results-label> <banner></banner> <selections /> <template v-if="searchError"> <span>{{ $t('response_error_generic') }}</span> </template> <template v-else-if="searchOutput && searchOutput.Results && searchOutput.Results.length == 0"> <span>{{ $t('No Results') }}</span> </template> <template v-else-if="!waitingForInitialSearch"> <tabs></tabs> <div class="hawk-results__top-tool-row"> <tool-row /> </div> <result-listing /> <div class="hawk-results__bottom-tool-row"> <tool-row /> </div> </template> </div> </script>
  5. The end result so far is only the value of the field. To dynamically display additional labels around the required field, the component provides several slots. Currently, there are three available: before, after and default (i.e. instead) slot. With this example, the label will be either Found 12 results or No results found depending on the search output.

    ... <custom-results-label results-field="Pagination.NofResults"> <template v-slot:before> Found </template> <template v-slot:after> results </template> <template v-slot> No results found </template> </custom-results-label> ...
  6. Include the file in the project.

  7. Save, build and reload the site.