views:

61

answers:

1

Can I filter the rows of a static dataset using multiple drop-down menus and a paginated YUI datatable ?

http://www.mappingbahia.org/project/iguape_dataset.html

A: 

Each YAHOO.widget component such as YUI DataTable uses a YUI DataSource component which provides data needed To populate each rendered YAHOO.widget component. Bellow you can see how it works

YUI datasource

Notice each YAHOO.widget component internally makes a call To The underlying YUI datasource Through sendRequest method (See step 1). Now let's see sendRequest signature

sendRequest(request, callback)
  • request

For remote data, this request may be a param/value string, such as "id=123&name=foo". For local data, this request maybe a simpler value such as 123. Specifying parameters may be irrelevent, so this value may be simply be null

  • callback

It is just an JavaScript object which can be described as follows (Notice each property)

var callback = {
    success:function(request, response, payload) {
        /*
         * successful request is performed by success property
         */
    },
    failure:function(request, response, payload) {
        /*
         * failure request is performed by failure property
         */
    },
    scope:null,
    argument:null
}

So when each YAHOO.widget component makes a call To The internally YUI datasource Through sendRequest method, It pass a built-in callback object as shown above which Takes care of rendering The YAHOO.widget component itself. So if you want a custom behavior, you need To make a call To The underlying YUI datasource and pass your custom callback object To filter The data provided by The YUI datasource as follows

var datatable = // YUI datatable settings goes here

To attach each change event To each select, you can use

YAHOO.util.Event.addListener("sex", "change", function(e) {
    var value = e.getTarget(e).value;

    if(YAHOO.lang.isValue(value)) {
        /**
          * Notice i am retrieving The underlying datasource To make a call To sendRequest method
          */
        datatable.getDataSource().sendRequest(null, {
            success:function(request, response, payload) {
                /**
                  * because scope property (see bellow) refers To The datatable
                  * this keyword can be used To get a reference To The datatable
                  */

                /**
                  * initializeTable method clear any data stored by The datatable
                  */
                this.initializeTable();

                var rs = response.results;
                var filtered = [];
                for(var i = 0; i < rs.length; i++) {
                    /**
                      * Is The sex property equal To The value selected by The user ?
                      */ 
                    if(rs[i]["sex"] == value) {
                        filtered[filtered.length] = rs[i];
                    }
                }

                this.getRecordSet().setRecords(filtered, 0);

                // Update UI
                this.render();
            },
            scope:datatable,
            argument:null
        });
    }
});

Althoug not Tested, i am pretty sure it will work fine.

Arthur Ronald F D Garcia