Solved

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

Posted on 2012-04-09
4
546 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now