最近学的用javascript给datagrid排序

比较简单的排序方法,对于小型的排序,在客户端完成就可,麻烦客户端,以来刷新麻烦来回传数据,二来写代码也容易出错.
调用方法:给datagrid的表头的各个列标题添加onclick事件,然后调用sortTable('tblId', colIndex),分别传入要排序的表或datagrid的id号,要排序的列的索引 例如
<asp:BoundColumn DataField="gb_id" HeaderText="&lt;div οnclick=&quot;sortTable('dgResult', 0)&quot;&gt;ID&lt;/div&gt;"></asp:BoundColumn>

 

  1 None.gif function  compareMutiTRs(colIndex)
  2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  3InBlock.gif    return function compareTRs(tr1, tr2)
  4ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
  5InBlock.gif                        var value1;
  6InBlock.gif                        var value2;
  7InBlock.gif                        //必须得考虑到 合并单元格的问题 
  8InBlock.gif                        var isNull = (tr2.cells[colIndex]== null || tr1.cells[colIndex] ==  null)
  9InBlock.gif                        if (isNull)
 10ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 11InBlock.gif                                value1 = tr1.cells[0].firstChild.nodeValue;
 12InBlock.gif                                value2 = tr2.cells[0].firstChild.nodeValue;
 13ExpandedSubBlockEnd.gif                       }

 14InBlock.gif                       else
 15ExpandedSubBlockStart.gifContractedSubBlock.gif                       dot.gif
 16InBlock.gif                               value1 = tr1.cells[colIndex].firstChild.nodeValue;
 17InBlock.gif                               value2 = tr2.cells[colIndex].firstChild.nodeValue;
 18ExpandedSubBlockEnd.gif                       }

 19InBlock.gif                        
 20InBlock.gif                        if (value1 > value2)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 22InBlock.gif                            return 1;
 23ExpandedSubBlockEnd.gif                        }

 24InBlock.gif                        else if (value1 < value2)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 26InBlock.gif                            return -1;
 27ExpandedSubBlockEnd.gif                        }

 28InBlock.gif                        else
 29ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
 30InBlock.gif                            return 0;
 31ExpandedSubBlockEnd.gif                        }

 32ExpandedSubBlockEnd.gif             }

 33ExpandedBlockEnd.gif}

 34 None.gif
 35 None.gif var  lastCol  =   - 1 ;
 36 None.gif function  sortTable(tblId, colIndex)
 37 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 38InBlock.gif    var tbl = document.getElementById(tblId);
 39InBlock.gif    var trs = tbl.rows;
 40InBlock.gif    var arrayTRs = new Array();
 41InBlock.gif    var lastTR = null;
 42InBlock.gif    var start;
 43InBlock.gif    var end;
 44InBlock.gif    
 45InBlock.gif    var flagUp = trs[0].cells.length == 1;
 46InBlock.gif    var flagDown = trs[trs.length - 1].cells.length == 1
 47InBlock.gif    if (flagUp)//如果分页提示在上
 48ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 49InBlock.gif            start = 2;
 50InBlock.gif            end = trs.length;
 51ExpandedSubBlockEnd.gif    }

 52InBlock.gif    else if (flagDown)//如果分页提示在下
 53ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 54InBlock.gif            start = 1;
 55InBlock.gif            end = trs.length -1;  
 56ExpandedSubBlockEnd.gif    }

 57InBlock.gif    else if (flagUp && flagDown)//上下都有分页提示
 58ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 59InBlock.gif            start = 2;
 60InBlock.gif            end = trs.length - 1
 61ExpandedSubBlockEnd.gif    }

 62InBlock.gif    else//上下都没有分页提示
 63ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 64InBlock.gif            start = 1;
 65InBlock.gif            end = trs.length; 
 66ExpandedSubBlockEnd.gif    }

 67InBlock.gif    
 68InBlock.gif    if (trs[trs.length - 2].cells.length != trs[trs.length - 1].cells.length)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 70InBlock.gif           lastTR = trs[trs.length - 1];//把最后一行保存起来
 71ExpandedSubBlockEnd.gif    }

 72InBlock.gif     
 73InBlock.gif    for (var i = start; i < end; i++)//
 74ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 75InBlock.gif            arrayTRs.push(trs[i]);
 76ExpandedSubBlockEnd.gif    }

 77InBlock.gif    
 78InBlock.gif    if (colIndex == lastCol)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 80InBlock.gif            arrayTRs.reverse();
 81ExpandedSubBlockEnd.gif    }

 82InBlock.gif    else
 83ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 84InBlock.gif            arrayTRs.sort(compareMutiTRs(colIndex));
 85ExpandedSubBlockEnd.gif    }

 86InBlock.gif    
 87InBlock.gif    var fragment = document.createDocumentFragment();
 88InBlock.gif    
 89InBlock.gif    for (var j = 0; j < arrayTRs.length; j++)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 91InBlock.gif            fragment.appendChild(arrayTRs[j]);
 92ExpandedSubBlockEnd.gif    }

 93InBlock.gif    
 94InBlock.gif    if (lastTR != null)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 96InBlock.gif            fragment.appendChild(lastTR);
 97ExpandedSubBlockEnd.gif    }

 98InBlock.gif    
 99InBlock.gif    tbl.tBodies[0].appendChild(fragment);
100InBlock.gif    
101InBlock.gif    lastCol = colIndex;
102ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/shenba/archive/2007/06/15/785285.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值