Search topic and subtopic

1

I think it should be simple, but I did not get a perfect run.

I'm developing a hotsite to make some slides, theories and codes available during the development of CodeLab's in certain programming languages, I've created a menu to separate by topics and subtopics.

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

$("#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 by hiding the others, and also showing the topic in which it is related.

Example 1 : fetching the word "Introduction"

HTML & CSS - > Topic
Introduction - > Subtopic

Java - > Topic
Introduction - > Subtopic

C - > Topic
Introduction - > Subtopic

Example 2 : fetching the word "HTML"

HTML & CSS - > Topic

Expected : fetching the word "HTML"

HTML & CSS - > Topic
Introduction - > Subtopic
Table - > Subtopic
Lists - > Subtopic
Links - > Subtopic
... - > Subtopic
... - > Subtopic

I'd like to perform the following function: when we search for topics, also show all related subtopics on that subject.

    
asked by anonymous 11.10.2016 / 13:46

1 answer

0

Maybe this javascript plugin is what you are looking for: link

A working 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>
    
11.10.2016 / 14:53