前端纯js对表格进行快速排序,统计

后台框架:ssh

页面表格:(前端的样式用的是bootstrap)

这里的span是bootstrap的箭头样式,给箭头增加的点击事件

<table class="table table-striped" id="t1">
    <thead>      
        <tr>
            <th width="25%">
                学号
         <span class="glyphicon glyphicon-arrow-up" οnclick="orderByStudentId(2)"></span>
                <span class="glyphicon glyphicon-arrow-down" οnclick="orderByStudentId(1)"></span>
            </th>
            <th width="20%">姓名</th>
            <th width="20%">
                正确
         <span class="glyphicon glyphicon-arrow-up" οnclick="orderByCorrect(2)"></span>
                <span class="glyphicon glyphicon-arrow-down" οnclick="orderByCorrect(1)"></span>
            </th>
            <th width="20%">
                错误
         <span class="glyphicon glyphicon-arrow-up" οnclick="orderByError(2)"></span>
                <span class="glyphicon glyphicon-arrow-down" οnclick="orderByError(1)"></span>
            </th>
            <th width="15%">详情</th>
        </tr>
    </thead>
    <tbody class="tbody">
        <s:iterator value="recitingStudents">
        <tr>
            <td><s:property value="studentId"/></td>
            <td><s:property value="name"/></td>
            <td><s:property value="correct"/></td>
            <td><s:property value="error"/></td>
            <td><a>查看</a></td>
        </tr>
        </s:iterator>
    </tbody>
</table>

排序的js代码

为了代码的重用,可以增加传入的参数判断

快速开发,copy几个稍微改改就可以用了

//正确的排序
function orderByCorrect(num){
    var $tr = $(".tbody").find('tr');
    var array =[];
    $tr.each(function(){
        var obj ={};
        obj["index"] = $(this).index();
        obj["studentId"] = $(this).children('td').first().html();
        obj["name"] = $(this).children('td').eq(1).html();
        obj["correct"] = $(this).children('td').eq(2).html();
        obj["error"] = $(this).children('td').eq(3).html();
        obj["see"] = $(this).children('td').last().html();
        array.push(obj);
    });
    //排序
    if(num==1){
        array.sort(sortBy);
        function sortBy(a,b){
            return a.correct-b.correct;
        }
    }
    if(num==2){
        array.sort(sortBy);
        function sortBy(a,b){
            return b.correct-a.correct;
        }
    }
    //总数的计算 只用于刚开始的页面加载
   var corrects = 0;
    $.each(array,function(i){
        corrects+=parseInt(array[i].correct);
    });
    $("#correct").html(corrects);

    var errors = 0;
    $.each(array,function (i) {
        errors+=parseInt(array[i].error);
    });
    $("#error").html(errors);

    //删除旧数组生成新数组
    $('.tbody').empty().append($tr);
    for(var i =0 ;i<array.length;i++){
        $('.tbody').append($tr.eq(array[i].index));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值