﻿function JsDropDown(selectId )
{
    if( selectId.tagName != "undefined" && selectId.tagName != null && selectId.tagName == "SELECT" )
        this.select = selectId;
    else
        this.select = document.getElementById(selectId);
    
    this.onBind = null; //数据绑定后触发
    this.dataSource = [];
};

JsDropDown.prototype.dataBind = function(value,text)
{
    for(var i=0;i<this.select.options.length;i++)
    {
        var option = this.select.options[i];
        if(option.value!="0"&&option.nodeName!="#text")
        {
            this.select.removeChild(option);
            i--;
        }
    }
    if(this.dataSource == null) //没有数据，返回
        return;
     for(var i=0;i<this.dataSource.length;i++)
     {
        var newoption = new Option();
        var len = value.split(".").length;
       
        switch( len )
        {
            case 1:
                  newoption.text =  this.dataSource[i][text]; 
                  newoption.value =  this.dataSource[i][value];
                break;
                
            case 2:
                newoption.text = this.dataSource[i][text.split(".")[0]][text.split(".")[1]]; 
                newoption.value = this.dataSource[i][value.split(".")[0]][value.split(".")[1]];
                break;
                
            case 3:
                newoption.text= this.dataSource[i][text.split(".")[0]][text.split(".")[1]][text.split(".")[2]];
                newoption.value= this.dataSource[i][value.split(".")[0]][value.split(".")[1][text.split(".")[2]]];
                break;
        }
        this.select.options.add(newoption);
     }
};

JsDropDown.prototype.GetSelected = function(id)
{ 
        for(var i = 0;i<this.select.options.length;i++)
        {
            var option = this.select.options[i]; 
            if(option.value == id)
            {
               option.setAttribute("selected","selected");
               return;  
            }
        }
}
