question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Search Modules: Create html element inside search result

See original GitHub issue

Previously I used type category for my search… I want to inject a simply html to the result such as image below, what I should be do here…

screenshot-127 0 0 1 8000 2017-01-20 22-51-43

$.fn.api.settings.api = {
  'search': '/api/search/json/?q={query}'
};

$('.ui.search.quick-search').search({
  type          : 'category',
  minCharacters : 1,
  apiSettings   : {
    action: 'search',
    onResultsAdd: function(html) {
      // i can't inject here..
    },
    onResponse: function(searchApiResponse) {
      var response = {
        results : {}
      };
      // translate `searchApiResponse` response to work with search
      var numbThreads = 0, numbUsers = 0;
      $.each(searchApiResponse.items, function(index, item) {
        var
          model = item.model || 'Unknown',
          maxResults = 8,
          maxItems = maxResults/2
        ;
        if(index >= maxResults) {
          return false;
        }
        // create new model category
        if(response.results[model] === undefined) {
          response.results[model] = {
            name    : model,
            results : []
          };
        }
        if(item.model === 'Threads') {
          if (numbThreads < maxItems) {
            response.results[model].results.push({
              title: item.title,
              description: item.description,
              url: item.url
            });
          }
          numbThreads++;
        }else if (item.model === 'Users') {
          if (numbUsers < maxItems) {
            response.results[model].results.push({
              title: item.username,
              description: item.username,
              url: item.profile_url,
              image: item.gravatar_url,
              price: item.total_threads
            });
          }
          numbUsers++;
        }
      });

      selector = $(document).find('.ui.search.quick-search');
      injector = '<div class="ui divider"></div>'+
                 '<button class="ui red button">'+
                    'Show more options...'+
                 '</button>';
      selector.find('.results').append(injector);
      
      console.log(response); // return Object such as image below..
      return response;
    }
  }
});// EOF

screenshot from 2017-01-20 23-12-33

I also has a problem to marked results by query. For example:

  • Query = text marked as bold
  • Results:
    1. Collaboratively extend fully tested networks for 2.0
    2. Proactively text marked as bold customer directed solutions via multidisciplinary.
    3. Continually lorem ipsum matrix excellent communities vis-a-vis team driven

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
agusmakmuncommented, Feb 1, 2017

finally… people suggested me to handle this problem at the backend, and of course using regex. I never think it before can be works with perfectly… 😸

http://stackoverflow.com/questions/41985525/semantic-ui-search-api-how-i-can-highlight-certain-characters-that-match-the#comment71148868_41985525

I think on item.description you should replace with a regex and wrap it with usually I do it from the backend, but in this case this is the only way I think. – peterpeterson.

currently I doing this case with django, and here is my backend script…

import re

def highlight_query(query, text):
    """
    return injected html element
    to the `text` that matched by query.
    """
    return re.sub(r'(%s)+' % query,
        '<span class="marked-text">%s</span>' % query, text)

results

Thank you so much @Cyb3rWarri0r8 and peterpeterson

0reactions
awgvcommented, Feb 4, 2017

Closing as requested.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Searching: getElement*, querySelector*
querySelectorAll(css) returns all elements inside elem matching the given CSS selector. ... Previous methods were searching the DOM.
Read more >
Search | Semantic UI
A search module allows a user to query for results from a selection of data. ... A search can look for results inside...
Read more >
Search Module in Semantic-UI - jquery - Stack Overflow
Semantic-UI (search) expects a "results" as root and nodes with childs content with title and description, and others that api specify.
Read more >
search-result.tpl.php | Drupal 7.x
This template renders a single search result and is collected into search-results.tpl.php. This and the parent template are dependent to one another sharing ......
Read more >
Web Scraping with Beautiful Soup - Pluralsight
Python offers an automated way, through various modules, to fetch the HTML content from the web (URL/URI) and extract data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found