
已解决问题
谷歌adnazbp用户在2016.04.20提交了关于“国考j**ascript-表格排序(降序/反序)实现介绍(附图)”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2025-02-27T04:20:07。希望大家能够帮助她。详细问题描述及疑问:期待您的答案,千言万语,表达不了我的感激之情,我已铭记在心 !
详细问题描述及疑问:期待您的答案,千言万语,表达不了我的感激之情,我已铭记在心 !
知识点:
Array方法:
sort:降序
rever
效果:
代码:
复制代码代码如
<style>
*{
}
#tableSort{
line-height:22px;
back
}
#tableSortthead,
#tableSorttfoot{
backgrou
color:#fff;
fo
}
#
#tableSorttfoota{
color:#f
}
#tableS
backg
}
#tableSort
background:#fff;
padding:**x**x;
font-size:12px;
}
</styl
<tablewidth="500"border="0"c
<thead>
<tr>
<thwidth="31"align=
<t
<thwidth="104">商品描述</th>
<t
<thwidth="
<thwidth="48">操作</th>
</t
</thead>
<tbody>
<t
<tdalign="ce
<td>1</
<td>12312312313</td>
<td>2013-
<td>¥120</t
<tdalign="center"><ahr
</tr>
<tr>
<tdalig
<t
<td>顶戴</td>
<td>2013-5-12</td>
<td>¥140</td>
<tdalign="
</tr>
<tr>
<tdalign="center"><inputtype="checkbox"name="checkbox5"id="checkbox4"_op="check"/></td>
<td>3</td>
<td>欠工</td>
<td>2013-4-8</td>
<td>¥320</td>
<tdalign="center"><ahref="j**ascript:void(0)"_op="del">删除</a></td>
</tr>
<tr>
<tdalign="center"><inputtype="checkbox"name="checkbox6"id="checkbox5"_op="check"/></td>
<td>4</td>
<td>七七</td>
<td>2013-8-8</td>
<td>¥520</td>
<tdalign="center"><ahref="j**ascript:void(0)"_op="del">删除</a></td>
</tr>
<tr>
<tdalign="center"><inputtype="checkbox"name="checkbox7"id="checkbox6"_op="check"/></td>
<td>5</td>
<td>苛</td>
<td>2013-5-25</td>
<td>¥820</td>
<tdalign="center"><ahref="j**ascript:void(0)"_op="del">删除</a></td>
</tr>
<tr>
<tdalign="center"><inputtype="checkbox"name="checkbox8"id="checkbox7"_op="check"/></td>
<td>6</td>
<td>黄梅雨</td>
<td>2013-5-2</td>
<td>¥120</td>
<tdalign="center"><ahref="j**ascript:void(0)"_op="del">删除</a></td>
</tr>
<tr>
<tdalign="center"><inputtype="checkbox"name="checkbox9"id="checkbox8"_op="check"/></td>
<td>7</td>
<td>工作服</td>
<td>2013-5-18</td>
<td>¥1220</td>
<tdalign="center"><ahref="j**ascript:void(0)"_op="del">删除</a></td>
</tr>
<tr>
<tdalign="center"><inputtype="checkbox"name="checkbox10"id="checkbox9"_op="check"/></td>
<td>8</td>
<td>地茜**</td>
<td>2013-3-8</td>
<td>¥1260</td>
<tdalign="center"><ahref="j**ascript:void(0)"_op="del">删除</a></td>
</tr>
</tbody>
<tfoot>
<tr>
<thalign="center"><inputtype="checkbox"name="checkbox"id="checkAll"/></th>
<thalign="left"><ahref="j**ascript:void(0)"id="reserveCheck">反选</a><ahref="j**ascript:void(0)"id="delSelect">删除</a></th>
<thalign="left"></th>
<th></th>
<th></th>
<th><ahref="#"></a></th>
</tr>
</tfoot>
</table>
<script>
//批量设置checked值
functionsetChecked(checkBoxs,checked){
for(vari=0,len=checkBoxs.length;i<len;i++){
checkBoxs[i].checked=checked;
}
}
//批量反置checked值
functionreverseChecked(checkBoxs){
for(vari=0,len=checkBoxs.length;i<len;i++){
checkBoxs[i].checked=!checkBoxs[i].checked;
}
}
//移除tr值
functionremoveTr(tBody,tr){
tBody.removeChild(tr);
}
//获取tr
functiongetParentTr(o){
while(o){
o=o.parentNode;
if(o&&o.tagName==="TR"){
returno;
}
}
}
//arrSort排序
functionarrSort(arr,isDesc){
vararr=arr.sort(function(**m1,**m2){
return**m1-**m2;
});
if(isDesc){//desc
arr.reverse();
}
returnarr;
}
//表格排序
functiontableSort(tablePart,col,fun,isDesc){
vararrNum=[],trs={};
for(vari=0,len=tablePart.rows.length;i<len;i++){
vartd=tablePart.rows[i].cells[col];
var**m=fun(td);
arrNum.push(**m);
trs["id"+**m]=trs["id"+**m]||[];
trs["id"+**m].push(getParentTr(td));
}
arrNum=arrSort(arrNum,isDesc);
for(varj=0,jlen=arrNum.length;j<jlen;j++){
for(vark=0,klen=trs["id"+arrNum[j]].length;k<klen;k++){
vartr=trs["id"+arrNum[j]].pop();
tablePart.appendChild(tr);
}
}
}
vartable=document.getElementById("tableSort");
varcheckBoxs=table.tBodies[0].getElementsByTagName('input');
varcheckAll=document.getElementById("checkAll");
varreserveCheck=document.getElementById("reserveCheck");
vardelSelect=document.getElementById("delSelect");
vartimeSort=document.getElementById("timeSort");
varpriceSort=document.getElementById("priceSort");
checkAll.onclick=function(){
setChecked(checkBoxs,this.checked);
}
reserveCheck.onclick=function(){
reverseChecked(checkBoxs);
}
table.tBodies[0].onclick=function(e){
varev=e||window.event;
vartarget=***.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"){
vartr=getParentTr(target);
removeTr(table.tBodies[0],tr);
}
}
delSelect.onclick=function(){
varchk=[];
for(vari=0,len=checkBoxs.length;i<len;i++){
if(checkBoxs[i].checked){
vartr=getParentTr(checkBoxs[i]);
chk.push(tr);
}
}
for(varj=0,jlen=chk.length;j<jlen;j++){
removeTr(table.tBodies[0],chk[j]);
}
}
varsortMark="↑↓";
timeSort.onclick=function(){
this.isDesc=(this.isDesc===true)?false:true;
tableSort(table.tBodies[0],3,function(td){
return(newDate(td.innerh**TML)).getTime();
},this.isDesc);
priceSort.innerh**TML="价格"
this.innerh**TML="上架时间"+sortMark[this.isDesc?1:0];
}
priceSort.onclick=function(){
this.isDesc=(this.isDesc===true)?false:true;
tableSort(table.tBodies[0],4,function(td){
returnparseInt(td.innerh**TML.replace("¥",''));
},this.isDesc);
timeSort.innerh**TML="上架时间";
this.innerh**TML="价格"+sortMark[this.isDesc?1:0];
}
</script>
总结:
完成了基本功能,对于联合排序没有实现。后期会慢慢加入,有兴趣的可以把代码放到html页面,运行查看效果。