Home > Web Front-end > JS Tutorial > body text

JS method to implement different fields of table sorting

一个新手
Release: 2017-10-02 09:43:58
Original
1154 people have browsed it

<table id="tableSort">
    <thead>
        <tr>
            <th data-type="num">工号</th>
            <th data-type="string">姓名</th>
            <th data-type="string">性别</th>
            <th data-type="date">时间</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>06</td>
            <td>sssss</td>
            <td>男</td>
            <td>2008-10-10</td>
        </tr>
        <tr>
            <td>07</td>
            <td>ddddd</td>
            <td>男</td>
            <td>2009-10-10</td>
        </tr>
        <tr>
            <td>11</td>
            <td>qqqqq</td>
            <td>男</td>
            <td>2010-10-10</td>
        </tr>
        <tr>
            <td>02</td>
            <td>vvvvv</td>
            <td>男</td>
            <td>2011-10-10</td>
        </tr>
        <tr>
            <td>11</td>
            <td>hhhhh</td>
            <td>男</td>
            <td>2011-12-10</td>
        </tr>
        <tr>
            <td>01</td>
            <td>eeeee</td>
            <td>女</td>
            <td>2015-10-10</td>
        </tr>
        <tr>
            <td>09</td>
            <td>mmmmm</td>
            <td>女</td>
            <td>2015-11-10</td>
        </tr>
    </tbody></table>
Copy after login
<script> 
    var th = document.querySelector("#tableSort").tHead.rows[0].cells;
    var tbody = document.querySelector("#tableSort").tBodies[0];
    var td = tbody.rows;
    for(var i=0;
    i<th.length;
    i++) {
    th[i].flag = 1;
    th[i].onclick = function () {
    sort(this.getAttribute("data-type"),this.cellIndex,this.flag);
    this.flag = -this.flag;
}
} function sort(str,index,flag) {
    var arr = [];
    for(var i=0;
    i<td.length;
    i++) {
    arr.push(td[i]);
}
;
    //排序开始 arr.sort(function(a,b) {
    return method(str, a.cells[index].innerHTML, b.cells[index].innerHTML)*flag;
}
);
    //添加到table for(var i=0;
    i<arr.length;
    i++) {
    tbody.appendChild(arr[i]) }
} //排序方法 function method(str,a,b) {
    switch(str) {
    case &#39;num&#39;: return a-b;
    break;
    case &#39;string&#39;: return a.localeCompare(b);
    break;
    case "date": //日期排序,IE8下&#39;2012-12-12&#39;这种格式无法设置时间,替换成&#39;/&#39; return new Date(a.split("-").join("/")).getTime() - new Date(b.split("-").join("/")).getTime();
}
}
</script>
对于sort()排序方法。里面可以自定义小编觉得还是挺灵活的 arr.sort((a,b) => a-b) 针对number类型,
Copy after login
arr.sort((a,b) => a.localeCompare(b))这波能反杀,针对string类型作比较 可返回(1,0,-1)三个选项,
Copy after login
1,0,-1的先后顺序,由于小编记忆力极差,刚看就忘了,请大家自行百度
Copy after login

The above is the detailed content of JS method to implement different fields of table sorting . For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!