Click here to Skip to main content
11,645,087 members (62,830 online)
Click here to Skip to main content
Articles » Database » Database » General » Downloads
Add your own
alternative version

Online DB Administration

, 17 Mar 2003 200.8K 2.3K 58
Online administration of your database. Create or modify tables, keys, and indexes. Edit or insert data.
<!-- b.grid.asp             - copyright (c) 2001-2002 by Bj�rnar Henden http://www.bhenden.org/ -->
<!--#include file=b.dropdown.asp-->
<!--#include file=b.func.asp-->

<script language=javascript runat=server>
function CreateGrid(rs) {
	return new BGrid(rs);
}

function BGrid(rs,pk) {
	// Config stuff
	this._config_image_path = "images/";
	// End config stuff

	if(""+pk!="undefined") {
		throw new Error("Too many arguments to grid. Set PK manually with Grid.SetOption(\"pk\",\"field1,field2,field3\") if necessary.");
	}

	if(!rs.Supports(adApproxPosition | adBookmark) || rs.RecordCount<0) {
		throw new Error("Wrong kind of recordset. Must support adApproxPosition.");
	}

	this.Process = BGrid_Process;
	this.Display = BGrid_Display;
	this.IsNull = BGrid_IsNull;
	this.IsProtected = BGrid_IsProtected;
	this.IsHidden = BGrid_IsHidden;
	this.IsLookup = BGrid_IsLookup;
	this.IsKeyColumn = BGrid_IsKeyColumn;
	this.IsFieldTypeSupported = BGrid_IsFieldTypeSupported;
	this.IsStringType = BGrid_IsStringType;
	this.GetFormValue = BGrid_GetFormValue;
	this.PutFormValue = BGrid_PutFormValue;
	this.GetDefault = BGrid_GetDefault;
	this.SetDefault = BGrid_SetDefault;
	this.ProtectFields = BGrid_ProtectFields;
	this.HideFields = BGrid_HideFields;
	this.DisplayEdit = BGrid_DisplayEdit;
	this.ConvertValue = BGrid_ConvertValue;
	this.SetLookup = BGrid_SetLookup;
	this.GetLookupIndex = BGrid_GetLookupIndex;
	this.GetLookupValue = BGrid_GetLookupValue;
	this.FindKeyRecord = BGrid_FindKeyRecord;
	this.SetOption = BGrid_SetOption;
	this.AddError = BGrid_AddError;
	this.CreateSmallButton = BGrid_CreateSmallButton;
	this.CreateButton = BGrid_CreateButton;
	this.GetValue = BGrid_GetValue;
	this.IsEditMode = BGrid_IsEditMode;
	this.IsBrowseMode = BGrid_IsBrowseMode;
	this.IsUpdated = BGrid_IsUpdated;
	this.IsProcessed = BGrid_IsProcessed;
	this.DisplayControls = BGrid_DisplayControls;
	
	this._pk = "";
	this._rs = rs;
	this._pageSize = 10;
	this._pageNo = this.GetFormValue("pageno",0);
	this._action = this.GetFormValue("action","none");
	this._defaultFields = new Array();
	this._defaultValues = new Array();
	this._protected = "";
	this._hidden = "";
	this._error = false;
	this._lookup = new Array;
	this._sortField = this.GetFormValue("sortfield","");
	this._sortOrder = this.GetFormValue("sortorder","ASC");
	this._option_debug = false;
	this._option_truncate = 0;
	this._filter_old = this._rs.Filter;
	this._errorList = new Array();
	this._option_pageselect = true;
	this._bookmark = null;
	this._mode = "browse";
	this._updated = false;
	this._processed = false;
	this._focuscontrol = "";

	this._rs.PageSize = this._pageSize;
	
	// Scan through recordset and protect/hide strange field types that we don't support
	for(var n=0;n<this._rs.Fields.Count;n++) {
		if(!this.IsFieldTypeSupported(this._rs.Fields(n))) {
			this.ProtectFields(this._rs.Fields(n).Name);
			this.HideFields(this._rs.Fields(n).Name);
		}
		if(this.IsKeyColumn(this._rs.Fields(n))) {
			if(this._pk!="") this._pk += ",";
			this._pk += this._rs.Fields(n).Name;
		}
	}

	//this._pk = "ShipName,ShipAddress,OrderID,ProductID";	
	//DebugOut(Request.Form);
}

function BGrid_IsFieldTypeSupported(oField) {
	return oField.Type!=adLongVarBinary;
}

function BGrid_Process() {
	if(Request.Form("action").Item=="sort") {
		var sField = this.GetFormValue("sort","");
		if(this._sortField==sField) {
			if(this._sortOrder=="ASC")
				this._sortOrder = "DESC";
			else
				this._sortOrder = "ASC";
		} else {
			this._sortField = sField;
			this._sortOrder = "ASC";
		}
	}

	if(this._sortField!="" && this._rs.CursorLocation==adUseClient) {
		try {
			this._rs.Sort = this._sortField + " " + this._sortOrder;
		}
		catch(e) {
			this.AddError(new Error("The field "+this._sortField+" can't be sorted."));
			this._sortField = "";
			this._sortOrder = "ASC";
		}
	}

	if(Request.Form("action").Item=="savenew" || Request.Form("action").Item=="save") {
		var bNewRecord = Request.Form("action").Item=="savenew";
		
		try {
			// Add new record if needed
			if(bNewRecord) this._rs.AddNew();
			// if not find existing one to edit
			if(bNewRecord || this.FindKeyRecord()) {
				for(var n=0;n<this._rs.Fields.Count;n++) {
					var oField = this._rs.Fields(n);

					//if(!this.IsHidden(oField.Name) && !this.IsProtected(oField.Name)) {
					if(true) {
						if((oField.Attributes & (adFldUpdatable|adFldUnknownUpdatable)) && this.IsFieldTypeSupported(oField)) {
							var oValue;

							if(this.IsHidden(oField.Name) || this.IsProtected(oField.Name)) {
								// Don't alter value of hidden or protected
								// fields if the value is set.
								if(!oField.Value) {
									oValue = this.GetDefault(oField.Name);
									if(""+oValue!="") oField.Value = oValue;
								}
							} else {
								switch(oField.Type) {
								case adBoolean:
									oValue = Request.Form("dbField"+oField.Name).Count>0;
									break;
								default:
									oValue = Request.Form("dbField"+oField.Name).Item;
									break;
								}
								if(""+oValue=="") oValue = null;
								// Only set value if it's changed
								if(oField.Value!=oValue) oField.Value = oValue;
							}
						}
					}

					oField = null;
				}
				this._rs.Update();
				this._bookmark = this._rs.Bookmark;
				this._isupdated = true;
			}
		}
		catch(e) {
			this._error = true;
			this.AddError(e);
			this._rs.CancelUpdate();
		}
	} else if(Request.Form("action").Item=="del") {
		try {
			if(this.FindKeyRecord()) 
				this._rs.Delete();
			else
				throw new Error("Couldn't find record to delete");
		}
		catch(e) {
			this._error = true;
			this.AddError(e);
			this._rs.CancelUpdate();
		}
	}

	if(this._rs.RecordCount>0) {
		if(Request.Form("action").Item=="next") {
			if(this._pageNo<Math.ceil(this._rs.RecordCount / this._pageSize)-1)
				this._pageNo++;
		} else if(Request.Form("action").Item=="prev") {
			if(this._pageNo>0)
				this._pageNo--;
		} else if(Request.Form("action").Item=="last") {
			this._pageNo = Math.ceil(this._rs.RecordCount / this._pageSize) - 1;
		} else if(Request.Form("action").Item=="first") {
			this._pageNo = 0;
		} else if(Request.Form("action").Item=="page") {
			this._pageNo = Request.Form("param").Item - 1;
		}
	}

	this._rs.Filter = this._filter_old;
	this._processed = true;
	return true;
}

function BGrid_Display() {
	var nVisibleColumns = 0;
	var nRow = 1;

	// Add new record
	if(Request.Form("action").Item=="add" || (this._error && Request.Form("action").Item=="savenew")) {
		this._rs.AddNew();
		this.DisplayEdit(true);
		this._rs.CancelUpdate();
	} else if(Request.Form("action").Item=="edit" || (Request.Form("action").Item=="save" && this._error)) {
		if(this.FindKeyRecord()) this.DisplayEdit(false);
	} else if(Request.Form("action").Item=="copy" || (Request.Form("action").Item=="save" && this._error)) {
		if(this.FindKeyRecord()) this.DisplayEdit(true);
	} else {
		Out("<table cellspacing=\"2\" class=\"bgrid\">");
		if(this._rs.EOF || this._rs.BOF || this._rs.RecordCount==0) {
			this.DisplayControls(0);
			Out("<tr><td>No data...</td></tr>");
		} else {
			// Find number of visible columns
			for(var n=0;n<this._rs.Fields.Count;n++) {
				if(!this.IsHidden(this._rs.Fields(n).Name))
					nVisibleColumns++;
			}
			// Display controls
			this.DisplayControls(nVisibleColumns);

			Out("<tr class=\"gridheader\">");
			Out("<td>&nbsp;</td>");
			for(var n=0;n<this._rs.Fields.Count;n++) {
				if(this.IsHidden(this._rs.Fields(n).Name)) continue;
				var oField = this._rs.Fields(n);
				var bIsKey = this.IsKeyColumn(oField);

				Out("<td><nobr><a href=\"\" onclick=\"document.forms[0].action.value='sort';document.forms[0].sort.value='"+this._rs.Fields(n).Name+"';document.forms[0].submit();return false\">");
				if(bIsKey) Out("<em>");		// Show primary keys in italic
				Out(this._rs.Fields(n).Name);
				if(bIsKey) Out("</em>");	// Show primary keys in italic
				Out("</a>");
				if(oField.Name==this._sortField) {
					if(this._sortOrder=="DESC")
						Out(" (D)");
					else
						Out(" (A)");
				}
				Out("</nobr></td>");
			}
			Out("</tr>");

			// Move to the correct page
			this._rs.MoveFirst();
			this._rs.Move(this._pageNo * this._pageSize);
	
			// Show this page
			while(!this._rs.EOF) {
				Out("<tr class=\"gridrow\">");
	
				Out("<td class=title><nobr>" + (nRow + this._pageSize * this._pageNo));
				var sCode = "";
				if(this._pk!="") {
					var a = this._pk.split(",");
					for(n=0;n<a.length;n++) {
						var v = "" + this._rs.Fields(a[n]).Value;
						v = v.replace("'","\\'");
						sCode += "document.forms[0]." + a[n] + ".value='"+v+"';";
					}
					this.CreateSmallButton("Edit","document.forms[0].action.value='edit';"+sCode+"document.forms[0].submit()");
					this.CreateSmallButton("Del","if(confirm('Delete this record?')) { document.forms[0].action.value='del';"+sCode+"document.forms[0].submit();}");
					this.CreateSmallButton("Copy","if(confirm('Copy this record?')) { document.forms[0].action.value='copy';"+sCode+"document.forms[0].submit()}");
				}
				Out("</nobr></td>");

				for(var n=0;n<this._rs.Fields.Count;n++) {
					if(this.IsHidden(this._rs.Fields(n).Name)) continue;
					var oField = this._rs.Fields(n);
					var sValue;
					
					if(this.IsNull(oField))
						sValue = "&nbsp;";
					else if(this.IsLookup(oField.Name))
						sValue = "" + this.GetLookupValue(oField.Name,oField.Value);
					else
						sValue = "" + this.ConvertValue(oField.Value,oField.Type);

					if(this._option_truncate>0 && sValue.length>this._option_truncate)
						sValue = sValue.slice(0,this._option_truncate) + "...";

					Out("<td>" + sValue + "</td>\n");
					
					oField = null;
				}
				Out("</tr>");
				this._rs.MoveNext();
				nRow++;
				if(nRow>this._pageSize) break;
			}
		}

		//this.DisplayControls(nVisibleColumns);

		Out("</table>");
	}
	
	this.PutFormValue("action","none");
	this.PutFormValue("pageno",this._pageNo);
	this.PutFormValue("sort","");
	this.PutFormValue("sortfield",this._sortField);
	this.PutFormValue("sortorder",this._sortOrder);
	this.PutFormValue("param","");
	var a = this._pk.split(",");
	for(n=0;n<a.length;n++) this.PutFormValue(a[n],this.GetFormValue(a[n],""));

	/*
	** Display any error messages received
	*/
	var errors = new Enumerator(this._errorList);
	if(!errors.atEnd()) {
		Out("<div class=\"bgriderror\">ERRORS!<ul>");
		while(!errors.atEnd()) {
			var e = errors.item();
			errors.moveNext();

			Out("<li>" + e.description + "</li>");
		}
		Out("</ul>");
		Out("</div>");
	}

	return true;
}

function BGrid_DisplayControls(nVisibleColumns) {
	// Footer
	Out("<tr class=\"gridfooter\">");
	if(nVisibleColumns) {
		//Out("<td>&nbsp;</td>");
		Out("<td colspan=\"" + (1+nVisibleColumns) + "\"><nobr>");
		this.CreateButton("Previous","this.form.action.value='prev';this.form.submit()",this._pageNo==0);
		this.CreateButton("Next","this.form.action.value='next';this.form.submit()",this._pageNo>=Math.ceil(this._rs.RecordCount / this._pageSize)-1);
		this.CreateButton("First","this.form.action.value='first';this.form.submit()",this._pageNo==0);
		this.CreateButton("Last","this.form.action.value='last';this.form.submit()",this._pageNo>=Math.ceil(this._rs.RecordCount / this._pageSize)-1);
		//if(this._pk!="")
		this.CreateButton("Add","this.form.action.value='add';this.form.submit()",this._pk=="");
			
		var nPages = Math.ceil(this._rs.RecordCount / this._pageSize);
		var nCurrPage = this._pageNo -(-1);
		if(nPages>1 && this._option_pageselect) {
			Out('<select onchange="this.form.action.value=\'page\';this.form.param.value=this.value;this.form.submit();return false" id=select1 name=select1>');
			for(var n=1;n<=nPages;n++) {
				if(n==nCurrPage)
					Out('<option selected value="' + n + '">Page ' + n + '</option>');
				else
					Out('<option value="' + n + '">Page ' + n + '</option>');
			}
			Out('</select>');
			Out(" of " + Math.ceil(this._rs.RecordCount / this._pageSize) + " ");
		} else {
			Out("Page " + nCurrPage + " of " + nPages + " ");
		}
	} else {
		Out("<td><nobr>");
		if(this._pk!="")
			this.CreateButton("Add","this.form.action.value='add';this.form.submit()",this._pk=="");
		else
			Out("&nbsp;");
	}
	Out("</nobr></td>");
	Out("</tr>");
	// FooterEnd
}

function BGrid_IsNull(oField) {
	return "" + oField.Value == "null";
}

function BGrid_GetFormValue(varname,vardef) {
	if(Request.Form(varname).Count==1)
		return Request.Form(varname).Item;
	else
		return vardef;
}

function BGrid_PutFormValue(varname,varvalue) {
	Out("<input type=\"hidden\" name=\"" + varname + "\" value=\"" + varvalue + "\"/>\n");
}

function BGrid_ConvertValue(oValue,nType) {
	var sValue = "";
	switch(nType) {
	case adDBTimeStamp:
		sValue = "" + jsFormatDateTime(oValue,0);
		break;
	case adDBDate:
	case adDate:
		sValue = "" + jsFormatDateTime(oValue,2);
		break;
	case adDBTime:
		sValue = "" + jsFormatDateTime(oValue,4);
		break;
	default:
		sValue = "" + oValue;
		break;
	}
	return sValue;
}

function BGrid_GetDefault(sFieldName,nType) {
	var nLength = this._defaultFields.length;
	for(var n=0;n<nLength;n++) {
		if(sFieldName==this._defaultFields[n]) {
			var rsTmp = this._rs.ActiveConnection.Execute(this._defaultValues[n]);
			var sDefault = "";
			if(!this.IsNull(rsTmp(0))) sDefault = this.ConvertValue(rsTmp(0).Value,nType);
			rsTmp.Close();
			rsTmp = null;
			return sDefault;
		}
	}
	return "";
}

function BGrid_SetDefault(sFieldName,sDefault) {
	var nLength = this._defaultFields.length;
	this._defaultFields[nLength] = sFieldName;
	this._defaultValues[nLength] = sDefault;
}

function BGrid_ProtectFields(sFields) {
	var aFields = sFields.split(",");
	if(this._protected!="") this._protected += ",";
	this._protected += aFields.join(",");
}

function BGrid_HideFields(sFields) {
	var aFields = sFields.split(",");
	if(this._hidden!="") this._hidden += ",";
	this._hidden += aFields.join(",");
}

function BGrid_IsProtected(sFieldName) {
	var aFields = this._protected.split(",");
	var nLength = aFields.length;
	for(var n=0;n<nLength;n++) {
		if(aFields[n]==sFieldName) 
			return true;
	}
	return false;
}

function BGrid_IsHidden(sFieldName) {
	var aFields = this._hidden.split(",");
	var nLength = aFields.length;
	for(var n=0;n<nLength;n++) {
		if(aFields[n]==sFieldName) 
			return true;
	}
	return false;
}

function BGrid_DisplayEdit(bNew) {
	var bCopy = Request.Form("action").Count==1 && Request.Form("action").Item=="copy";

	Out("<table cellspacing=\"2\" class=\"bgrid\">");
	for(var n=0;n<this._rs.Fields.Count;n++) {
		if(this.IsHidden(this._rs.Fields(n).Name)) continue;
		var oField = this._rs.Fields(n);
		Out("<tr class=\"editrow\"><td class=\"title\">");
		if(this.IsKeyColumn(oField))
			Out("<em>" + oField.Name + "</em>");
		else
			Out(oField.Name);
		Out(":&nbsp;</td><td>");

		if((oField.Attributes & (adFldUpdatable|adFldUnknownUpdatable)) && !this.IsProtected(oField.Name)) {
			var sValue = "";
			if(Request.Form("dbField"+oField.Name).Count==1)
				sValue = Request.Form("dbField"+oField.Name).Item;
			else if(bNew)
				sValue = this.GetDefault(oField.Name,oField.Type);
			else if(!this.IsNull(oField))
				sValue = this.ConvertValue(oField.Value,oField.Type);
			
			if(bCopy && sValue=="" && !this.IsNull(oField))
				sValue = this.ConvertValue(oField.Value,oField.Type);

			if(this.IsLookup(oField.Name)) {
				var lData = this._lookup[this.GetLookupIndex(oField.Name)];
				var dd = new BDropdown("DBField"+oField.Name,lData[1],lData[2]);
				dd.SetValue(sValue);
				dd.SetOption("onchange_submit",false);
				dd.Display();
				dd = null;
				if(this._focuscontrol=="") this._focuscontrol = "DBField" + oField.Name;
			} else {
				sValue = Server.HTMLEncode("" + sValue);
				
				switch(oField.Type) {
				case adLongVarWChar:
				case adLongVarChar:
					Out("<textarea class=\"bgrid_input\" name=\"dbField" + oField.Name + "\">" + sValue + "</textarea>");
					if(this._focuscontrol=="") this._focuscontrol = "dbField" + oField.Name;
					break;
				case adBoolean:
					Out("<input style=\"alignment:left\" type=\"checkbox\" name=\"dbField" + oField.Name + "\"");
					if(oField.Value || this.GetDefault(oField.Name)) Out(" checked value=\"1\"");
					if(this._focuscontrol=="") this._focuscontrol = "dbField" + oField.Name;
					Out("/>");
					break;
				default:
					Out("<input class=\"bgrid_input\" type=\"edit\" name=\"dbField" + oField.Name + "\" value=\"" + sValue + "\"/>");
					if(this._focuscontrol=="") this._focuscontrol = "dbField" + oField.Name;
					break;
				}
			}
		} else {
			var sValue = "";
			if(bNew)
				sValue = this.GetDefault(oField.Name,oField.Type);
			else if(!this.IsNull(oField))
				sValue = this.ConvertValue(oField.Value,oField.Type);

			if(bCopy && sValue=="" && !this.IsNull(oField))
				sValue = this.ConvertValue(oField.Value,oField.Type);

			Out("<input class=\"bgrid_disabled\" type=\"edit\" disabled value=\""+sValue+"\"/>");
		}
		if(this._option_debug) {
			Out("</td><td>");
			Out(BGrid_GetDataTypeEnum(oField.Type));
			Out("</td><td>");
			Out(BGrid_GetFieldAttributeEnum(oField.Attributes));
		}
		Out("</td></tr>\n");
				
		oField = null;
	}
	Out("<tr class=\"editfooter\"><td>&nbsp;</td><td>");
	if(bNew)
		this.CreateButton("Save","this.form.action.value='savenew';this.form.submit()");
	else
		this.CreateButton("Save","this.form.action.value='save';this.form.submit()");
	this.CreateButton("Cancel","this.form.submit()");
	Out("</td></tr>");
	
	Out("</table>");
	this._mode = "edit";

	// Set focus to first control
	if(this._focuscontrol!="")
		Out('\n<scr'+'ipt>document.forms[0].'+this._focuscontrol+'.focus()</scr'+'ipt>\n');
}

function BGrid_SetLookup(sFieldName,rsData,sField) {
	var a = new Array;
	a[0] = sFieldName;
	a[1] = rsData;
	a[2] = sField;
	a[3] = BDropdown_GetTextField(rsData);
	
	this._lookup[this._lookup.length] = a;
}

function BGrid_IsLookup(sFieldName) {
	var nLength = this._lookup.length;
	for(var n=0;n<nLength;n++) {
		if(this._lookup[n][0]==sFieldName)
			return true;
	}
	return false;
}

function BGrid_GetLookupIndex(sFieldName) {
	var nLength = this._lookup.length;
	for(var n=0;n<nLength;n++) {
		if(this._lookup[n][0]==sFieldName)
			return n;
	}
	throw new Error("Failed to find lookup index.");
}

function BGrid_GetLookupValue(sFieldName,value) {
	var sValue = "" + value;
	var nIndex = this.GetLookupIndex(sFieldName);
	var rs = this._lookup[nIndex][1];

	rs.MoveFirst();
	if(this.IsStringType(rs.Fields(this._lookup[nIndex][2]).Type))
		rs.Find(this._lookup[nIndex][2]+"='"+value+"'");
	else
		rs.Find(this._lookup[nIndex][2]+"="+value);
	if(!rs.EOF && !rs.BOF)
		sValue = "" + rs(this._lookup[nIndex][3]).Value;
	rs = null;
	return sValue;
}

function BGrid_GetDataTypeEnum(value) {
	switch(value) {
	case adInteger:			return "Integer";
	case adVarWChar:		return "VarWChar";
	case adLongVarWChar:	return "LongVarWChar";
	case adCurrency:		return "Currency";
	case adSmallInt:		return "SmallInt";
	case adBoolean:			return "Boolean";
	case adSingle:			return "Single";
	case adVarChar:			return "VarChar";
	case adDBDate:			return "DBDate";
	case adDBTime:			return "DBTime";
	case adDBTimeStamp:		return "DBTimeStamp";
	case adNumeric:			return "Numeric";
	case adDouble:			return "Double";
	case adLongVarChar:		return "LongVarChar";
	case adTinyInt:			return "TinyInt";
	case adBinary:			return "Binary";
	case adWChar:			return "WChar";
	case adChar:			return "Char";
	case adDate:			return "Date";
	case adUnsignedTinyInt:	return "UnsignedTinyInt";
	case adGUID:			return "GUID";
	case adBSTR:			return "BSTR";
	case adEmpty:			return "Empty";
	case adVariant:			return "Variant";
	default: return "" + value;
	}
}

function BGrid_GetFieldAttributeEnum(value) {
	var s = "";
	if(value==adFldUnspecified) return "Unspecified";
	if(value & adFldCacheDeferred)		s += "CacheDeferred, ";
	if(value & adFldFixed)				s += "Fixed, ";
	if(value & adFldIsChapter)			s += "IsChapter, ";
	if(value & adFldIsCollection)		s += "IsCollection, ";
	if(value & adFldIsDefaultStream)	s += "IsDefaultStream, ";
	if(value & adFldIsNullable)			s += "IsNullable, ";
	if(value & adFldIsRowURL)			s += "IsRowURL, ";
	if(value & adFldLong)				s += "Long, ";
	if(value & adFldMayBeNull)			s += "MayBeNull, ";
	if(value & adFldMayDefer)			s += "MayDefer, ";
	if(value & adFldNegativeScale)		s += "NegativeScale, ";
	if(value & adFldRowID)				s += "RowID, ";
	if(value & adFldRowVersion)			s += "RowVersion, ";
	if(value & adFldUnknownUpdatable)	s += "UnknownUpdatable, ";
	if(value & adFldUpdatable)			s += "Updatable, ";
	
	if(s!="") s = s.slice(0,s.length-2);
	return s;
}

function BGrid_IsKeyColumn(oField) {
	return oField.Properties("KEYCOLUMN").Value;
}

function BGrid_FindKeyRecord() {
	var a = this._pk.split(",");
	var sFind = "";
	for(n=0;n<a.length;n++) {
		if(sFind!="") sFind += " AND ";
		sFind += a[n] + "=";
		if(this.IsStringType(this._rs.Fields(a[n]).Type)) {
			var v = "" + this.GetFormValue(a[n],"");
			v = v.replace("'","''");
			sFind += "'" + v + "'";
		} else
			sFind += this.GetFormValue(a[n],0);
	}
	this._rs.Filter = sFind;
	return !this._rs.BOF && !this._rs.EOF;
}

function BGrid_IsStringType(type) {
	return type==adVarWChar || type==adWChar || type==adVarChar || type==adChar;
}

function BGrid_SetOption(option,value) {
	if(this.IsProcessed()) throw new Error("BGrid.SetOption must be called before Process!");
	switch(option) {
	case "debug":
		this._option_debug = value;
		break;
	case "pagesize":
		this._pageSize = value;
		break;
	case "pk":
		this._pk = value;
		break;
	case "truncate":
		this._option_truncate = value;
		break;
	case "pageselect":
		this._option_pageselect = value;
		break;
	default:
		throw new Error("Unknown grid option \"" + option + "\"!");
	}
}

function BGrid_AddError(e) {
	var nLen = this._errorList.length;
	this._errorList[nLen] = e;
}

function BGrid_CreateSmallButton(title,href) {
	var sImage = "";
	var sTitle = "";
		
	switch(title) {
	case "Edit":
		sImage = this._config_image_path + "edit.gif";
		sTitle = "Edit this record";
		break;
	case "Del":
		sImage = this._config_image_path + "delete.gif";
		sTitle = "Delete this record";
		break;
	case "Copy":
		sImage = this._config_image_path + "copy.gif";
		sTitle = "Copy this record";
		break;
	}
	Out(" <a href=\"\" onclick=\"" + href + ";return false\">");
	Out("<img border=0 width=13 height=13 src=\"" + sImage + "\" title=\"" + sTitle + "\"/>");
	Out("</a>");
}

function BGrid_CreateButton(title,href,disabled) {
	var sType = "button";
	if(title=="Save") sType = "submit";
	Out("<input class=\"bgrid_button\" ");
	if(""+disabled!="undefined" && disabled) Out("disabled ");
	Out("type=\"" + sType + "\" value=\"" + title + "\" onclick=\""+href+"; return false\"/> ");
}

function BGrid_GetValue(field) {
	if(!this.IsProcessed()) throw new Error("BGrid.Process must be called before GetValue!");
	try {
		this._rs.Bookmark = this._bookmark;
		return this._rs(field).Value;
	}
	catch(e) {
		return null;
	}
}

function BGrid_IsBrowseMode() {
	if(!this.IsProcessed()) throw new Error("BGrid.Process must be called before IsBrowseMode!");
	return this._mode == "browse";
}

function BGrid_IsEditMode() {
	if(!this.IsProcessed()) throw new Error("BGrid.Process must be called before IsEditMode!");
	return this._mode == "edit";
}

function BGrid_IsUpdated() {
	if(!this.IsProcessed()) throw new Error("BGrid.Process must be called before IsUpdate!");
	return this._isupdated;
}

function BGrid_IsProcessed() {
	return this._processed;
}
</script>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Bjornar Henden
Web Developer
Norway Norway
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 18 Mar 2003
Article Copyright 2002 by Bjornar Henden
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid