技术开发 频道

使用Javascript模拟一个DataTable!


【IT168技术文档】

  看过一位大侠写的AjaxTable,我就自己也写了这个一个很件简单的东西,但是本人不是出于好玩,是觉得在使用Ajax中如果使用这样的复杂对象来保存数据也许是一个不错的选择,故而写了这样一个看上去多此一举的东东,请大家批评指正。这里只是写了一些简单功能,后续还要不断完善。
function iDataTable(tbName) { //Begin New if(tbName==undefined){tbName="table1"} this._Columns=new Array(); this._Rows=new Array(); //End New this.TableName=tbName; this.DataSchema=""; this.Columns=iColumnsCollection; this.Rows=iRowCollection; } function iColumnsCollection(_iColumnIndex) { this.Add=function(iCol) { if(!this.Exists(iCol.ColumnName)) { iCol.ColumnIndex=this._Columns.length; this._Columns.push(iCol) } } this.Remove=function(iCol) { if(this.Exists(iCol.ColumnName)) this._Columns.splice(iCol.ColumnIndex) } this.Exists=function(iColumnName) { return (this.IndexOf(iColumnName)>-1); } this.IndexOf=function(iColumnName) { for(var _Index=0;_Index<this._Columns.length;_Index++) { if(this._Columns[_Index].ColumnName==iColumnName) { return _Index; break; } } return -1; } if(_iColumnIndex!=undefined) { if(_iColumnIndex<0 || _iColumnIndex>this._Columns.length-1) { alert("Column Index Error") } else { return this._Columns[_iColumnIndex]; } } return this; } function iColumn(_ColumnName,_DataType) { this.ColumnName=_ColumnName; this.DataType=_DataType;//System.String System.Decimal System.Int32 System.DateTime this.ColumnIndex=-1; } function iRowCollection(_iRowIndex) { this.Add=function(_iRow) { _iRow.RowIndex=this._Rows.length; this._Rows.push(_iRow); } this.Remove=function(_iRow) { if(_iRow.RowIndex>-1 && _iRow.RowIndex<this._Rows.length) { this._Rows.splice(_iRow.RowIndex); } } if(_iRowIndex!=undefined) { if(_iRowIndex<0 || _iRowIndex>this._Rows.length-1) { alert("Row Index Error") } else { return this._Rows[_iRowIndex]; } } this.insertRow=function() { var _iRow=new iRow(this._Columns); this.Add(_iRow); return _iRow; } return this; } function iRow(_ColumnsX) { this._Cells=new Array(); var t=_ColumnsX.length; for(var i=0;i<t;i++) { var _Cell=new iCell(); _Cell.ColumnName=_ColumnsX[i].ColumnName; _Cell.CellIndex=i; this._Cells.push(_Cell); } this.Cells=function(_CellIndex) { if(_CellIndex!=undefined) {return this._Cells[_CellIndex];} else { return this._Cells; } } this.RowIndex=-1; return this; } function iCell(_ColumnName) { this.value=""; this.ColumnName=_ColumnName; this.CellIndex=-1; }
0
相关文章