webgui/www/extras/yui-ext/docs/overview-summary-DefaultDataModel.js.html
JT Smith 4f68a0933c added YUI and YUI-ext
fixed the resizable text area with IE problem
fixed the ad space with IE problem
merged the 7.2.0 and 7.1.4 change logs
2006-11-07 23:15:57 +00:00

369 lines
17 KiB
HTML

<!doctype html public "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
<html>
<head>
<title>
Overview
</title>
<link rel ="stylesheet" type="text/css" href="stylesheet.css" title="Style">
<script>
function asd() {
parent.document.title="DefaultDataModel.js Overview";
}
</script>
</head>
<body bgcolor="white" onload="asd();" style="margin:15px;">
<center>
<h2>DefaultDataModel.js</h2>
</center>
<h4>Summary</h4>
<p>
</p>
<table border="1" cellpadding="3" cellspacing="0" width="100%">
<tr bgcolor="#CCCCFF" class="TableHeadingColor">
<td colspan="2" class="title-cell">
<b>Class Summary</b>
</td>
</tr>
<tr bgcolor="white" class="TableRowColor">
<td width="15%"><b><a href="YAHOO.ext.grid.DefaultDataModel.html">YAHOO.ext.grid.DefaultDataModel</a></b></td>
<td>This is the default implementation of a DataModel used by the Grid.</td>
</tr>
</table>
<hr/>
<!-- ========== METHOD SUMMARY =========== -->
<!-- ========== END METHOD SUMMARY =========== -->
<pre class="sourceview">
<span class="comment">/**
* <span class="attrib">@class</span>
* 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
* {<span class="attrib">@link</span> YAHOO.ext.grid.AbstractDataModel}, all updates to this DataModel are automatically
* reflected in the user interface.
* &lt;br&gt;Usage:&lt;br&gt;
* &lt;pre&gt;&lt;code&gt;
* 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);
* &lt;/code&gt;&lt;/pre&gt;
* <span class="attrib">@extends</span> YAHOO.ext.grid.AbstractDataModel
* <span class="attrib">@constructor</span>
*/</span>
YAHOO.ext.grid.DefaultDataModel = <span class="reserved">function</span>(data){
YAHOO.ext.grid.DefaultDataModel.superclass.constructor.call(<span class="reserved">this</span>);
<span class="comment">/**<span class="attrib">@private</span>*/</span>
<span class="reserved">this</span>.data = data;
};
YAHOO.extendX(YAHOO.ext.grid.DefaultDataModel, YAHOO.ext.grid.AbstractDataModel);
<span class="comment">/**
* Returns the number of rows in the dataset
* <span class="attrib">@return</span> {Number}
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.getRowCount = <span class="reserved">function</span>(){
<span class="reserved">return</span> <span class="reserved">this</span>.data.length;
};
<span class="comment">/**
* Returns the ID of the specified row. By default it return the value of the first column.
* Override to provide more advanced ID handling.
* <span class="attrib">@return</span> {Number}
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.getRowId = <span class="reserved">function</span>(rowIndex){
<span class="reserved">return</span> <span class="reserved">this</span>.data[rowIndex][0];
};
<span class="comment">/**
* Returns the column data for the specified row.
* <span class="attrib">@return</span> {Array}
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.getRow = <span class="reserved">function</span>(rowIndex){
<span class="reserved">return</span> <span class="reserved">this</span>.data[rowIndex];
};
<span class="comment">/**
* 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.
* <span class="attrib">@param</span> {Array} indexes The row indexes to fetch
* <span class="attrib">@return</span> {Array}
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.getRows = <span class="reserved">function</span>(indexes){
var data = <span class="reserved">this</span>.data;
var r = [];
<span class="reserved">for</span>(var i = 0; i &lt; indexes.length; i++){
r.push(data[indexes[i]]);
}
<span class="reserved">return</span> r;
};
<span class="comment">/**
* Returns the value at the specified data position
* <span class="attrib">@param</span> {Number} rowIndex
* <span class="attrib">@param</span> {Number} colIndex
* <span class="attrib">@return</span> {Object}
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.getValueAt = <span class="reserved">function</span>(rowIndex, colIndex){
<span class="reserved">return</span> <span class="reserved">this</span>.data[rowIndex][colIndex];
};
<span class="comment">/**
* Sets the specified value at the specified data position
* <span class="attrib">@param</span> {Object} value The new value
* <span class="attrib">@param</span> {Number} rowIndex
* <span class="attrib">@param</span> {Number} colIndex
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.setValueAt = <span class="reserved">function</span>(value, rowIndex, colIndex){
<span class="reserved">this</span>.data[rowIndex][colIndex] = value;
<span class="reserved">this</span>.fireCellUpdated(rowIndex, colIndex);
};
<span class="comment">/**
* <span class="attrib">@private</span>
* Removes the specified range of rows.
* <span class="attrib">@param</span> {Number} startIndex
* <span class="attrib">@param</span> {&lt;i&gt;Number&lt;/i&gt;} endIndex (optional) Defaults to startIndex
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.removeRows = <span class="reserved">function</span>(startIndex, endIndex){
endIndex = endIndex || startIndex;
<span class="reserved">this</span>.data.splice(startIndex, endIndex-startIndex+1);
<span class="reserved">this</span>.fireRowsDeleted(startIndex, endIndex);
};
<span class="comment">/**
* Remove a row.
* <span class="attrib">@param</span> {Number} index
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.removeRow = <span class="reserved">function</span>(index){
<span class="reserved">this</span>.data.splice(index, 1);
<span class="reserved">this</span>.fireRowsDeleted(index, index);
};
<span class="comment">/**
* <span class="attrib">@private</span>
* Removes all rows.
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.removeAll = <span class="reserved">function</span>(){
var count = <span class="reserved">this</span>.getRowCount();
<span class="reserved">if</span>(count &gt; 0){
<span class="reserved">this</span>.removeRows(0, count-1);
}
};
<span class="comment">/**
* Query the DataModel rows by the filters defined in spec, for example...
* &lt;pre&gt;&lt;code&gt;
* // column 1 starts with Jack, column 2 filtered by myFcn, column 3 equals 'Fred'
* dataModel.filter({1: /^Jack.+/i}, 2: myFcn, 3: 'Fred'});
* &lt;/code&gt;&lt;/pre&gt;
* <span class="attrib">@param</span> {Object} spec 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.
* <span class="attrib">@param</span> {Boolean} returnUnmatched True to return rows which &lt;b&gt;don't&lt;/b&gt; match the query instead
* of rows that do match
* <span class="attrib">@return</span> {Array} An array of row indexes that match
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.query = <span class="reserved">function</span>(spec, returnUnmatched){
var d = <span class="reserved">this</span>.data;
var r = [];
<span class="reserved">for</span>(var i = 0; i &lt; d.length; i++){
var row = d[i];
var isMatch = true;
<span class="reserved">for</span>(var col in spec){
<span class="reserved">if</span>(typeof spec[col] != <span class="literal">'function'</span>){
<span class="reserved">if</span>(!isMatch) continue;
var filter = spec[col];
switch(typeof filter){
case <span class="literal">'string'</span>:
case <span class="literal">'number'</span>:
case <span class="literal">'boolean'</span>:
<span class="reserved">if</span>(row[col] != filter){
isMatch = false;
}
break;
case <span class="literal">'function'</span>:
<span class="reserved">if</span>(!filter(row[col], row)){
isMatch = false;
}
break;
case <span class="literal">'object'</span>:
<span class="reserved">if</span>(filter instanceof RegExp){
<span class="reserved">if</span>(String(row[col]).search(filter) === -1){
isMatch = false;
}
}
break;
}
}
}
<span class="reserved">if</span>(isMatch &amp;&amp; !returnUnmatched){
r.push(i);
}<span class="reserved">else</span> <span class="reserved">if</span>(!isMatch &amp;&amp; returnUnmatched){
r.push(i);
}
}
<span class="reserved">return</span> r;
};
<span class="comment">/**
* Filter the DataModel rows by the query defined in spec, see {<span class="attrib">@link</span> #query} for more details
* on the query spec.
* <span class="attrib">@param</span> {Object} query The query spec {<span class="attrib">@link</span> #query}
* <span class="attrib">@return</span> {Number} The number of rows removed
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.filter = <span class="reserved">function</span>(query){
var matches = <span class="reserved">this</span>.query(query, true);
var data = <span class="reserved">this</span>.data;
<span class="comment">// go thru the data setting matches to deleted</span>
<span class="comment">// while not disturbing row indexes</span>
<span class="reserved">for</span>(var i = 0; i &lt; matches.length; i++){
data[matches[i]]._deleted = true;
}
<span class="reserved">for</span>(var i = 0; i &lt; data.length; i++){
<span class="reserved">while</span>(data[i] &amp;&amp; data[i]._deleted === true){
<span class="reserved">this</span>.removeRow(i);
}
}
<span class="reserved">return</span> matches.length;
};
<span class="comment">/**
* Adds a row to the dataset.
* <span class="attrib">@param</span> {Array} cellValues The array of values for the new row
* <span class="attrib">@return</span> {Number} The index of the added row
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.addRow = <span class="reserved">function</span>(cellValues){
<span class="reserved">this</span>.data.push(cellValues);
var newIndex = <span class="reserved">this</span>.data.length-1;
<span class="reserved">this</span>.fireRowsInserted(newIndex, newIndex);
<span class="reserved">this</span>.applySort();
<span class="reserved">return</span> newIndex;
};
<span class="comment">/**
* <span class="attrib">@private</span>
* Adds a set of rows.
* <span class="attrib">@param</span> {Array} rowData This should be an array of arrays like the constructor takes
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.addRows = <span class="reserved">function</span>(rowData){
<span class="reserved">this</span>.data = <span class="reserved">this</span>.data.concat(rowData);
var firstIndex = <span class="reserved">this</span>.data.length-rowData.length;
<span class="reserved">this</span>.fireRowsInserted(firstIndex, firstIndex+rowData.length-1);
<span class="reserved">this</span>.applySort();
};
<span class="comment">/**
* Inserts a row a the specified location in the dataset.
* <span class="attrib">@param</span> {Number} index The index where the row should be inserted
* <span class="attrib">@param</span> {Array} cellValues The array of values for the new row
* <span class="attrib">@return</span> {Number} The index the row was inserted in
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.insertRow = <span class="reserved">function</span>(index, cellValues){
<span class="reserved">this</span>.data.splice(index, 0, cellValues);
<span class="reserved">this</span>.fireRowsInserted(index, index);
<span class="reserved">this</span>.applySort();
<span class="reserved">return</span> index;
};
<span class="comment">/**
* <span class="attrib">@private</span>
* Inserts a set of rows.
* <span class="attrib">@param</span> {Number} index The index where the rows should be inserted
* <span class="attrib">@param</span> {Array} rowData This should be an array of arrays like the constructor takes
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.insertRows = <span class="reserved">function</span>(index, rowData){
<span class="comment">/*
if(index == this.data.length){ // try these two first since they are faster
this.data = this.data.concat(rowData);
}else if(index == 0){
this.data = rowData.concat(this.data);
}else{
var newData = this.data.slice(0, index);
newData.concat(rowData);
newData.concat(this.data.slice(index));
this.data = newData;
}*/</span>
var args = rowData.concat();
args.splice(0, 0, index, 0);
<span class="reserved">this</span>.data.splice.apply(<span class="reserved">this</span>.data, args);
<span class="reserved">this</span>.fireRowsInserted(index, index+rowData.length-1);
<span class="reserved">this</span>.applySort();
};
<span class="comment">/**
* Applies the last used sort to the current data.
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.applySort = <span class="reserved">function</span>(suppressEvent){
<span class="reserved">if</span>(<span class="reserved">this</span>.columnModel &amp;&amp; typeof <span class="reserved">this</span>.sortColumn != <span class="literal">'undefined'</span>){
<span class="reserved">this</span>.sort(<span class="reserved">this</span>.columnModel, <span class="reserved">this</span>.sortColumn, <span class="reserved">this</span>.sortDir, suppressEvent);
}
};
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.setDefaultSort = <span class="reserved">function</span>(columnModel, columnIndex, direction){
<span class="reserved">this</span>.columnModel = columnModel;
<span class="reserved">this</span>.sortColumn = columnIndex;
<span class="reserved">this</span>.sortDir = direction;
};
<span class="comment">/**
* Sorts the data by the specified column - Uses the sortType specified for the column in the passed columnModel.
* <span class="attrib">@param</span> {YAHOO.ext.grid.DefaultColumnModel} columnModel The ColumnModel for this dataset
* <span class="attrib">@param</span> {Number} columnIndex The column index to sort by
* <span class="attrib">@param</span> {String} direction The direction of the sort ('DESC' or 'ASC')
*/</span>
YAHOO.ext.grid.DefaultDataModel.<span class="reserved">prototype</span>.sort = <span class="reserved">function</span>(columnModel, columnIndex, direction, suppressEvent){
<span class="comment">// store these so we can maintain sorting when we load new data</span>
<span class="reserved">this</span>.columnModel = columnModel;
<span class="reserved">this</span>.sortColumn = columnIndex;
<span class="reserved">this</span>.sortDir = direction;
var dsc = direction == <span class="literal">'DESC'</span>;
var sortType = columnModel.getSortType(columnIndex);
var fn = <span class="reserved">function</span>(cells, cells2){
var v1 = sortType(cells[columnIndex], cells);
var v2 = sortType(cells2[columnIndex], cells2);
<span class="reserved">if</span>(v1 &lt; v2)
<span class="reserved">return</span> dsc ? -1 : +1;
<span class="reserved">if</span>(v1 &gt; v2)
<span class="reserved">return</span> dsc ? +1 : -1;
<span class="reserved">return</span> 0;
};
<span class="reserved">this</span>.data.sort(fn);
<span class="reserved">if</span>(!suppressEvent){
<span class="reserved">this</span>.fireRowsSorted(columnIndex, direction);
}
};</pre>
<hr>
<hr>
<font size="-1">
</font>
<div class="jsdoc_ctime">Documentation generated by <a href="http://jsdoc.sourceforge.net/" target="_parent">JSDoc</a> on Sat Oct 14 06:07:10 2006</div>
</body>
</html>