Solved

Filter tree control by typing in a textbox using Jquery

Posted on 2013-02-07
6
731 Views
Last Modified: 2013-02-08
I have a textbox and a tree control on a page that I need to filter the contents of the tree as I type in the textbox. My tree control has 2 levels to it. The root level is alpha type characters and the child level is numeric based on the following...
<ul class="BNG">
     <li class="BNH">Aiken
          <ul class="BNG">
               <li class="BNH">1550
               <li class="BNH">7456
               <li class="BNH">4589
          </ul>
     </li>
</ul>

This is a short grouping. The tree has many parent and child nodes. It is possible that there might be a numeric character in the root node and also possible to have a alpha character in the child node. I need to have a Jquery code filter the child and root nodes as I type in the textbox. I have been trying to use the .keyup function of the textbox for this. If I type "15" in the textbox I need the root node Aiken to be visible and the child node 1550 to be visible. If I type "Aik" in the textbox, I need the root node Aiken to be visible as well as all the child nodes for Aiken (1550, 7456, 4589). My java and jquery is beginner level and would appreciate any help on this. Thanks!
0
Comment
Question by:Cubbybulin
  • 3
  • 3
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38867503
Test page : http://jsfiddle.net/xwkQ5/
$(document).ready(function() {
    $("#textbox").bind("keyup change blur paste", function() {
        var term = $(this).val();
        $("ul,li", ".BNG").filter(function() { return $(this).text().indexOf(term)>=0; }).show()
        $("ul,li", ".BNG").filter(function() { return $(this).text().indexOf(term)<0; }).hide()
    })
})

Open in new window

0
 

Author Comment

by:Cubbybulin
ID: 38867577
Thank you for your reply! I tested this and it partially works. When I type "15" in the text box it does show the Aiken root node and the 1550 child node. Which is perfect. However, when I type "Aik" it only shows the Aiken root node and not all of the child nodes associated with Aiken. Is this doable?
One other thing is that it is case sensitive. If I type "a", Aiken does not show up unless I type "A". Is this doable not to have it case sensitive?

Thanks!
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 38867636
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Cubbybulin
ID: 38868300
This works great, except it is very, very slow in executing. Any ideas on how to speed it up or is just a factor of page contenet?
There are allot of other controls on my page.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38868320
not really
maybe use the server with or without ajax to do the filtering
0
 

Author Comment

by:Cubbybulin
ID: 38868373
ok, thanks for your help! code is exactly what I was after!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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…

821 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