最近要對(duì)報(bào)表數(shù)組進(jìn)行排序,在前端處理如下:
首先在前臺(tái)頁(yè)面加載時(shí)對(duì)每行tr添加自定義的屬性data-sort-field-ftime,屬性值為要排序的字段的值(我的是數(shù)字比較方便):
$.each(jsonarray, function(i, obj) { troptions += "<tr data-sort-field-ftime=/""+obj.paiming+"/">"; troptions += "<td>"+(Number(obj.cdsPrem)/unitnow).toFixed(dotnow)+"</td>"; troptions += "<td>"+(Number(obj.cdjPrem)/unitnow).toFixed(dotnow)+"</td>"; troptions += "<td>"+(Number(obj.sumPrem)/unitnow).toFixed(dotnow)+"</td>"; troptions += "<td>"+obj.paiming+"</td>"; troptions += "</tr>"; });在要排序的表頭添加onchange事件,以下為onchange事件:
//排序處理 function changepm(){ var sortType=$("#pm").val(); var $trList = $("#ta tbody > tr");//獲取現(xiàn)有tr對(duì)象 //冒泡排序 for (var i = 0; i < $trList.length - 1; i++) { for (var j = 0; j < $trList.length - 1 - i; j++) { var value1 = parseInt($trList[j].attributes["data-sort-field-ftime"].nodeValue); var value2 = parseInt($trList[j + 1].attributes["data-sort-field-ftime"].nodeValue); if (sortType === "asc" ? value1 > value2 : value1 < value2) { var $temp = $trList[j]; $trList[j] = null; $trList[j] = $trList[j + 1]; $trList[j + 1] = null; $trList[j + 1] = $temp; } } } //返回排序后的tr集合 //將原來的tr清空,再將排序后的tr插入到table的dom中 console.log($trList); $trList.appendTo($("#ta > tbody").empty()); }以上就是jquery對(duì)table做排序操作的詳細(xì)內(nèi)容啦,希望對(duì)大家有所幫助,也希望大家繼續(xù)支持錯(cuò)新站長(zhǎng)站~
新聞熱點(diǎn)
疑難解答
圖片精選