Source Code

for file /controls/Select.js

// Select.js
// Javascript Behaviour for the Select Control
// Copyright (c) by Matthias Hertel, http://www.mathertel.de
// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
// ----- 
// 22.08.2005 created by Matthias Hertel
// 27.09.2005 PageProperty attribute added
// 25.12.2005 CreateOptions defaultOption added
// 07.07.2007 syntax error fixed.
// 25.08.2007 registering in OpenAjax added.
// 14.09.2007 eventname attribute added and pageproperty removed.

var SelectBehaviour = {

  // ----- Properties -----
  name: null, // the name of the element for the AjaxForm recordset
  nosubmit: true,

  // ----- Events -----
  onkeypress: function (evt) {
    evt = evt || window.event;
    if (evt.keyCode == 13)
      OpenAjax.hub.publish(jcl.BuildFullEventname(this), this.value);
  }, // onkeypress


  onchange: function (evt) {
    OpenAjax.hub.publish(jcl.BuildFullEventname(this), this.value);
  }, // onchange


  // ---- Methods -----
  init: function () {
    if ((this.eventname != null) && (this.eventname != "")) {
      OpenAjax.hub.subscribe(jcl.BuildFullEventname(this), this.handleEvent, this);
    } // if
  },


  // --- OpenAjax event handler ---
  handleEvent: function (eventName, eventData) {
    this.setData(eventData);
  }, // handleEvent


  // Create Option elements for this SELECT from a string
  CreateOptions: function (options, defaultOption) {
    var currentValue = this.value;
    this.length = 0;

    options = options.split(";");
    for (n = 0; n < options.length; n++) {
      var s = options[n];
      var o = document.createElement("OPTION");

      if (s.indexOf(':') < 0) {
        o.value = n + 1;
        o.innerHTML = s;
      } else {
        s = s.split(':');
        o.value = s[0];
        o.innerHTML = s[1];
      } // if
      this.appendChild(o);
      if ((o.value == currentValue) || (o.value == defaultOption))
        o.selected = true;
    } // for
    if (this.value != currentValue)
      OpenAjax.hub.publish(jcl.BuildFullEventname(this), this.value);
  }, // CreateOptions


  // --- data binding ---
  setData: function (v) {
    this.value = v;
  }, // setData


  getData: function () {
    return (this.value);
  } // getData

}; // SelectBehaviour
OpenAjax.hub.registerLibrary("SelectBehaviour", "http://www.mathertel.de/OpenAjax/SelectBehaviour");

This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.