//当改变下拉列表框项目框展开收起时
function CDropDownList_OnChngListBoard(sender)
{
if (sender == "documentBody")
{
//事件来源于 document.body
if (this.skipDocumentBodyEvent || this.listBoard.style.display == "none")
{
//如果需要跳过此事件(controller 也触发了该事件就该跳过)
//或者 listBoard 本来就没有展开
return;
}
}
if (this.listBoard.style.display == "none")
{
//已经收起,展开之
//确定展开位置
var x = this.targetAsst.offsetLeft;
var y = this.targetAsst.offsetTop + this.targetAsst.offsetHeight;
var pe = this.targetAsst.offsetParent;
while (pe)
{
x += pe.offsetLeft;
y += pe.offsetTop;
pe = pe.offsetParent;
}
this.listBoard.style.position = "absolute";
this.listBoard.style.left = x + "px";
this.listBoard.style.top = y + "px";
this.listBoard.style.display = "block";
//根据项目索引创建项目的 ID 值
function CDropDownList_CreateItemID(itemIndex)
{
return this.target.id + "_item_" + itemIndex;
}
//追加一个项目
function CDropDownList_AddItem(itemText, itemValue)
{
var newItemIndex = this.itemArr.length;
this.itemArr[newItemIndex] = new CDropDownList_CItem(itemText, itemValue);
this.AddItemFromItemData(newItemIndex);
}
//将 itemArr 中的一项数据添加到下拉列表框项目框中
//此函数不应该公开,否则可能造成实际数据与显示的不一致
function CDropDownList_AddItemFromItemData(itemIndex)
{
var itemText = this.itemArr[itemIndex].itemText;
var itemValue = this.itemArr[itemIndex].itemValue;
if (this.listBoardAsst)
{
var li = document.createElement("div");
li.style.cursor = "default";
li.style.width = "100%";
li.className = DDLIS_UNSELECTED_CLASSNAME;
li.id = this.CreateItemID(itemIndex);
var t = document.createTextNode(itemText);
li.appendChild(t);
this.listBoardAsst.appendChild(li);
}
}
//修改一个项目
function CDropDownList_ModifyItem(itemIndex, newItemText, newItemValue)
{
if (itemIndex < 0 || itemIndex >= this.itemArr.length)
{
return;
}
var li = document.getElementById(this.CreateItemID(itemIndex)); //要修改的项目对象
this.ClearChildNodes(li);
var t = document.createTextNode(newItemText);
li.appendChild(t);
this.itemArr[itemIndex].itemText = newItemText;
this.itemArr[itemIndex].itemValue = newItemValue;
if (itemIndex == this.GetSelectedItemIndex())
{
//修改了的项目就是已经选中了的项目,要重新选中,以便更新显示选中项目的文字
this.SelectItem(itemIndex);
}
}
//删除一个项目
function CDropDownList_DeleteItem(itemIndex)
{
if (itemIndex < 0 || itemIndex >= this.itemArr.length)
{
return;
}
this.itemArr.splice(itemIndex, 1);
//由于下拉列表框项目框中的项目是与 this.itemArr 的索引相关的
//而删除数组会引起索引的改变,故重新生成下拉列表框中的项目
this.ClearChildNodes(this.listBoardAsst);
for (var i = 0; i < this.itemArr.length; i++)
{
this.AddItemFromItemData(i);
}
if (itemIndex < this.GetSelectedItemIndex())
{
//删除的项目是在已经选中的项目的前面
this.SelectItem(this.GetSelectedItemIndex() - 1);
}
else if (itemIndex == this.GetSelectedItemIndex())
{
//删除的项目就是已经选中的项目
this.SelectItem(-1);
}
}
//选中一个项目
function CDropDownList_SelectItem(itemIndex)
{
if (itemIndex < 0 || itemIndex >= this.itemArr.length)
{
this.ClearChildNodes(this.titleBox);
this.titleBox.appendChild(document.createTextNode(""));
this.selectedItemIndex = -1;
}
else
{
this.ClearChildNodes(this.titleBox);
var t = document.createTextNode(this.itemArr[itemIndex].itemText);
this.titleBox.appendChild(t);
this.selectedItemIndex = itemIndex;
}
}
//返回选中项目的索引
function CDropDownList_GetSelectedItemIndex()
{
return this.selectedItemIndex;
}
//返回选中项目的文字
function CDropDownList_GetSelectedItemText()
{
if (this.selectedItemIndex < 0 || this.selectedItemIndex >= this.itemArr.length)
{
return null;
}