Recent Weblogs

Links I like

Client Side Filter

A feature I've becoming increasingly familiar with is Client Side filter. With prototype.js collections are king and it definitely makes them easier to manage.

Build your model

One thing I've also learned is that if you can help it, represent the total data set in regular HTML markup. The downfall of creating them via JS Arrays is that if the user does not have Javascript enabled they will see nothing. We're hoping to enrich the experience, not destroy it. Once the page loads then use JS to traverse over your collections and stash their own references to them.

The Big Idea

The scenario set up below is to filter one select box based off of the selection of another. In this example the subject box contains two options Fruit and Vegetables, the second select box contains 6 values, 3 Fruits and 3 Vegetables. On change of the subject box I want the observing box to only represent values that have the appropriate category.

The Code

/**
 * author : Matthew Foster
 * @date : July 10th 2007
 */
        var SelectFilter = Class.create();
        
        Object.extend(SelectFilter.prototype,
                        {
                            
                            initialize : function(subject, observer){
                                
                                this.subject = $(subject);
                                this.observer = $(observer);
                                
                                this.changeHandle = this.handleChange.bindAsEventListener(this);
                                
                                Event.observe(this.subject, "change", this.changeHandle);
                                
                                this.cacheOptions(this.observer);
                                
                            },
                            cacheOptions : function(selectBox){
                                
                                this.optionCache = $A(selectBox.getElementsByTagName("option"));
                            
                            },
                            handleChange : function(e){
                                
                                var filter = this.subject.value;
                                
                                this.optionCache.each(this.filterObserver.bind(this, filter));
                            
                            },
                            filterObserver : function(val, ele){
                                
                                if(ele.getAttribute("category") == val)
                                    this.observer.appendChild(ele);
                                else
                                    try{
                                        this.observer.removeChild(ele);
                                    }
                                    catch(e){
                                        //shh
                                    }
                                    
                            
                            }
                            
                         
                        }
                    );
	
					
					

Still reading?

Good because now that we've taken a look at the code I can talk more about it.

The Filter

In the handleChange method we extract the value that we want to filter against, we iterate over the optionCache, being an array that we created at page load so we know it contains the entire data set. We use bind to A) Keep reference to "this" so we can reference the observe select box. B) So we can attach the filter value to the argument scope. In the actual invokation of filterObserver the filter value comes through first as it was prebound the argument scope in the function closure created by the .bind() call. Next is the element that the iterator sends us. Now we have all the data to take action, if the elements category property matches the filter value then append it, if not then remove it from the select box.

Conclusion

This is a simple example and written with a class that is very implementation specific. The point of this blog was to demonstrate an easy example of how to do some client side filtering and implement with best practices.

Comments

September 18, 2008Guncel driver download sitesi - actual driver download site

Thanks

October 29, 2008figurin

Thank you very much for this information.
Good post thanks for sharing.
I like this site ;)

December 22, 2008nostalji filmler

Thank you very much for this information.
Good post thanks for sharing.
I like this site ;)

December 22, 2008Gazeteler

Good post thanks for sharing.

December 30, 2008savaş oyunu

thanks admin good job

January 13, 2009Bovik Laan Penge Online

Client side spam filters are used with your mail client rather than working on the mail server. They are generally a less effective means of filtering spam on a large scale, but are useful if your ISP either cannot filter all of their spam or refuse to. Some client side filters will work with any mail client whereas others are built as plugins for a particular program.

January 24, 2009Web Design Leicester

For any client side action we have to choose a scripting language to build our logic. Let us go for JavaScript. When use a file control we have to use a button control for doing the uploading action. It is better to refer to Anand’s article CodeSnip: Working with FileUpload Control to get the basic idea of using FileUpload control.

Name
Site
Comment
  CAPTCHA Image
Reload Captcha Image
Captcha