Miura-shi
Miura-shi

Reputation: 4519

Javascript Fuzzy Search Breaking

I am using the following plugin by ListJS for a fuzzy search:

http://www.listjs.com/examples/fuzzy-search

I tried extending the plugin by adding my own method to filter by first letter, selecting from an A-Z list on click event.

The problem is as soon as you sort by letter, it breaks the search and wont filter anymore. I am storing the original list and adding it back in when you sort by letter because the ListJS plugin is removing list items and not hiding them by css. I am not sure if it's causing a problem or not? Any ideas?

JS Fiddle:

http://jsfiddle.net/xzzLuv3b/1/

HTML:

<div id="conditions-search-results">
    <div class="col-md-12 conditions-search">
        <h2 class="conditions-search-title">Find a Condition</h2>
        <div class="conditions-search-form text-center">
            <form class="form-inline form-flat-grey">       
                <div class="form-group">
                    <input type="text" class="form-control fuzzy-search" size="60" placeholder="Search by keyword or topic">
                </div>
            </form>
            <div class="divider-wrapper">
                <span class="divider-horizontal">OR</span>
            </div>
            <p>Choose by letter to browse conditions</p>
            <ul class="list-unstyled conditions list-inline conditions-search-sort">
                <li><a href="#">A</a></li>
                <li><a href="#">B</a></li>
                <li><a href="#">C</a></li>
                <li><a href="#">D</a></li>
                <li><a href="#">E</a></li>
                <li><a href="#">F</a></li>
                <li><a href="#">G</a></li>
                <li><a href="#">H</a></li>
                <li><a href="#">I</a></li>
                <li><a href="#">J</a></li>
                <li><a href="#">K</a></li>
                <li><a href="#">L</a></li>
                <li><a href="#">M</a></li>
                <li><a href="#">N</a></li>
                <li><a href="#">O</a></li>
                <li><a href="#">P</a></li>
                <li><a href="#">Q</a></li>
                <li><a href="#">R</a></li>
                <li><a href="#">S</a></li>
                <li><a href="#">T</a></li>
                <li><a href="#">U</a></li>
                <li><a href="#">V</a></li>
                <li><a href="#">W</a></li>
                <li><a href="#">X</a></li>
                <li><a href="#">Y</a></li>
                <li><a href="#">Z</a></li>  
            </ul>
        </div>
        <div class="col-md-12 conditions-wrapper">
<ul class="list-unstyled conditions-index list"><li class="condition">
                    <div class="condition-title">Arthritis</div>
                    <div class="condition-description"><p>Arthritis is another autoimmune disease that is long-term and causes inflammation of joints and the surrounding tissue. Severe cases have been known to affect other organs, as well.</p></div>
                </li><li class="condition">
                    <div class="condition-title">Back Pain</div>
                    <div class="condition-description"><p>Back pain can rear its ugly head in several forms. Whether you suffer from this condition due to genetics, age or from a work-related injury, we have the ability to help you with your discomfort.</p></div>
                </li><li class="condition">
                    <div class="condition-title">Carpal Tunnel</div>
                    <div class="condition-description"><p>Excessive pressure placed on the median nerve of the wrist. It may cause loss of feeling, immobility, numbness or tingling.</p></div>
                </li><li class="condition">
                    <div class="condition-title">Chronic Fatigue Syndrome</div>
                    <div class="condition-description"><p>Chronic Fatigue is continuous and often severe tiredness that isn’t remedied by rest and is not caused by any other known medical conditions.</p></div>
                </li><li class="condition">
                    <div class="condition-title">Degenerative Disc Disease</div>
                    <div class="condition-description"><p>Degenerative Disc Disease isn’t actually a disease. Rather, it’s a sanctioned medical term used to describe the normal changes that occurs in spinal discs as the body ages.*</p></div>
                </li><li class="condition">
                    <div class="condition-title">Degenerative Joint Disease</div>
                    <div class="condition-description"><p>Degenerative Joint Disease is more commonly known as Osteoarthritis. It is due to the wear and tear of joints throughout the body as it ages.</p></div>
                </li><li class="condition">
                    <div class="condition-title">Failed Surgery</div>
                    <div class="condition-description"><p>Failed Surgery, also known as Failed Back Surgery Syndrome (FBSS) is chronic pain in the back or legs after a spinal surgery.</p></div>
                </li><li class="condition">
                    <div class="condition-title">Fibromyalgia</div>
                    <div class="condition-description"><p>Fibromyalgia is a very real disorder causing constant pain and general unease. Those suffering from this condition are frequently in a constant state of pain.</p></div>
                </li><li class="condition">
                    <div class="condition-title">Gastroesophageal Reflux</div>
                    <div class="condition-description"><p>Gastroesophageal Reflux disease (GERD) occurs when the contents of the stomach leak backwards from the stomach into the esophagus.”</p></div>
                </li><li class="condition">
                    <div class="condition-title">Headaches</div>
                    <div class="condition-description"><p>Painful, chronic headaches can make even the simplest of daily tasks unbearable. Here at Pittsburgh Chiropractic and West Hills Medical Center we provide several services to ascertain the origin of your headaches and help alleviate the pain.</p></div>
                </ul>
        </div>
    </div>
</div>

Javascript:

    /**
     * Target conditions search list for fuzzy search
     * @type {List} List JS object
     */
    var conditionsList = new List('conditions-search-results', { 
      valueNames: ['condition-title'], 
      plugins: [ ListFuzzySearch() ] 
    });

    /**
     * Toggle list items when searching
     */
    $('.fuzzy-search').on('keypress', function(e){

      // Show conditions matched to the letter
      $('li.condition').show();

    });

    /**
     * Filter by Letter
     * @param {letter} Selected letter from search box
     */
    function filterByLetter(letter){
        $('.condition').filter(function() {     
              return $(this).find('.condition-title').text().charAt(0).toUpperCase() === letter;
        }).show();
    };

    /**
     * Filter click event
     * Sort by the letter that was clicked.
     */
    $('.conditions-search-sort a').on('click',function(e){
        e.preventDefault();

      // Restore the original list
      $('.conditions-index').replaceWith(conditionsIndex);

      // Hide all list items
        $('li.condition').hide();

      // Selected Letter
      var letter = $(this).text(); 

      // Filter and show list items
      filterByLetter(letter);

    });

    // Original conditions list index
    var conditionsIndex = $(conditionsList.list).clone();

Upvotes: 0

Views: 609

Answers (1)

Dhiraj
Dhiraj

Reputation: 33618

Using the List API filter the results of the fuzzy list instead of writing a custom filtering. This way ListFuzzySearch knows that the results have been filtered and thus the search will only work on the filtered results.

conditionsList.filter(); // resets the filter everytime

conditionsList.filter(function (item) {
    return $(item.elm).find('.condition-title').text().charAt(0).toUpperCase() == letter;
});

The filter method finally looks like this

$('.conditions-search-sort a').on('click', function (e) {
    e.preventDefault();
    
    var letter = $(this).text();

    conditionsList.filter();
    conditionsList.filter(function (item) {
        return $(item.elm).find('.condition-title').text().charAt(0).toUpperCase() == letter;
    });

});

Here is a demo http://jsfiddle.net/dhirajbodicherla/xzzLuv3b/3/


Update

If the filters should reset on typing then the following should do it

$('.fuzzy-search').on('keypress', function (e) {

    // Show conditions matched to the letter
    conditionsList.filter();
    $('li.condition').show();

});

 $('.conditions-search-sort a').on('click', function (e) {
    e.preventDefault();
    
    var letter = $(this).text();

    conditionsList.fuzzySearch.search(''); // this will make fuzzy ignore the text in the input.

    conditionsList.filter(function (item) {
        return $(item.elm).find('.condition-title').text().charAt(0).toUpperCase() == letter;
    });

});

Here is an updated demo http://jsfiddle.net/dhirajbodicherla/xzzLuv3b/6/

However, you would need a way to remove the filter. Probably by adding a reset after A-Z links?

Upvotes: 1

Related Questions