javascript-表格排序(降序/反序)實現介紹(附圖)

來源:互聯網
上載者:User

知識點:

Array方法:

sort:降序

reverse:反序

效果:

代碼: 複製代碼 代碼如下:<style>
*{
margin: 0;
padding: 0;
}
#tableSort{
line-height: 22px;
background: #ccc;
}
#tableSort thead,
#tableSort tfoot{
background: #066;
color: #fff;
font-size: 14px;
}
#tableSort thead a,
#tableSort tfoot a{
color: #fff;
}
#tableSort tfoot{
background: #069;
}
#tableSort tbody td{
background: #fff;
padding: 3px 5px;
font-size: 12px;
}
</style>
<table width="500" border="0" cellspacing="1" cellpadding="0" id="tableSort">
<thead>
<tr>
<th width="31" align="center"> </th>
<th width="104">商品名稱</th>
<th width="104">商品描述</th>
<th width="124" id="timeSort" _isDesc="false">上架時間</th>
<th width="82" id="priceSort" _isDesc="false">價格</th>
<th width="48">操作</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><input type="checkbox" name="checkbox3" id="checkbox2" _op="check" /></td>
<td>1</td>
<td>12312312313</td>
<td>2013-5-8</td>
<td>¥120</td>
<td align="center"><a href="javascript:void(0)" _op="del">刪除</a></td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="checkbox4" id="checkbox3" _op="check" /></td>
<td>2</td>
<td>頂戴</td>
<td>2013-5-12</td>
<td>¥140</td>
<td align="center"><a href="javascript:void(0)" _op="del">刪除</a></td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="checkbox5" id="checkbox4" _op="check" /></td>
<td>3</td>
<td>欠工</td>
<td>2013-4-8</td>
<td>¥320</td>
<td align="center"><a href="javascript:void(0)" _op="del">刪除</a></td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="checkbox6" id="checkbox5" _op="check" /></td>
<td>4</td>
<td>七七</td>
<td>2013-8-8</td>
<td>¥520</td>
<td align="center"><a href="javascript:void(0)" _op="del">刪除</a></td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="checkbox7" id="checkbox6" _op="check" /></td>
<td>5</td>
<td>苛</td>
<td>2013-5-25</td>
<td>¥820</td>
<td align="center"><a href="javascript:void(0)" _op="del">刪除</a></td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="checkbox8" id="checkbox7" _op="check" /></td>
<td>6</td>
<td>黃梅雨</td>
<td>2013-5-2</td>
<td>¥120</td>
<td align="center"><a href="javascript:void(0)" _op="del">刪除</a></td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="checkbox9" id="checkbox8" _op="check" /></td>
<td>7</td>
<td>工作服</td>
<td>2013-5-18</td>
<td>¥1220</td>
<td align="center"><a href="javascript:void(0)" _op="del">刪除</a></td>
</tr>
<tr>
<td align="center"><input type="checkbox" name="checkbox10" id="checkbox9" _op="check" /></td>
<td>8</td>
<td>地茜共</td>
<td>2013-3-8</td>
<td>¥1260</td>
<td align="center"><a href="javascript:void(0)" _op="del">刪除</a></td>
</tr>
</tbody>
<tfoot>
<tr>
<th align="center"><input type="checkbox" name="checkbox" id="checkAll" /></th>
<th align="left"><a href="javascript:void(0)" id="reserveCheck" >反選</a> <a href="javascript:void(0)" id="delSelect">刪除</a></th>
<th align="left"> </th>
<th> </th>
<th> </th>
<th><a href="#"></a></th>
</tr>
</tfoot>
</table>
<script>
//大量設定checked值
function setChecked(checkBoxs,checked){
for(var i=0,len=checkBoxs.length;i<len;i++){
checkBoxs[i].checked=checked;
}
}
//批量反置checked值
function reverseChecked(checkBoxs){
for(var i=0,len=checkBoxs.length;i<len;i++){
checkBoxs[i].checked=!checkBoxs[i].checked;
}
}
//移除tr值
function removeTr(tBody,tr){
tBody.removeChild(tr);
}
//擷取tr
function getParentTr(o){
while(o){
o=o.parentNode;
if(o&&o.tagName==="TR"){
return o;
}
}
}
//arrSort排序
function arrSort(arr,isDesc){
var arr=arr.sort(function(num1,num2){
return num1-num2;
});
if(isDesc){//desc
arr.reverse();
}
return arr;
}
//表格排序
function tableSort(tablePart,col,fun,isDesc){
var arrNum=[],trs={};

for(var i=0,len=tablePart.rows.length;i<len;i++){
var td=tablePart.rows[i].cells[col];
var num=fun(td);
arrNum.push(num);
trs["id"+num]=trs["id"+num]||[];
trs["id"+num].push(getParentTr(td));
}
arrNum=arrSort(arrNum,isDesc);
for(var j=0,jlen=arrNum.length;j<jlen;j++){
for(var k=0,klen=trs["id"+arrNum[j]].length;k<klen;k++){
var tr=trs["id"+arrNum[j]].pop();
tablePart.appendChild(tr);
}
}
}

var table=document.getElementById("tableSort");
var checkBoxs=table.tBodies[0].getElementsByTagName('input');
var checkAll=document.getElementById("checkAll");
var reserveCheck=document.getElementById("reserveCheck");
var delSelect=document.getElementById("delSelect");
var timeSort=document.getElementById("timeSort");
var priceSort=document.getElementById("priceSort");

checkAll.onclick=function(){
setChecked(checkBoxs,this.checked);
}

reserveCheck.onclick=function(){
reverseChecked(checkBoxs);
}
table.tBodies[0].onclick=function(e){
var ev=e||window.event;
var target=ev.target||ev.srcElement;
if(!target)return;
target._op=target.getAttribute("_op");
if(!target._op)return;

if(target._op==="check"&&target.type==="checkbox"&&!target.checked){
checkAll.checked=target.checked;
}
if(target._op==="del"){
var tr=getParentTr(target);
removeTr(table.tBodies[0],tr);
}
}
delSelect.onclick=function(){
var chk=[];
for(var i=0,len=checkBoxs.length;i<len;i++){
if(checkBoxs[i].checked){
var tr=getParentTr(checkBoxs[i]);
chk.push(tr);
}
}

for(var j=0,jlen=chk.length;j<jlen;j++){
removeTr(table.tBodies[0],chk[j]);
}
}
var sortMark="↑↓";
timeSort.onclick=function(){
this.isDesc=(this.isDesc===true)?false:true;
tableSort(table.tBodies[0],3,function(td){
return (new Date(td.innerHTML)).getTime();
},this.isDesc);
priceSort.innerHTML="價格"
this.innerHTML="上架時間"+sortMark[this.isDesc?1:0];
}
priceSort.onclick=function(){
this.isDesc=(this.isDesc===true)?false:true;
tableSort(table.tBodies[0],4,function(td){
return parseInt(td.innerHTML.replace("¥",''));
},this.isDesc);
timeSort.innerHTML="上架時間";
this.innerHTML="價格"+sortMark[this.isDesc?1:0];
}

</script>

總結:

完成了準系統,對於聯合排序沒有實現。後期會慢慢加入,有興趣的可以把代碼放到html頁面,運行查看效果。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.