Login Register

dojox.data.CsvStore

CsvStore is a simple read-only store provided by Dojo and contained in the DojoX project. CsvStore is a read interface that works with CSV formated data files. The CSV file format is commonly known to folks who work regularly with spread sheet data. Like ItemFileReadStore, CsvStore reads the contents from an http endpoint or a JavaScript Data object that contains CSV formatted data. The following is an example of a CSV data source:

Title, Year, Producer
City of God, 2002, Katia Lund
Rain,, Christine Jeffs
2001: A Space Odyssey, , Stanley Kubrick
"This is a ""fake"" movie title", 1957, Sidney Lumet
Alien, 1979   , Ridley Scott
"The Sequel to ""Dances With Wolves.""", 1982, Ridley Scott
"Caine Mutiny, The", 1954, "Dymtryk ""the King"", Edward"

Note that in the above data, the first row is always assumed to be the column names. Those are what get assigned as the attribute names for the CSV items. Each row in the CSV data is treated as a single item.

The following dojo.data APIs are implemented by CsvStore

  • dojo.data.api.Read
  • dojo.data.api.Identity

Constructor Parameters

The constructor for CsvStore takes three possible parameters in its keyword arguments as defined in the following list:

url
The URL from which to load the CSV data. This is optional.
data
The JavaScript object which represents the stores contents as defined by the structure in the previous example. This is optional.
label
A string that identifies which column to treat as the human-readable label. It must match one of the column labels in the file for it to be effective.

Query Syntax:

The fetch method query syntax for CsvStore is simple and straightforward. It allows a list of attributes to match against in an AND fashion, just like ItemFileReadStore. For example, a query object that looks like the following example will locate all items that have attributes of those names that match both of those values:

{ foo:"bar", bit:"bite"}

Note that CsvStore supports the use of wild cards (multi-character * and single character ?) in its attribute value matching.

Examples

To find all items with attribute foo that start with bar, the query would be:

{ foo:"bar*"}

To find all items with attribute foo the value of which ends with ar and ignoring only the first character, the query would be:

{ foo:"?ar"}

NOTE: Other stores should follow the same semantics in how it defines its queries for consistency.

Usage Examples

Assume a data source as defined by the example data format in this page.

Example 1: Query for all movies by producer Ridley Scott

var store = new dojox.data.CsvStore({url: "movies.csv", label: "Title"});
var gotMovies = function(items, request){
    for (var i = 0; i < items.length; i++){
       var item = items[i];
       console.log("Located movie: " + store.getLabel(item);
    }
}
var request = store.fetch({query: {Producer:"Ridley Scott"}, onComplete: gotMovies});

Example 2: Query for titles that that start with A, case insensitively

var store = new dojo.data.ItemFileReadStore({url: "movies.csv", label: "Title"});
var gotTitles= function(items, request){
    for (var i = 0; i < items.length; i++){
       var item = items[i];
       console.log("Located name that started with A: " + store.getLabel(item);
    }
}
var request = store.fetch({query: {Title:"A*"}, queryOptions: {ignoreCase: true}, onComplete: gotTitles});

Further Examples

For further examples refer to the test cases provided in dojox.data.tests.