function CategoryTable(Table)
{
this.Table=Table;
this.body=document.createElement("TBODY");
this.Table.appendChild(this.body);
this.Table.border ="1";
this.width="190px";
this.categoryColor="gray";
this.nameColor="silver";
this.valueColor="silver";
}
CategoryTable.prototype.AddCategory=function(caption,text)
{
var category=document.createElement("tr");
this.body.appendChild(category);
var name=document.createElement("td");
var value=document.createElement("td");
category.appendChild(name);
category.appendChild(value);
category.width=this.width;
name.width="50%";
with(value)
{
width="50%";
innerText=text;
}
category.bgColor=this.categoryColor;
name.innerText=caption;
category.tag="category";
return category;
}
CategoryTable.prototype.AddField=function(category,caption,value)
{
if(null==category)
{
throw "Parent Category Null Reference!";
}
if(category.tag!="category")
{
throw "Invalid category!";
}
var field=document.createElement("tr");
var objName=document.createElement("td");
var objValue=document.createElement("td");
objValue.bgColor=this.valueColor;
objName.bgColor=this.nameColor;
objValue.innerText=value;
objName.innerText=caption;
field.appendChild(objName);
field.appendChild(objValue);
field.tag="field";
var sibling;
if((sibling=category.nextSibling)==null)
{
this.body.appendChild(field);
}
else
{
this.body.insertBefore(field,sibling);
}
return field;
}
CategoryTable.prototype.RemoveField=function(field)
{
if(field==null)
{
throw "Null reference!";
}
if(field.tag!="field")
{
throw "Invalid field!";
}
this.body.removeChild(field);
}
CategoryTable.prototype.RemoveCategory=function(category)
{
if(category==null)
if(category.tag!="category")
{
throw "Invalid category!";
}
if(this.body.children.length<=1)
{
throw "At least one field or category must exists!";
}
var sibling=category.nextSibling;
var tmp;
while(sibling!=null)
{
if((sibling.tag=="category")||this.body.children.length<=1)
return;
tmp=sibling;
sibling=sibling.nextSibling;
this.body.removeChild(tmp);
}
}
演示代码:
var cateList=new CategoryTable(DataGrid);
var cate1=cateList.AddCategory("Hello2","f");
var category=cateList.AddCategory("Hello","f");
var field=cateList.AddField(category,"Name 1","Value");
var field=cateList.AddField(cate1,"Name 2","Value");
var field=cateList.AddField(cate1,"Name 3","Value");
// cateList.RemoveCategory(category);
你在创建实例的时候是直接绑定到某个Table上的,这样Table的样式你可以自己来弄了,这个CategoryTable有下面几个方法:
function AddCategory(caption,text) 返回一个Category
function AddField(category,caption,value) 返回一个分类下的field
function RemoveCategory(category) 删除一个分类
function RemoveField(field) 删除一个分类的field