O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

21.3. Filtering Recordsets

Problem

You want to create a new recordset that contains a subset of the original.

Solution

Use the RecordSet.filter( ) method.

Discussion

You can manually create a new RecordSet object by reading the records from a recordset and writing selected ones to a new recordset. However, this is unnecessarily laborious. Instead, you should use the filter( ) method, which handles almost all of the processing for you. Simply provide a filter function that contains the logic to decide which records to use. The filter( ) method expects a reference to a filter function as a parameter, and it accepts a second parameter that is automatically passed to the filter function. The second parameter lets you filter records based on different criteria. The filter( ) method returns a new RecordSet object.

filteredRs = rs.filter(filterFunction, context);

The filter function is called once for each record. Each time it is called, it is passed the next record object, and if a context parameter was specified in the filter( ) method call, that value is also passed along to the filter function. Within the filter function you should place the logic to determine whether the record should be included in the filtered recordset. The optional context parameter, if used, lets the filter function filter based on different criteria. If the filter function returns true, then the record is included in the filtered recordset; if it returns false, the record is not included.

function filterFunction (record, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required