
//指定表头索引
<PUBLIC:PROPERTY  NAME="headindex" ID="headindexid" GET="getHeadIndex" PUT="setHeadIndex" VALUE="0" />
//开始行
<PUBLIC:PROPERTY  NAME="begin"  ID="beginId" GET="getBegin" PUT="setBegin" VALUE="0" />
//结束行
<PUBLIC:PROPERTY  NAME="end"  ID="endId" GET="getEnd" PUT="setEnd" VALUE="0" />
//排序sort(index,order,datatype)
<PUBLIC:METHOD NAME="sort" />
//排序事件(event.sortIndex 排序列索引，event.sortOrder	排序列是升序还是降序，event.srcElement 触发此事件的表格对象)
<PUBLIC:EVENT NAME="onsort" ID="sortId" />
//单击事件
<PUBLIC:ATTACH EVENT="onclick" ONEVENT="doClick()" />
//双击事件
<PUBLIC:ATTACH EVENT="ondblclick"  ONEVENT="doClick()" />

<script language="JScript">
var headindex = -1;
var begin = 0;
var end = 0;

var sortIndex = -1;
var sortOrder = true;

//得到表头列的索引
function getHeadIndex() {
	return headindex;
}
//设置表头列的索引
function setHeadIndex(vValue) {
	vValue = parseInt(vValue);
	if (isNaN(vValue) || vValue < 0 || vValue >= element.rows.length) return;
	if (headindex == vValue) return;
	headindex = vValue;
	headindexid.fireChange();
}
//获取表格开始行
function getBegin() {
	return begin;
}
//设置表格开始行
function setBegin(vValue) {
	vValue = parseInt(vValue);
	if (isNaN(vValue) || vValue < 0 || vValue >= element.rows.length) {
		begin = 0;
	}
	else {
		begin = vValue;
	}
	beginId.fireChange();
}
//获取表格结束行
function getEnd() {
	return end;
}
//设置表格结束行
function setEnd(vValue) {
	vValue = parseInt(vValue);
	if (isNaN(vValue) || vValue < 0 || vValue >= element.rows.length) {
		end = 0;
	}
	else {
		end = vValue;
	}
	endId.fireChange();
}
//排序，colIndex－要排序的列的索引，order－true:正序，false:倒序
function sort(colIndex,order,dataType) {
	if (dataType == "none")	{
		return;
	}
	if (dataType != "String" && dataType != "int" && dataType != "float" && dataType != "datetime")	{
		dataType = "String";
	}

	var chg = null;
	var flag = true;	//是否进行过交换的标志
	var m = null;
	var n = null;
	var len = element.rows.length - 1 - end;
	for (var j=len;j>begin;j--) {
		flag = true;
		for (var k=begin;k<j;k++) {
			if (element.rows[k].cells[colIndex].getAttribute("value") == null) {
				m = element.rows[k].cells[colIndex].innerText;
			}
			else {
				m = element.rows[k].cells[colIndex].getAttribute("value");
			}
			if (element.rows[k+1].cells[colIndex].getAttribute("value") == null) {
				n = element.rows[k+1].cells[colIndex].innerText;
			}
			else {
				n = element.rows[k+1].cells[colIndex].getAttribute("value");
			}

			if (dataType == "int")	{
				m = parseInt(m);
				n = parseInt(n);
			}
			else if (dataType == "float")	{
				m = parseFloat(m);
				n = parseFloat(n);
			}
			else if (dataType == "datetime")	{
				m = parseInt(m);
				n = parseInt(n);
			}
			if (!order) {		//如果是倒序排列，则交换m和n
				var tt = m;
				m = n;
				n = tt;
			}
			if (m > n) {	//如果m > n 交换两个选项
				element.rows[k].insertAdjacentElement("beforeBegin",element.rows[k+1]);
				flag = false;		//交换过，设为false*/
			}
		}
		if (flag) break;		//如果一次都没有交还，则退出
	}
	sortIndex = colIndex;
	sortOrder = order;
	var oEvent = createEventObject();
	oEvent.sortIndex = sortIndex;
	oEvent.sortOrder = sortOrder;
	sortId.fire(oEvent);
}
//点击事件
function doClick() {
	var oTable = element;
	oE = window.event.srcElement;
	//找出点击的单元
	while (oE.tagName != "TD" || (oE.parentElement.parentElement != oTable && oE.parentElement.parentElement.parentElement != oTable))	{
		if (oE.tagName == "BODY") return;
		oE = oE.parentElement;
	}
	var oTd = oE;
	//如果点击的行不是表头索引行，返回
	if (oTd.parentElement.rowIndex != headindex) return;
	var colIndex = -1;
	var order = true;
	var datatype = "String";
	//得到单元索引
	for (var i=0;i<oE.parentElement.cells.length;i++) {
		if (oE == oE.parentElement.cells[i]) {
			colIndex = i;
			break;
		}
	}
	if (colIndex == sortIndex) {
		order = ! sortOrder;
	}
	//找到排序类型
	if (element.rows[headindex].cells[colIndex].getAttribute("data-type") != null) {
		datatype = element.rows[headindex].cells[colIndex].getAttribute("data-type");
	}
	//排序
	sort(colIndex,order,datatype);
}
</script>