Print Friendly

# Class YAHOO.ext.grid.DefaultDataModel

Package: | YAHOO.ext.grid
Class: | DefaultDataModel
Extends: | AbstractDataModel
Subclasses: | LoadableDataModel
Defined In: | DefaultDataModel.js

This is the default implementation of a DataModel used by the Grid. It works with multi-dimensional array based data. Using the event system in the base class YAHOO.ext.grid.AbstractDataModel, all updates to this DataModel are automatically reflected in the user interface.
Usage:

    var myData = [
    	["MSFT","Microsoft Corporation", "314,571.156", "32,187.000", "55000"],
    	["ORCL", "Oracle Corporation", "62,615.266", "9,519.000", "40650"]
    ];
    var dataModel = new YAHOO.ext.grid.DefaultDataModel(myData);

## Public Properties

Property | Defined By
--- | ---
| sortTypes<static> : Object | DefaultDataModel
<static> Alias to YAHOO.ext.grid.DefaultColumnModel.sortTypes

## Public Methods

Method | Defined By
--- | ---
| DefaultDataModel(`Array data`) | DefaultDataModel
| addListener(`String eventName`, `Function handler`, [`Object scope`], [`boolean override`]) : void | Observable
Appends an event handler to this element
| addRow(`Array cellValues`) : Number | DefaultDataModel
Adds a row to the dataset.
| applySort() : void | DefaultDataModel
Applies the last used sort to the current data.
| delayedListener(`String eventName`, `Function handler`, [`Object scope`], [`Number delay`]) : Function | Observable
Appends an event handler to this element that is delayed the specified number of milliseconds.
| each(`Function fn`, [`Object scope`]) : void | DefaultDataModel
Calls passed function with each rows data - if the function returns false it stops.
| filter(`Object query`) : Number | DefaultDataModel
Filter the DataModel rows by the query defined in spec, see query for more details on the query spec.
| fireEvent(`String eventName`, `Object... args`) : Boolean | Observable
Fires the specified event with the passed parameters (minus the event name).
| getRow() : Array | DefaultDataModel
Returns the column data for the specified row.
| getRowCount() : Number | DefaultDataModel
Returns the number of rows in the dataset
| getRowId() : Number | DefaultDataModel
Returns the ID of the specified row. By default it return the value of the first column. Override to provide more ad...
| getRows(`Array indexes`) : Array | DefaultDataModel
Returns the column data for the specified rows as a multi-dimensional array: rows[3][0] would give you the value of ...
| getValueAt(`Number rowIndex`, `Number colIndex`) : Object | DefaultDataModel
Returns the value at the specified data position
| insertRow(`Number index`, `Array cellValues`) : Number | DefaultDataModel
Inserts a row a the specified location in the dataset.
| purgeListeners() : void | Observable
Removes all listeners for this object
| query(`Object spec`, `Boolean returnUnmatched`) : Array | DefaultDataModel
Query the DataModel rows by the filters defined in spec, for example... // column 1 starts with Jack, column 2 filte...
| removeListener(`String eventName`, `Function handler`, [`Object scope`]) : void | Observable
Removes a listener
| removeRow(`Number index`) : void | DefaultDataModel
Remove a row.
| setDefaultSort(`Function/Object sortInfo`, `Number columnIndex`, `String direction`) : void | DefaultDataModel
Sets the default sort info. Note: this function does not actually apply the sort.
| setValueAt(`Object value`, `Number rowIndex`, `Number colIndex`) : void | DefaultDataModel
Sets the specified value at the specified data position
| sort(`Function/Object sortInfo`, `Number columnIndex`, `String direction`) : void | DefaultDataModel
Sorts the data by the specified column - Uses the sortType specified for the column in the passed columnModel.

## Public Events

Event | Defined By
--- | ---
| cellupdated : (`DataModel this`, `Number rowIndex`, `Number columnIndex`) | AbstractDataModel
Fires when a cell is updated
| datachanged : (`DataModel this`) | AbstractDataModel
Fires when the entire data structure has changed
| rowsdeleted : (`DataModel this`, `Number firstRowIndex`, `Number lastRowIndex`) | AbstractDataModel
Fires when a range of rows have been deleted
| rowsinserted : (`DataModel this`, `Number firstRowIndex`, `Number lastRowIndex`) | AbstractDataModel
Fires when a range of rows have been inserted
| rowssorted : (`DataModel this`) | AbstractDataModel
Fires when the data has been sorted
| rowsupdated : (`DataModel this`, `Number firstRowIndex`, `Number lastRowIndex`) | AbstractDataModel
Fires when a range of rows have been updated

## Property Details

### sortTypes<static>

`public Object sortTypes<static>`
<static> Alias to YAHOO.ext.grid.DefaultColumnModel.sortTypes
This property is defined by DefaultDataModel.

## Constructor Details

### DefaultDataModel

`public function DefaultDataModel(`Array data`)`
Parameters:

- `data` : Array

## Method Details

### addListener

`public function addListener(`String eventName`, `Function handler`, [`Object scope`], [`boolean override`])`
Appends an event handler to this element
Parameters:

- `eventName` : String
  The type of event to listen for
- `handler` : Function
  The method the event invokes
- `scope` : Object
  (optional) The scope (this object) for the handler
- `override` : boolean
  (optional) If true, scope becomes the scope

Returns:

- `void`

This method is defined by Observable.

### addRow

`public function addRow(`Array cellValues`)`
Adds a row to the dataset.
Parameters:

- `cellValues` : Array
  The array of values for the new row

Returns:

- `Number`
  The index of the added row

This method is defined by DefaultDataModel.

### applySort

`public function applySort()`
Applies the last used sort to the current data.
Parameters:

- None.

Returns:

- `void`

This method is defined by DefaultDataModel.

### delayedListener

`public function delayedListener(`String eventName`, `Function handler`, [`Object scope`], [`Number delay`])`
Appends an event handler to this element that is delayed the specified number of milliseconds.
Parameters:

- `eventName` : String
  The type of event to listen for
- `handler` : Function
  The method the event invokes
- `scope` : Object
  (optional) The scope (this object) for the handler
- `delay` : Number
  (optional) The number of milliseconds to delay

Returns:

- `Function`
  The wrapped function that was created (can be used to remove the listener)

This method is defined by Observable.

### each

`public function each(`Function fn`, [`Object scope`])`
Calls passed function with each rows data - if the function returns false it stops.
Parameters:

- `fn` : Function
- `scope` : Object
  (optional)

Returns:

- `void`

This method is defined by DefaultDataModel.

### filter

`public function filter(`Object query`)`
Filter the DataModel rows by the query defined in spec, see query for more details on the query spec.
Parameters:

- `query` : Object
  The query spec query

Returns:

- `Number`
  The number of rows removed

This method is defined by DefaultDataModel.

### fireEvent

`public function fireEvent(`String eventName`, `Object... args`)`
Fires the specified event with the passed parameters (minus the event name).
Parameters:

- `eventName` : String
- `args` : Object...
  Variable number of parameters are passed to handlers

Returns:

- `Boolean`
  returns false if any of the handlers return false otherwise it returns true

This method is defined by Observable.

### getRow

`public function getRow()`
Returns the column data for the specified row.
Parameters:

- None.

Returns:

- `Array`

This method is defined by DefaultDataModel.

### getRowCount

`public function getRowCount()`
Returns the number of rows in the dataset
Parameters:

- None.

Returns:

- `Number`

This method is defined by DefaultDataModel.

### getRowId

`public function getRowId()`
Returns the ID of the specified row. By default it return the value of the first column. Override to provide more advanced ID handling.
Parameters:

- None.

Returns:

- `Number`

This method is defined by DefaultDataModel.

### getRows

`public function getRows(`Array indexes`)`
Returns the column data for the specified rows as a multi-dimensional array: rows[3][0] would give you the value of row 4, column 0.
Parameters:

- `indexes` : Array
  The row indexes to fetch

Returns:

- `Array`

This method is defined by DefaultDataModel.

### getValueAt

`public function getValueAt(`Number rowIndex`, `Number colIndex`)`
Returns the value at the specified data position
Parameters:

- `rowIndex` : Number
- `colIndex` : Number

Returns:

- `Object`

This method is defined by DefaultDataModel.

### insertRow

`public function insertRow(`Number index`, `Array cellValues`)`
Inserts a row a the specified location in the dataset.
Parameters:

- `index` : Number
  The index where the row should be inserted
- `cellValues` : Array
  The array of values for the new row

Returns:

- `Number`
  The index the row was inserted in

This method is defined by DefaultDataModel.

### purgeListeners

`public function purgeListeners()`
Removes all listeners for this object
Parameters:

- None.

Returns:

- `void`

This method is defined by Observable.

### query

`public function query(`Object spec`, `Boolean returnUnmatched`)`
Query the DataModel rows by the filters defined in spec, for example...

    // column 1 starts with Jack, column 2 filtered by myFcn, column 3 equals 'Fred'
    dataModel.filter({1: /^Jack.+/i}, 2: myFcn, 3: 'Fred'});

Parameters:

- `spec` : Object
  The spec is generally an object literal consisting of column index and filter type. The filter type can be a string/number (exact match), a regular expression to test using String.search() or a function to call. If it's a function, it will be called with the value for the specified column and an array of the all column values for that row: yourFcn(value, columnData). If it returns anything other than true, the row is not a match. If you have modified Object.prototype this method may fail.
- `returnUnmatched` : Boolean
  True to return rows which don't match the query instead of rows that do match

Returns:

- `Array`
  An array of row indexes that match

This method is defined by DefaultDataModel.

### removeListener

`public function removeListener(`String eventName`, `Function handler`, [`Object scope`])`
Removes a listener
Parameters:

- `eventName` : String
  The type of event to listen for
- `handler` : Function
  The handler to remove
- `scope` : Object
  (optional) The scope (this object) for the handler

Returns:

- `void`

This method is defined by Observable.

### removeRow

`public function removeRow(`Number index`)`
Remove a row.
Parameters:

- `index` : Number

Returns:

- `void`

This method is defined by DefaultDataModel.

### setDefaultSort

`public function setDefaultSort(`Function/Object sortInfo`, `Number columnIndex`, `String direction`)`
Sets the default sort info. Note: this function does not actually apply the sort.
Parameters:

- `sortInfo` : Function/Object
  A sort comparison function or null to use the default or A object that has a method getSortType(index) that returns a function like a grid column model.
- `columnIndex` : Number
  The column index to sort by
- `direction` : String
  The direction of the sort ('DESC' or 'ASC')

Returns:

- `void`

This method is defined by DefaultDataModel.

### setValueAt

`public function setValueAt(`Object value`, `Number rowIndex`, `Number colIndex`)`
Sets the specified value at the specified data position
Parameters:

- `value` : Object
  The new value
- `rowIndex` : Number
- `colIndex` : Number

Returns:

- `void`

This method is defined by DefaultDataModel.

### sort

`public function sort(`Function/Object sortInfo`, `Number columnIndex`, `String direction`)`
Sorts the data by the specified column - Uses the sortType specified for the column in the passed columnModel.
Parameters:

- `sortInfo` : Function/Object
  A sort comparison function or null to use the default or A object that has a method getSortType(index) that returns a function like a grid column model.
- `columnIndex` : Number
  The column index to sort by
- `direction` : String
  The direction of the sort ('DESC' or 'ASC')

Returns:

- `void`

This method is defined by DefaultDataModel.

## Event Details

### cellupdated

`public event cellupdated`
Fires when a cell is updated
Subscribers will be called with the following parameters:

- `this` : DataModel
- `rowIndex` : Number
- `columnIndex` : Number

This event is defined by AbstractDataModel.

### datachanged

`public event datachanged`
Fires when the entire data structure has changed
Subscribers will be called with the following parameters:

- `this` : DataModel

This event is defined by AbstractDataModel.

### rowsdeleted

`public event rowsdeleted`
Fires when a range of rows have been deleted
Subscribers will be called with the following parameters:

- `this` : DataModel
- `firstRowIndex` : Number
- `lastRowIndex` : Number

This event is defined by AbstractDataModel.

### rowsinserted

`public event rowsinserted`
Fires when a range of rows have been inserted
Subscribers will be called with the following parameters:

- `this` : DataModel
- `firstRowIndex` : Number
- `lastRowIndex` : Number

This event is defined by AbstractDataModel.

### rowssorted

`public event rowssorted`
Fires when the data has been sorted
Subscribers will be called with the following parameters:

- `this` : DataModel

This event is defined by AbstractDataModel.

### rowsupdated

`public event rowsupdated`
Fires when a range of rows have been updated
Subscribers will be called with the following parameters:

- `this` : DataModel
- `firstRowIndex` : Number
- `lastRowIndex` : Number

This event is defined by AbstractDataModel.

Copyright © 2006 Jack Slocum. All rights reserved.