YUI 3.x Home -

YUI Library Examples: Recordset: Recordset Filter Plugin

Recordset: Recordset Filter Plugin

The RecordsetFilter plugin provides the ability to filter records within a specific recordset instance.
State Population Land Area

The filter plugin allows operations to filter a recordset instance. All the methods generate new recordsets that are subsets of the original recordset.

Using the Plugin

The RecordsetFilter plugin supports filtering a recordset instance using a custom filter function, or key/value pairs.

  1. YUI().use("recordset-base", "recordset-filter", function(Y) {
  2. var state_census_data = [
  3. {ANSI: "01000", STATE: "ALABAMA", TOTAL_POP: 4708708, LAND_AREA: 50744, POP_PER_SQ_MILE: 87.6},
  4. {ANSI: "06000", STATE: "CALIFORNIA", TOTAL_POP: 36961664, LAND_AREA: 155959.34, POP_PER_SQ_MILE: 217.2},
  5. {ANSI: "10000", STATE: "DELAWARE", TOTAL_POP: 885122, LAND_AREA: 1953.56, POP_PER_SQ_MILE: 401},
  6.  
  7. //instantiate a new Recordset
  8. recordset = new Y.Recordset({records: state_census_data});
  9.  
  10. //Plug it with the Filter plugin
  11. recordset.plug(Y.Plugin.RecordsetFilter);
  12.  
  13. //call filter methods with the 'filter' namespace
  14. var subset = recordset.filter.filter("STATE", "CALIFORNIA");
  15. });
YUI().use("recordset-base", "recordset-filter", function(Y) {
    var state_census_data = [
        {ANSI: "01000", STATE: "ALABAMA", TOTAL_POP: 4708708, LAND_AREA: 50744, POP_PER_SQ_MILE: 87.6},
        {ANSI: "06000", STATE: "CALIFORNIA", TOTAL_POP: 36961664, LAND_AREA: 155959.34, POP_PER_SQ_MILE: 217.2},
        {ANSI: "10000", STATE: "DELAWARE", TOTAL_POP: 885122, LAND_AREA: 1953.56, POP_PER_SQ_MILE: 401},
 
	//instantiate a new Recordset
	recordset = new Y.Recordset({records: state_census_data});
 
	//Plug it with the Filter plugin
	recordset.plug(Y.Plugin.RecordsetFilter);
 
	//call filter methods with the 'filter' namespace
	var subset = recordset.filter.filter("STATE", "CALIFORNIA");
});

Filtering by Key/Value

The filter() method can take a key as its first argument, and a value as its second. Doing so will return a subset of the original recordset, with records that match the specific key/value pair.

  1. //call filter methods with the 'filter' namespace
  2. var subset = recordset.filter.filter("STATE", "CALIFORNIA");
//call filter methods with the 'filter' namespace
var subset = recordset.filter.filter("STATE", "CALIFORNIA");

Filtering by Custom Function

Additionally, a custom function can be passed in to the filter method. In this scenario, all records will be evaluated against this function, and if a truthy value is returned, it will be pushed onto the returning sub-recordset.

  1. var myCustomFilterFunction = function(item) {
  2. var letter = item.getValue('STATE').charAt(0);
  3. if (letter === 'M' || letter === 'N' || letter === 'O' || letter === 'P') {
  4. return true;
  5. }
  6. else {
  7. return false;
  8. }
  9. };
  10.  
  11. //All records which have a STATE property starting with "M", "N", "O", or "P" will be pushed onto the new recordset.
  12. var subset = recordset.filter.filter(myCustomFilterFunction);
var myCustomFilterFunction = function(item) {
	var letter = item.getValue('STATE').charAt(0);
	if (letter === 'M' || letter === 'N' || letter === 'O' || letter === 'P') {
		return true;
	}
	else {
		return false;
	}
};
 
//All records which have a STATE property starting with "M", "N", "O", or "P" will be pushed onto the new recordset.
var subset = recordset.filter.filter(myCustomFilterFunction);

Other Ways to Filter

As Recordset augments Y.Arraylist, methods from the array-extras sub-module can be used. Currently, the RecordsetFilter plugin supports reject() and grep(). However, using other methods from the array-extras utility is straight-forward:
  1.  
  2. //use the 'array-extras' submodule
  3. YUI().use("recordset-base", "recordset-filter", "array-extras", function(Y) {
  4.  
  5. var state_census_data = [
  6. {ANSI: "01000", STATE: "ALABAMA", TOTAL_POP: 4708708, LAND_AREA: 50744, POP_PER_SQ_MILE: 87.6},
  7. {ANSI: "06000", STATE: "CALIFORNIA", TOTAL_POP: 36961664, LAND_AREA: 155959.34, POP_PER_SQ_MILE: 217.2},
  8. {ANSI: "10000", STATE: "DELAWARE", TOTAL_POP: 885122, LAND_AREA: 1953.56, POP_PER_SQ_MILE: 401}
  9. ],
  10. //instantiate a new Recordset
  11. recordset = new Y.Recordset({records: state_census_data});
  12.  
  13. //Define a custom function
  14. customFindFunction = function() {
  15. ...
  16. }
  17.  
  18. //Use the Array.find method found in the array-extras sub-module
  19. var subset = new Y.Recordset({
  20. records: Y.Array.find(recordset.get('records'), customFindFunction)
  21. });
  22. });
  23.  
 
//use the 'array-extras' submodule
YUI().use("recordset-base", "recordset-filter", "array-extras", function(Y) {
 
    var state_census_data = [
        {ANSI: "01000", STATE: "ALABAMA", TOTAL_POP: 4708708, LAND_AREA: 50744, POP_PER_SQ_MILE: 87.6},
        {ANSI: "06000", STATE: "CALIFORNIA", TOTAL_POP: 36961664, LAND_AREA: 155959.34, POP_PER_SQ_MILE: 217.2},
        {ANSI: "10000", STATE: "DELAWARE", TOTAL_POP: 885122, LAND_AREA: 1953.56, POP_PER_SQ_MILE: 401}
	],
	//instantiate a new Recordset
	recordset = new Y.Recordset({records: state_census_data});
 
	//Define a custom function
	customFindFunction = function() {
		...
	}
 
	//Use the Array.find method found in the array-extras sub-module
	var subset = new Y.Recordset({
	       records: Y.Array.find(recordset.get('records'), customFindFunction)
	});
});
 

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings