You're here: Snippet Directory » HTML/JavaScript (65)
Language:

JavaScript ADO Recordset Object Clone

Language: English
Programming Language: JavaScript
Published by: pjjt [not registered]
Last Update: 5/15/2006
Views: 407


Description

This code makes it possible to pass data to the client, and use the same code you'd use for an ADO Recordset on the server.

Code

1 // use this in ASP pages 2 function GetRecordsetCode(oRS) { 3 var strCode = "new Recordset( (new Array(" 4 for (var i=0; i<oRS.Fields.Count; i++) { 5 strCode += "'" + oRS.Fields(i).Name + "'" + ((i != oRS.Fields.Count-1) ? "," : ")"); 6 } 7 if (!oRS.EOF) { 8 strCode += "), (new Array( new Array('" 9 + oRS.GetString(2,-1, "&&&&1&&&&", "&&&&2&&&&").replace(/'/g,"&quot;").replace(/\r\n/g,"<br>").replace(/&&&&1&&&&/g,"','").replace(/&&&&2&&&&/g,"'),new Array('") 10 + "'))"; 11 strCode = strCode.substring(0,strCode.length-15) + ")"; 12 } else strCode += "), (new Array()"; 13 strCode += "));"; 14 return strCode; 15 } 16 17 // use this on the client 18 function Recordset(arrFieldNames,arrData) { 19 this.Columns = arrFieldNames; 20 this.Data = arrData; 21 22 this.Columns.Count = this.Columns.length; 23 this.Record = 0; 24 this.AbsolutePosition = 1; 25 this.EOF = false; 26 this.BOF = false; 27 this.RecordCount = arrData.length; 28 29 this.Fields = Recordset_Fields; 30 this.Move = Recordset_Move; 31 this.MoveFirst = Recordset_MoveFirst; 32 this.MoveNext = Recordset_MoveNext; 33 this.MovePrevious = Recordset_MovePrevious; 34 this.MoveLast = Recordset_MoveLast; 35 this.AddNew = Recordset_AddNew; 36 this.Update = Recordset_Update; 37 this.Delete = Recordset_Delete; 38 this.CheckPosition = Recordset_CheckPosition; 39 40 this.CheckPosition(); 41 } 42 43 function Recordset_Fields(x, debug) { 44 this.Record = this.AbsolutePosition-1; 45 if (isNaN(x*1)) { 46 for (var i=0; i<this.Columns.length; i++) if (this.Columns[i].toUpperCase() == x.toUpperCase()) return (new Field(this.Columns[i], this.Data[this.Record][i])); 47 } else if (x < this.Data[this.Record].length) { 48 return (new Field(this.Columns[x], this.Data[this.Record][x])); 49 } 50 alert("JS Recordset error: Item (" + x + ") not found."); 51 return (new Field(x, "")); 52 } 53 54 function Recordset_Move(i) { 55 this.Record += i-1; 56 this.AbsolutePosition += i; 57 this.CheckPosition(); 58 } 59 60 function Recordset_MoveFirst() { 61 this.Record = 0; 62 this.AbsolutePosition = 1; 63 this.EOF = false; 64 this.BOF = false; 65 } 66 67 function Recordset_MoveNext() { 68 this.Record++; 69 this.AbsolutePosition++; 70 this.CheckPosition(0); 71 } 72 73 function Recordset_MovePrevious() { 74 this.Record--; 75 this.AbsolutePosition--; 76 this.CheckPosition(1); 77 } 78 79 function Recordset_MoveLast() { 80 this.Record = this.Data.length-1; 81 this.AbsolutePosition = this.Data.length; 82 this.EOF = false; 83 this.BOF = false; 84 } 85 86 function Recordset_AddNew(xFields, xValues) { 87 this.Record = this.Data.length; 88 this.AbsolutePosition = this.Data.length+1; 89 this.Data[this.Data.length] = new Array(this.Columns.length); 90 this.RecordCount = this.Data.length; 91 if (xFields) this.Update(xFields, xValues); 92 } 93 94 function Recordset_Update(xFields, xValues) { 95 this.Record = this.AbsolutePosition-1; 96 if (typeof(xFields) == "string") { 97 for (var i=0; i<this.Columns.length; i++) { 98 if (this.Columns[i].toUpperCase() == xFields.toUpperCase()) { 99 this.Data[this.Record][i] = xValues + ""; 100 return true; 101 } 102 } 103 throw "JS Recordset error: Item (" + xFields + ") not found."; 104 } else { 105 if (xFields.length) for (var i=0; i<xFields.length; i++) this.Update(xFields[i], xValues[i]); 106 return true; 107 } 108 } 109 110 function Recordset_Delete() { 111 this.Data = this.Data.remove(this.Record); 112 this.RecordCount = this.Data.length; 113 this.Record--; 114 this.AbsolutePosition--; 115 this.CheckPosition(); 116 } 117 118 function Field(strName, strValue) { 119 this.Name = strName; 120 this.Value = strValue; 121 } 122 123 function Recordset_CheckPosition(iDirection) { 124 switch(iDirection) { 125 case 0 : // forward - check EOF 126 if (this.Record == this.Data.length) this.EOF = true; 127 else this.EOF = false; 128 break; 129 130 case 1 : // backward - check BOF 131 if (this.Record == -1) this.BOF = true; 132 else this.BOF = false; 133 break; 134 135 default : 136 if (this.Record == this.Data.length || this.Data.length == 0) this.EOF = true; 137 else this.EOF = false; 138 if (this.Record == -1 || this.Data.length == 0) this.BOF = true; 139 else this.BOF = false; 140 } 141 } 142

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS