| Database II: A Database Object along the way of JavaScript |
In previous discussions of client-side database programming in this tutorial, we've used 'arrays of objects' to hold the data. Here, the oxherder has created a Database Object, just like the objects supplied with JavaScript. In the Database Object (called here jBaseJr, because it's going to grow in the next supplement), the data is stored in an 'array of arrays' - accessed by the property record[fieldNumber][recordNumber].
The table below illustrates the three major methods of this implentation of jBaseJr - using a simple database with three fields: Name, Phone, and EMail:
- sortRecords() allows you to do an ascending or descending sort on any field, either case sensitive or insensitive.
- findRecords() lets you select a data subset, searching any field, case sensitive or insensitive, for a string, exact or anywhere in the field.
- showRecords() displays the dataset as a glossary or in a table. Try it out before looking at the code.
Here's a formal description of the jBaseJr object:
- Object:
- jBaseJr(field0, field1, ... , fieldn-1) Where fieldx is a list of field names.
- Properties:
- fields The number of fields.
field[i] An array of field names. [i=0 to fields-1]
records The number of records.
record[i][j] An array of the data's records. [i=0 to fields-1][j=0 to records-1]
order[j] The sort index (record numbers). [j=0 to records-1]
dataSet[j] The find index (true/false). [j=0 to records-1]
- Methods:
-
- addRecord(data0, data1, ... , datan-1)
- datax A list of the data elements for the new record.
- sortRecords(field, order, case)
- field The number of a field to sort.
order The sort order. [0=natural][1=ascending][2=descending]
case Is the sort case sensitive? [true][false]
- findRecords(field, string, method, case)
- field The number of the field to search.
string The search string.
method The search method. ['exact'=exact match]['all'=anywhere in the field]
case Is the sort case sensitive? [true][false]
- showRecords(style, field0, field1, ..., fieldn-1)
- style The report style. ['table']['glossary']
fieldx An optional list of the fields to display (in order).
myDatabase=new jBaseJr('Name', 'Phone', 'Email');
myDatabase.addRecord('John', '406-724-8987', 'jsmith@gsu.edu');
myDatabase.addRecord('Mary', '704-654-5887', 'mfox@memory.com');
myDatabase.addRecord('Abe', '584-264-4862', 'alincoln@illinois.net');
myDatabase.addRecord('Steve', '306-529-5491', 'slevi@magnum.com');
myDatabase.addRecord('Jane', '376-748-8793', 'jeyre@heath.net');
|
Using jBaseJr is pretty simple. Just include the code for the Database Object, the data, and instantiate jBaseJr. Then add whatever functions you need to manipulate the information, using the jBaseJr methods.
One new thing in the code is the use of multidimensional arrays. The technique is self explanatory (see the jBaseJr() function). Notice the use of arguments[ ] and arguments.length - utilizing the handy JavaScript array of the arguments passed to a function.
Note: This section has illustrated jBaseJr as it was originally constructed. If you're interested in using it, study this code carefully first. Then look at the next section for a more complex (and much more versatile) version (called jBase).