Search topic and topic

Asked

Viewed 81 times

1

I believe it should be simple, but, I did not get a perfect functioning.

I am developing a hotsite to provide some slides, theories and codes during the realization of Codelab’s in certain programming languages, created a menu to separate by topics and subtopics.

I’m having difficulty developing a field to perform the search, in the text field I’m using the Javascript function:

$("#search_box").on("keyup", function() {
    
  if(!String.prototype.trim) {
    
    String.prototype.trim = function () {
      
      return this.replace(/^\s+|\s+$/g,'');
      
    };
  }
  
  var box = $(this);
  var keyword = box.val().toLowerCase().trim();
  
  $(".subtopic").each(function() {
    var subtopicItem = $(this);
    
    if (subtopicItem.text().toLowerCase().trim().indexOf(keyword) == -1) {
      subtopicItem.hide();
    } 
    
    else {
      subtopicItem.show();
    }
  });
  
  $(".topic").each(function() {
    var topicItem = $(this);
    
    if (topicItem.text().toLowerCase().trim().indexOf(keyword) == -1) {
      topicItem.hide();
    } 
    
    else {
      topicItem.show();
    }
  });
});

When we perform the Subtopic search, the search returns only the Subtopic hiding the others, and also showing the topic in which it is related.

Example 1: searching for the word "Introduction"

HTML & CSS -> Topical
     Introducing -> Sub-topic

Java -> Topical
     Introducing -> Sub-topic

C -> Topical
     Introducing -> Sub-topic

Example 2: when fetching the word "HTML"

HTML & CSS -> Topical

Expected: when fetching the word "HTML"

HTML & CSS -> Topical
     Introducing -> Sub-topic
     Table -> Sub-topic
     Lists -> Sub-topic
     Links -> Sub-topic
     ... -> Sub-topic
     ... -> Sub-topic

Would like to perform the following function: when researching the topics, also show all the related subtopics in that particular subject.

1 answer

0

Maybe this javascript plugin is what you’re looking for: http://www.listjs.com/

A functional example:

var options = {
  valueNames: [ 'name', 'born' ]
};

var userList = new List('users', options);
h2 {
  font-family:sans-serif;
}
.list {
  font-family:sans-serif;
  margin:0;
  padding:20px 0 0;
}
.list > li {
  display:block;
  background-color: #eee;
  padding:10px;
  box-shadow: inset 0 1px 0 #fff;
}
.avatar {
  max-width: 150px;
}
img {
  max-width: 100%;
}
h3 {
  font-size: 16px;
  margin:0 0 0.3rem;
  font-weight: normal;
  font-weight:bold;
}
p {
  margin:0;
}

input {
  border:solid 1px #ccc;
  border-radius: 5px;
  padding:7px 14px;
  margin-bottom:10px
}
input:focus {
  outline:none;
  border-color:#aaa;
}
.sort {
  padding:8px 30px;
  border-radius: 6px;
  border:none;
  display:inline-block;
  color:#fff;
  text-decoration: none;
  background-color: #28a8e0;
  height:30px;
}
.sort:hover {
  text-decoration: none;
  background-color:#1b8aba;
}
.sort:focus {
  outline:none;
}
.sort:after {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid transparent;
  content:"";
  position: relative;
  top:-10px;
  right:-5px;
}
.sort.asc:after {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid #fff;
  content:"";
  position: relative;
  top:13px;
  right:-5px;
}
.sort.desc:after {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 5px solid #fff;
  content:"";
  position: relative;
  top:-10px;
  right:-5px;
}
<div id="users">
  <input class="search" placeholder="Search" />
  <button class="sort" data-sort="name">
    Sort by name
  </button>

  <ul class="list">
    <li>
      <h3 class="name">Jonny Stromberg</h3>
      <p class="born">1986</p>
    </li>
    <li>
      <h3 class="name">Jonas Arnklint</h3>
      <p class="born">1985</p>
    </li>
    <li>
      <h3 class="name">Martina Elm</h3>
      <p class="born">1986</p>
    </li>
    <li>
      <h3 class="name">Gustaf Lindqvist</h3>
      <p class="born">1983</p>
    </li>
  </ul>

</div>
<script src="http://listjs.com/no-cdn/list.js"></script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.