Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-09
4
Medium Priority
?
558 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
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)

688 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