修改表单WEBPART

闲来无事,便想到如何使用采用下拉列表<select>方式来筛选列表。

我的下来列表有两项部门和分室,但如何把部门和分室实现联动呢?在网上截取了一段联动的代码,经过简单修改,很容易就得已实现。

代码如下:
<script type="text/javascript">
var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

function addEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = fnHandler;
    }
};

function Each(arrList, fun){
    for (var i = 0, len = arrList.length; i < len; i++) { fun(arrList[i], i); }
};

function GetOption(val, txt) {
    var op = document.createElement("OPTION");
    op.value = val; op.innerHTML = txt;
    return op;
};

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

Object.extend = function(destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
    return destination;
}


var CascadeSelect = Class.create();
CascadeSelect.prototype = {
  //select集合,菜单对象
  initialize: function(arrSelects, arrMenu, options) {
    if(arrSelects.length <= 0 || arrMenu.lenght <= 0) return;//菜单对象
   
    var oThis = this;
   
    this.Selects = [];//select集合
    this.Menu = arrMenu;//菜单对象
   
    this.SetOptions(options);
   
    this.Default = this.options.Default || [];
   
    this.ShowEmpty = !!this.options.ShowEmpty;
    this.EmptyText = this.options.EmptyText.toString();
   
    //设置Selects集合和change事件
    Each(arrSelects, function(o, i){
        addEventHandler((oThis.Selects[i] = $(o)), "change", function(){ oThis.Set(i); });
    });
   
    this.ReSet();
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
        Default:    [],//默认值(按顺序)
        ShowEmpty:    false,//是否显示空值(位于第一个)
        EmptyText:    "请选择"//空值显示文本(ShowEmpty为true时有效)
    };
    Object.extend(this.options, options || {});
  },
  //初始化select
  ReSet: function() {
 
    this.SetSelect(this.Selects[0], this.Menu, this.Default.shift());
    this.Set(0);
  },
  //全部select设置
  Set: function(index) {
    var menu = this.Menu
   
    //第一个select不需要处理所以从1开始
    for(var i=1, len = this.Selects.length; i < len; i++){
        if(menu.length > 0){
            //获取菜单
            var value = this.Selects[i-1].value;
            if(value!=""){
                Each(menu, function(o){ if(o.val == value){ menu = o.menu || []; } });
            } else { menu = []; }
           
            //设置菜单
            if(i > index){ this.SetSelect(this.Selects[i], menu, this.Default.shift()); }
        } else {
            //没有数据
            this.SetSelect(this.Selects[i], [], "");
        }
    }
    //清空默认值
    this.Default.length = 0;
  },
  //select设置
  SetSelect: function(oSel, menu, value) {
    oSel.options.length = 0; oSel.disabled = false;
    if(this.ShowEmpty){ oSel.appendChild(GetOption("", this.EmptyText)); }
    if(menu.length <= 0){ oSel.disabled = true; return; }
   
    Each(menu, function(o){
        var op = GetOption(o.val, o.txt ? o.txt : o.val); op.selected = (value == op.value);
        oSel.appendChild(op);
    });   
 
   }
};


window.onload=function(){
   
    var menu = [
        {'val': '请选择', 'txt': '请选择'},
        {'val': '行政部', 'menu': [
            {'val': '办公室'},
            {'val': '人事室'},
            {'val': '质管室'},
            {'val': '总务室'}
        ]},
       {'val': '财务部', 'menu': [
            {'val': '财务室'},
            {'val': '会计室'},
            {'val': '库房'}
        ]}
    ];
   
    var sel=["sel1", "sel2"];
   
    var val=["3 ->", "3_1 ->", "3_1_2"];
   
    var cs = new CascadeSelect(sel, menu, { Default: val });
  
      
       
   }
</script>

<style type="text/css">
.sel select{ width:100px;}
</style>

 

<div class="sel" onkeydown="javascript:if (event.keyCode == 13) _SFSUBMIT_">
<select id="sel1" name='部门' ></select>
<select id="sel2" name='分室' ></select>

<input type="button" value="搜索" onclick="javascript:_SFSUBMIT_"/>
</div>

 

然后通过连接到列表,很容易就实现了。

效果如下:我连接的是两个任务:部门任务和分室任务

 

posted on 2008-09-18 16:27  无心阁  阅读(177)  评论(0编辑  收藏  举报

导航