设为首页
加入收藏
帮助中心
首页 | 红盾通告 | 信息中心 | ASP技术 | 数据库 | 网页设计 | 网管专栏 | OICQ攻略 | 墨客频道 | 网站运营 |
当前位置:首页 >> 网页设计 >> Javascript >> 正文
最新信息
·Javascript处理数字的几个…
·判断当前网页是否为本站页…
·鼠标指上图片替换后出现下…
·逍遥狼无图片智能圆角
·javascript获取当前网页文…
·用javascript获得地址栏参…
·JavaScript常用判断函数
·双击鼠标滚动屏幕的代码
·web应用漏洞扫描工具Jikto…
·常备JS操作
资料搜索
热点信息
·js获取图片尺寸
·怎么让弹出窗口最大化
·web应用漏洞扫描工具Jikto…
·判断radio多个单选按纽中是…
·如何用文字做一个只刷新验…
·防止F5刷新执行onunload事…
·Javascript获取IE浏览器窗…
·关闭IE时调用onbeforeunlo…
·js表单提交验证
·javascript获取当前网页文…
推荐信息
·显示日期方式
·添加到收藏夹
·终极应用--弹出的窗口之Co…
·防止被人frame
·web应用漏洞扫描工具Jikto…
·JavaScript常用的一些验证…
·关闭IE时调用onbeforeunlo…
·js表单提交验证
·自己综合其他资料写的一套…
·window.open具体参数


Google
 
排序表格
〖编辑:Cloudy | 浏览:人次〗

<html>
<head>
<title>排序列表</title>
<script language = "javascript">

function SortableTable(oTable, oSortTypes) {

this.element = oTable;
this.tHead = oTable.tHead;
this.tBody = oTable.tBodies[0];
this.document = oTable.ownerDocument || oTable.document;

this.sortColumn = null;
this.descending = null;

var oThis = this;
this._headeronclick = function (e) {
oThis.headeronclick(e);
};


// only IE needs this
var win = this.document.defaultView || this.document.parentWindow;
this._onunload = function () {
oThis.destroy();
};
if (win && typeof win.attachEvent != "undefined") {
win.attachEvent("onunload", this._onunload);
}

this.initHeader(oSortTypes || []);
}

SortableTable.gecko = navigator.product == "Gecko";
SortableTable.msie = /msie/i.test(navigator.userAgent);
// Mozilla is faster when doing the DOM manipulations on
// an orphaned element. MSIE is not
SortableTable.removeBeforeSort = SortableTable.gecko;

SortableTable.prototype.onsort = function () {};

// adds arrow containers and events
// also binds sort type to the header cells so that reordering columns does
// not break the sort types
SortableTable.prototype.initHeader = function (oSortTypes) {
var cells = this.tHead.rows[0].cells;
var l = cells.length;
var img, c;
for (var i = 0; i < l; i++) {
c = cells[i];
img = this.document.createElement("IMG");
img.src = "images/blank.png";
c.appendChild(img);
if (oSortTypes[i] != null) {
c._sortType = oSortTypes[i];
}
if (typeof c.addEventListener != "undefined")
c.addEventListener("click", this._headeronclick, false);
else if (typeof c.attachEvent != "undefined")
c.attachEvent("onclick", this._headeronclick);
}
this.updateHeaderArrows();
};

// remove arrows and events
SortableTable.prototype.uninitHeader = function () {
var cells = this.tHead.rows[0].cells;
var l = cells.length;
var c;
for (var i = 0; i < l; i++) {
c = cells[i];
c.removeChild(c.lastChild);
if (typeof c.removeEventListener != "undefined")
c.removeEventListener("click", this._headeronclick, false);
else if (typeof c.detachEvent != "undefined")
c.detachEvent("onclick", this._headeronclick);
}
};

SortableTable.prototype.updateHeaderArrows = function () {
var cells = this.tHead.rows[0].cells;
var l = cells.length;
var img;
for (var i = 0; i < l; i++) {
img = cells[i].lastChild;
if (i == this.sortColumn)
img.className = "sort-arrow " + (this.descending ? "descending" : "ascending");
else
img.className = "sort-arrow";
}
};

SortableTable.prototype.headeronclick = function (e) {
// find TD element
var el = e.target || e.srcElement;
while (el.tagName != "TD")
el = el.parentNode;

this.sort(SortableTable.msie ? SortableTable.getCellIndex(el) : el.cellIndex);
};

// IE returns wrong cellIndex when columns are hidden
SortableTable.getCellIndex = function (oTd) {
var cells = oTd.parentNode.childNodes
var l = cells.length;
var i;
for (i = 0; cells[i] != oTd && i < l; i++)
;
return i;
};

SortableTable.prototype.getSortType = function (nColumn) {
var cell = this.tHead.rows[0].cells[nColumn];
var val = cell._sortType;
if (val != "")
return val;
return "String";
};

// only nColumn is required
// if bDescending is left out the old value is taken into account
// if sSortType is left out the sort type is found from the sortTypes array

SortableTable.prototype.sort = function (nColumn, bDescending, sSortType) {
if (sSortType == null)
sSortType = this.getSortType(nColumn);

// exit if None
if (sSortType == "None")
return;

if (bDescending == null) {
if (this.sortColumn != nColumn)
this.descending = true;
else
this.descending = !this.descending;
}

this.sortColumn = nColumn;

if (typeof this.onbeforesort == "function")
this.onbeforesort();

var f = this.getSortFunction(sSortType, nColumn);
var a = this.getCache(sSortType, nColumn);
var tBody = this.tBody;

a.sort(f);

if (this.descending)
a.reverse();

if (SortableTable.removeBeforeSort) {
// remove from doc
var nextSibling = tBody.nextSibling;
var p = tBody.parentNode;
p.removeChild(tBody);
}

// insert in the new order
var l = a.length;
for (var i = 0; i < l; i++)
tBody.appendChild(a[i].element);

if (SortableTable.removeBeforeSort) {
// insert into doc
p.insertBefore(tBody, nextSibling);
}

this.updateHeaderArrows();

this.destroyCache(a);

if (typeof this.onsort == "function")
this.onsort();
};

SortableTable.prototype.asyncSort = function (nColumn, bDescending, sSortType) {
var oThis = this;
this._asyncsort = function () {
oThis.sort(nColumn, bDescending, sSortType);
};
window.setTimeout(this._asyncsort, 1);
};

SortableTable.prototype.getCache = function (sType, nColumn) {
var rows = this.tBody.rows;
var l = rows.length;
var a = new Array(l);
var r;
for (var i = 0; i < l; i++) {
r = rows[i];
a[i] = {
value: this.getRowvalue(r, sType, nColumn),
element: r
};
};
return a;
};

SortableTable.prototype.destroyCache = function (oArray) {
var l = oArray.length;
for (var i = 0; i < l; i++) {
oArray[i].value = null;
oArray[i].element = null;
oArray[i] = null;
}
}

SortableTable.prototype.getRowvalue = function (oRow, sType, nColumn) {
var s;
var c = oRow.cells[nColumn];
if (typeof c.innerText != "undefined")
s = c.innerText;
else
s = SortableTable.getInnerText(c);
return this.getvalueFromString(s, sType);
};

SortableTable.getInnerText = function (oNode) {
var s = "";
var cs = oNode.childNodes;
var l = cs.length;
for (var i = 0; i < l; i++) {
switch (cs[i].nodeType) {
case 1: //ELEMENT_NODE
s += SortableTable.getInnerText(cs[i]);
break;
case 3: //TEXT_NODE
s += cs[i].nodevalue;
break;
}
}
return s;
}

SortableTable.prototype.getvalueFromString = function (sText, sType) {
switch (sType) {
case "Number":
return Number(sText);
case "CaseInsensitiveString":
return sText.toUpperCase();
case "Date":
var parts = sText.split("-");
var d = new Date(0);
d.setFullYear(parts[0]);
d.setDate(parts[2]);
d.setMonth(parts[1] - 1);
return d.valueOf();
}
return sText;
};

SortableTable.prototype.getSortFunction = function (sType, nColumn) {
return function compare(n1, n2) {
if (n1.value < n2.value)
return -1;
if (n2.value < n1.value)
return 1;
return 0;
};
};

SortableTable.prototype.destroy = function () {
this.uninitHeader();
var win = this.document.parentWindow;
if (win && typeof win.detachEvent != "undefined") { // only IE needs this
win.detachEvent("onunload", this._onunload);
}
this._onunload = null;
this.element = null;
this.tHead = null;
this.tBody = null;
this.document = null;
this._headeronclick = null;
this.sortTypes = null;
this._asyncsort = null;
this.onsort = null;
};
</script>
<style type="text/css">
.sort-table {
font: Icon;
border: 1px Solid ThreeDShadow;
background: Window;
color: WindowText;
}

.sort-table thead {
background: ButtonFace;
}

.sort-table td {
padding: 2px 5px;
}

.sort-table thead td {
border: 1px solid;
border-color: ButtonHighlight ButtonShadow
ButtonShadow ButtonHighlight;
cursor: default;
}

.sort-table thead td:active {
border-color: ButtonShadow ButtonHighlight
ButtonHighlight ButtonShadow;
padding: 3px 4px 1px 6px;
}

.sort-arrow {
width: 11px;
height: 11px;
background-position: center center;
background-repeat: no-repeat;
margin: 0 2px;
}

.sort-arrow.descending {
background-image: url("http://www.lshdic.com/images/downsimple.png";);

}

.sort-arrow.ascending {
background-image: url("http://www.lshdic.com/images/upsimple.png";);
}
body {
font-family: Verdana, Helvetica, Arial, Sans-Serif;
font: Message-Box;
}

code {
font-size: 1em;
}

</style>
</head>
<body>
点击表头排序


<center><h1>排序表格</h1></center>
<table border = '1' class="sort-table" id="table-1" cellspacing="0" width = '100%'>
<col />
<col />
<col style="text-align: right" />
<col />
<thead>
<tr align = "center">
<td>选择</td>
<td>字符串</td>
<td title="CaseInsensitiveString">字符串</td>
<td>数字</td>
<td>日期</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type = "checkbox"></td>
<td><a href = "www">apple</a></td>
<td>Strawberry</td>
<td>45</td>
<td>2001-03-13</td>
</tr>
<tr>
<td><input type = "checkbox"></td>
<td>Banana</td>
<td>orange</td>
<td></td>
<td>1789-07-14</td>
</tr>
<tr>
<td><input type = "checkbox"></td>
<td>orange</td>
<td>Banana</td>
<td>4546</td>
<td>1949-07-04</td>
</tr>
<tr>
<td><input type = "checkbox"></td>
<td>Strawberry</td>
<td>apple</td>
<td>987</td>
<td>1975-08-19</td>
</tr>
<tr>
<td><input type = "checkbox"></td>
<td>Pear</td>
<td>blueberry</td>
<td>98743</td>
<td>2001-01-01</td>
</tr>
<tr>
<td><input type = "checkbox"></td>
<td>blueberry</td>
<td>Pear</td>
<td>4</td>
<td>2001-04-18</td>
</tr>
</tbody>
</table>

<script type="text/javascript">

var st1 = new SortableTable(document.getElementById("table-1"),
["None", "String", "CaseInsensitiveString", "Number", "Date", "None"]);

</script>
</body>
</html>


录入时间:2006-05-06 11:48:14 [打印本页] [关闭窗口] [返回顶部]
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。

Copyright © 2006-2014 0733168.Com Inc All Rights Reserved
关于我们 | 广告合作 | 联系我们 | 法律声明 | 友情链接 | 意见反馈
本站所收录信息、社区话题、及本站所做之广告均属其个人行为,与本站立场无关
湘ICP备06008436号