首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

table js排序,该如何处理

2012-04-24 
table js排序一个table如下tabletrtd分数87/tdtd王军/td/trtrtd分数287/tdtd王军/

table js排序
一个table 如下
<table>
<tr><td>分数87</td><td>王军</td></tr>
<tr><td>分数287</td><td>王军</td></tr>
<tr><td>分数33</td><td>李四</td></tr>
<tr><td>分数47</td><td>张三</td></tr>
<tr><td>分数37</td><td>李四</td></tr>
<tr><td>分数86</td><td>王军</td></tr>
</table>

用js 按照名字排序 就是一个名字的挨一起 每行 怎么js排序呢?

[解决办法]
这个不是应该在后台就处理好排序吗
[解决办法]
function sortTable() {
alert(1);
var table = $('table');
var rows = table.find('tr').get();
rows.sort(function(a, b) {
var keyA = $.trim($(a).find('td:first').text()).match(/\d+/);
var keyB = $.trim($(b).find('td:first').text()).match(/\d+/);
if(keyA == "" && keyB != "") {
return -1;
}
if(keyA != "" && keyB == "") {
return 1;
}
if (parseInt(keyA) < parseInt(keyB)) {
return -1;
}
if (parseInt(keyA) > parseInt(keyB)) {
return 1;
}
return 0;
});
$.each(rows, function(index, row) {
table.append(row);
});
}

这个是用jquery实现的
[解决办法]
其实应该在后台处理好的,,,如果非要在前台jsp页面上,你可以给每条数据加上个id,,,然后js中得到数据进行比较,再进行排列。。。
很繁琐就是了。。。
[解决办法]
jquery 有现成的插件:http://tablesorter.com/docs/

JScript code
$("#tableId").tablesorter();//
[解决办法]
探讨

jquery 有现成的插件:http://tablesorter.com/docs/
JScript code

$("#tableId").tablesorter();//

[解决办法]
既然用js,为什么不用js里的grid呢。然后在对应的store里用group对姓名排序。
JScript code
groupField : name
[解决办法]
探讨

jquery 有现成的插件:http://tablesorter.com/docs/
JScript code

$("#tableId").tablesorter();//

[解决办法]
探讨

jquery 有现成的插件:http://tablesorter.com/docs/
JScript code

$("#tableId").tablesorter();//

[解决办法]
支持表头排序,希望可以帮到你

<table onclick="sortColumn(event)">
<thead>
<tr>
<td>分数</td>
<td>姓名</td>
</tr>
 </thead>
 <tbody>
<tr><td>分数87</td><td>王军</td></tr>
<tr><td>分数287</td><td>王军</td></tr>
<tr><td>分数33</td><td>李四</td></tr>
<tr><td>分数47</td><td>张三</td></tr>
<tr><td>分数37</td><td>李四</td></tr>
<tr><td>分数86</td><td>王军</td></tr>
</tbody>
</table>

<SCRIPT language="javascript">
var dom = (document.getElementsByTagName) ? true : false;
var ie5 = (document.getElementsByTagName && document.all) ? true : false;
var arrowUp, arrowDown;



if (ie5 || dom)
 initSortTable();

function initSortTable() {
 arrowUp = document.createElement("SPAN");
 var tArrowUp = document.createTextNode("5");
 arrowUp.appendChild(tArrowUp);
 arrowUp.className = "arrow";

 arrowDown = document.createElement("SPAN");
 var tArrowDown = document.createTextNode("6");
 arrowDown.appendChild(tArrowDown);
 arrowDown.className = "arrow";
}

function sortTable(tableNode, nCol, bDesc, sType) {
 var tBody = tableNode.tBodies[0];
 var trs = tBody.rows;
 var trl= trs.length;
 var a = new Array();
 
 for (var i = 0; i < trl; i++) {
a[i] = trs[i];
 }
 
 var start = new Date;
 window.status = "Sorting data...";
 a.sort(compareByColumn(nCol,bDesc,sType));
 window.status = "Sorting data done";
 
 for (var i = 0; i < trl; i++) {
tBody.appendChild(a[i]);
window.status = "Updating row " + (i + 1) + " of " + trl +
" (Time spent: " + (new Date - start) + "ms)";
 }
 
 // check for onsort
 if (typeof tableNode.onsort == "string")
tableNode.onsort = new Function("", tableNode.onsort);
 if (typeof tableNode.onsort == "function")
tableNode.onsort();
}

function CaseInsensitiveString(s) {
 return String(s).toUpperCase();
}

function parseDate(s) {
 return Date.parse(s.replace(/\-/g, '/'));
}

/* alternative to number function
 * This one is slower but can handle non numerical characters in
 * the string allow strings like the follow (as well as a lot more)
 * to be used:
 * "1,000,000"
 * "1 000 000"
 * "100cm"
 */

function toNumber(s) {
return Number(s.replace(/[^0-9\.]/g, ""));
}

function compareByColumn(nCol, bDescending, sType) {
 var c = nCol;
 var d = bDescending;
 
 var fTypeCast = String;
 
 if (sType == "Number")
fTypeCast = Number;
 else if (sType == "Date")
fTypeCast = parseDate;
 else if (sType == "CaseInsensitiveString")
fTypeCast = CaseInsensitiveString;

 return function (n1, n2) {
if (fTypeCast(getInnerText(n1.cells[c])) < fTypeCast(getInnerText(n2.cells[c])))
return d ? -1 : +1;
if (fTypeCast(getInnerText(n1.cells[c])) > fTypeCast(getInnerText(n2.cells[c])))
return d ? +1 : -1;
return 0;
 };
}

function sortColumnWithHold(e) {
 // find table element
 var el = ie5 ? e.srcElement : e.target;
 var table = getParent(el, "TABLE");
 
 // backup old cursor and onclick
 var oldCursor = table.style.cursor;
 var oldClick = table.onclick;
 
 // change cursor and onclick 
 table.style.cursor = "wait";
 table.onclick = null;
 
 // the event object is destroyed after this thread but we only need
 // the srcElement and/or the target
 var fakeEvent = {srcElement : e.srcElement, target : e.target};
 
 // call sortColumn in a new thread to allow the ui thread to be updated
 // with the cursor/onclick
 window.setTimeout(function () {
sortColumn(fakeEvent);
// once done resore cursor and onclick
table.style.cursor = oldCursor;
table.onclick = oldClick;
 }, 100);
}

function sortColumn(e) {


 var tmp = e.target ? e.target : e.srcElement;
 var tHeadParent = getParent(tmp, "THEAD");
 var el = getParent(tmp, "TD");

 if (tHeadParent == null)
return;
  
 if (el != null) {
var p = el.parentNode;
var i;

// typecast to Boolean
el._descending = !Boolean(el._descending);

if (tHeadParent.arrow != null) {
if (tHeadParent.arrow.parentNode != el) {
tHeadParent.arrow.parentNode._descending = null; //reset sort order
}
tHeadParent.arrow.parentNode.removeChild(tHeadParent.arrow);
}

if (el._descending)
tHeadParent.arrow = arrowUp.cloneNode(true);
else
tHeadParent.arrow = arrowDown.cloneNode(true);

el.appendChild(tHeadParent.arrow);

 

// get the index of the td
var cells = p.cells;
var l = cells.length;
for (i = 0; i < l; i++) {
if (cells[i] == el) break;
}

var table = getParent(el, "TABLE");
// can't fail
  
sortTable(table,i,el._descending, el.getAttribute("type"));
 }
}


function getInnerText(el) {
 if (ie5) return el.innerText; //Not needed but it is faster
 
 var str = "";
 
 var cs = el.childNodes;
 var l = cs.length;
 for (var i = 0; i < l; i++) {
switch (cs[i].nodeType) {
case 1: //ELEMENT_NODE
str += getInnerText(cs[i]);
break;
case 3: //TEXT_NODE
str += cs[i].nodeValue;
break;
}
  
 }
 
 return str;
}

function getParent(el, pTagName) {
 if (el == null) return null;
 else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) // Gecko bug, supposed to be uppercase
return el;
 else
return getParent(el.parentNode, pTagName);
}


</script>

热点排行