Solved

Edit jquery to show all <li> initially, but show matched

Posted on 2012-04-09
4
554 Views
Last Modified: 2012-08-14
Hi All,

i am using the following jquery to find matching list items:

  jQuery.expr[':'].Contains = function(a,i,m){
      return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
  };


  function listFilter(header, list) { // header is any element, list is an unordered list
    // create and add the filter form to the header
    var form = $("<form>").attr({"class":"filterform","action":"#"}),
        input = $("<input>").attr({"class":"filterinput","type":"text"});
    $(form).append(input).appendTo(header);

    $(input)
      .change( function () {
        var filter = $(this).val();
        if(filter) {
          // this finds all links in a list that contain the input,
          // and hide the ones not containing the input while showing the ones that do
          $(list).find("a:not(:Contains(" + filter + "))").parent().slideUp();
          $(list).find("a:Contains(" + filter + ")").parent().slideDown();
        } else {
          $(list).find("li").slideDown();
        }
        return false;
      })
    .keyup( function () {
        // fire the above change event after every letter
        $(this).change();
    });
  }


  //ondomready
  $(function () {
    listFilter($("#header"), $("#list"));
  });
}(jQuery));

Open in new window



It searches each letter from all the displaying <li> tags and then displays the matches.

what i want it to do is make the list items hidden then only show the matches once the user has started typing.

here is it in action:

Any advice is appreciated,

richard
0
Comment
Question by:Richiep86
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 37823142
(function ($) {
  // custom css expression for a case-insensitive contains()
  jQuery.expr[':'].Contains = function(a,i,m){
      return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
  };


  function listFilter(header, list) { // header is any element, list is an unordered list
    // create and add the filter form to the header
    var form = $("<form>").attr({"class":"filterform","action":"#"}),
        input = $("<input>").attr({"class":"filterinput","type":"text"});
    $(form).append(input).appendTo(header);
    
    $(list).find("li").css({'display':'none'});
    $(input)
      .change( function () {
        var filter = $(this).val();
        if(filter.length > 0) {
          // this finds all links in a list that contain the input,
          // and hide the ones not containing the input while showing the ones that do
          $(list).find("a:not(:Contains(" + filter + "))").parent().slideUp().css({'display':'none'});
          $(list).find("a:Contains(" + filter + ")").parent().slideDown().css({'display':'block'});
        } else {
          $(list).find("li").css({'display':'none'});
        }
        return false;
      })
    .keyup( function () {
        // fire the above change event after every letter
        $(this).change();
    });
  }


  //ondomready
  $(function () {
    listFilter($("#header"), $("#list"));
  });
}(jQuery));

Open in new window

0
 

Author Closing Comment

by:Richiep86
ID: 37823185
Exactly what i needed - thanks!
0
 

Author Comment

by:Richiep86
ID: 37823378
How easy would it be to add a litte message if it didnt find anything?

not too fond of a javascript alert however...

Would i need to include another 'default' li which would only display if no matches were found?

Thanks

Richard
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37823681
Easy enough.

Add this to css:

    .noMatch {
        color:#999;
        font-style:italic
    }

Add this to the list:

<li class="noMatch">no match found</li>

Code:
(function ($) {
  // custom css expression for a case-insensitive contains()
  jQuery.expr[':'].Contains = function(a,i,m){
      return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
  };


  function listFilter(header, list) { // header is any element, list is an unordered list
    // create and add the filter form to the header
    var form = $("<form>").attr({"class":"filterform","action":"#"}),
        input = $("<input>").attr({"class":"filterinput","type":"text"});
    $(form).append(input).appendTo(header);
    
    $(list).find("li").css({'display':'none'});
    $(input)
      .change( function () {
        var filter = $(this).val();
        if(filter.length > 0) {
          // this finds all links in a list that contain the input,
          // and hide the ones not containing the input while showing the ones that do
          $(list).find("li a:not(:Contains(" + filter + "))").parent().slideUp().css({'display':'none'});
          $(list).find("li a:Contains(" + filter + ")").parent().slideDown().css({'display':'block'});
          $('.noMatch').css({'display':'none'});       
          $blocks = $('#list li').filter(function(){return $(this).css('display') == 'block';}).length;
          if($blocks == 0){$('.noMatch').css({'display':'block'});}
        } else {
          $(list).find("li").css({'display':'none'});
        } 
        return false;
      })
    .keyup( function () {
        // fire the above change event after every letter
        $(this).change();
    });
  }


  //ondomready
  $(function () {
    listFilter($("#header"), $("#list"));
  });
}(jQuery));

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question