Table of Contents
php使用js对表格进行排序,phpjs表格排序
Home php教程 php手册 php使用js对表格进行排序,phpjs表格排序

php使用js对表格进行排序,phpjs表格排序

Jun 13, 2016 am 08:54 AM
Data sorting

php使用js对表格进行排序,phpjs表格排序

<!DOCTYPE>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html" charset="utf-8">
    <title><span>sort</span> table</title>
    <style>
        *<span>{
            margin</span>:<span>0px;
            padding</span>:<span>0px;
        }
        body{
            background</span>:<span>#</span><span>ccc;</span>
<span>        }
        table{
            width</span>:<span>350px;
            margin</span>:0<span> auto;
            background</span>-color:<span>#</span><span>eee;</span>
<span>        }
        table th{
            cursor</span>:<span>hand;
            padding</span>:5px 0<span>;
            background</span>-color:<span>#</span><span>999;</span>
<span>        }
        table td{
            background</span>-color:<span>#</span><span>fff;</span>
            font-size:<span>16px;
            font</span>-weight:<span>normal;
            text</span>-align:<span>center;
            line</span>-height:<span>30px;
        }
    </span></style>
    <script language="javascript">
        <span>function</span><span> sortCells(type){
            </span><span>var</span> tbs=document.getElementsByTagName("table")[0<span>];
            </span><span>var</span> arr=<span>[];
            </span><span>var</span> arr2=<span>[];
            </span><span>for</span>(<span>var</span> i=1;i<tbs.rows.length;i++<span>){
                </span><span>var</span> text=tbs.rows[i].cells[type].<span>innerText;
                arr</span>.<span>push(text);
                arr2[text]</span>=<span>i;
            }
            </span><span>if</span>(type==0<span>){
                arr</span>.<span>sort</span>(<span>function</span>(a,b){<span>return</span> a-<span>b});
            }</span><span>else</span><span>{
                arr</span>.<span>sort</span><span>();
            }
            </span><span>var</span> temp=""<span>;

            </span><span>for</span>(<span>var</span> j=1;j<tbs.rows.length;j++<span>){
                temp</span>=tbs.rows[j].cells[0].<span>innerText;
                tbs</span>.rows[j].cells[0].innerText=tbs.rows[arr2[arr[j-1]]].cells[0].<span>innerText;
                tbs</span>.rows[arr2[arr[j-1]]].cells[0].innerText=<span>temp;

                temp</span>=tbs.rows[j].cells[1].<span>innerText;
                tbs</span>.rows[j].cells[1].innerText=tbs.rows[arr2[arr[j-1]]].cells[1].<span>innerText;
                tbs</span>.rows[arr2[arr[j-1]]].cells[1].innerText=<span>temp;

                temp</span>=tbs.rows[j].cells[2].<span>innerText;
                tbs</span>.rows[j].cells[2].innerText=tbs.rows[arr2[arr[j-1]]].cells[2].<span>innerText;
                tbs</span>.rows[arr2[arr[j-1]]].cells[2].innerText=<span>temp;
</span><span>//</span><span>                console.log(arr2);</span>
                <span>for</span>(<span>var</span> i=1;i<tbs.rows.length;i++<span>){
                    </span><span>var</span> text=tbs.rows[i].cells[type].<span>innerText;
                    arr2[text]</span>=<span>i;
                }
            }
        }
    </span></script>
</head>
<body>
<center><span>sort</span> table</center>
<table border="0">
    <tr>
        <th onclick="sortCells(0);">序号</th>
        <th onclick="sortCells(1);">姓名</th>
        <th onclick="sortCells(2);">日期</th>
    </tr>
    <tr>
        <td>2</td>
        <td>BB</td>
        <td>2015-09-12</td>
    </tr>
     <tr>
        <td>3</td>
        <td>CC</td>
        <td>2015-07-12</td>
    </tr>
     <tr>
        <td>1</td>
        <td>AA</td>
        <td>2015-09-11</td>
    </tr>
     <tr>
        <td>4</td>
        <td>DD</td>
        <td>2015-06-12</td>
    </tr>

</table>
</body>
</html>
Copy after login

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to access ET files How to access ET files Feb 18, 2024 pm 08:46 PM

How to access ET files

PHP form processing: form data sorting and ranking PHP form processing: form data sorting and ranking Aug 09, 2023 pm 06:01 PM

PHP form processing: form data sorting and ranking

The use of char in mysql The use of char in mysql Apr 27, 2024 am 09:09 AM

The use of char in mysql

How to improve the speed of large-scale data sorting through PHP multi-threading How to improve the speed of large-scale data sorting through PHP multi-threading Jun 29, 2023 pm 04:15 PM

How to improve the speed of large-scale data sorting through PHP multi-threading

How to optimize the efficiency of data sorting and data grouping in PHP and MySQL through indexes? How to optimize the efficiency of data sorting and data grouping in PHP and MySQL through indexes? Oct 15, 2023 pm 04:00 PM

How to optimize the efficiency of data sorting and data grouping in PHP and MySQL through indexes?

Detailed explanation of the implementation method of Java selection sort algorithm Detailed explanation of the implementation method of Java selection sort algorithm Feb 20, 2024 am 10:47 AM

Detailed explanation of the implementation method of Java selection sort algorithm

Aptos Price Forecast 2024-2030 Aptos Price Forecast 2024-2030 Mar 20, 2024 am 11:35 AM

Aptos Price Forecast 2024-2030

Installation and considerations: a simple guide to the pandas library Installation and considerations: a simple guide to the pandas library Feb 18, 2024 pm 12:46 PM

Installation and considerations: a simple guide to the pandas library

See all articles