Table of Contents
php uses js to sort tables, phpjs table sorting
Home Backend Development PHP Tutorial PHP uses js to sort tables, phpjs table sorting_PHP tutorial

PHP uses js to sort tables, phpjs table sorting_PHP tutorial

Jul 13, 2016 pm 04:53 PM
Data sorting

php uses js to sort tables, phpjs table sorting

<!DOCTYPE>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html" charset="utf-8">
    &lt;title&gt;<span>sort</span> table&lt;/title&gt;
    &lt;style&gt;
        *<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>&lt;/style&gt;
    &lt;script language="javascript"&gt;
        <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&lt;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&lt;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&lt;tbs.rows.length;i++<span>){
                    </span><span>var</span> text=tbs.rows[i].cells[type].<span>innerText;
                    arr2[text]</span>=<span>i;
                }
            }
        }
    </span>&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;<span>sort</span> table&lt;/center&gt;
&lt;table border="0"&gt;
    &lt;tr&gt;
        &lt;th onclick="sortCells(0);"&gt;序号&lt;/th&gt;
        &lt;th onclick="sortCells(1);"&gt;姓名&lt;/th&gt;
        &lt;th onclick="sortCells(2);"&gt;日期&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt;2&lt;/td&gt;
        &lt;td&gt;BB&lt;/td&gt;
        &lt;td&gt;2015-09-12&lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
        &lt;td&gt;3&lt;/td&gt;
        &lt;td&gt;CC&lt;/td&gt;
        &lt;td&gt;2015-07-12&lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
        &lt;td&gt;1&lt;/td&gt;
        &lt;td&gt;AA&lt;/td&gt;
        &lt;td&gt;2015-09-11&lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
        &lt;td&gt;4&lt;/td&gt;
        &lt;td&gt;DD&lt;/td&gt;
        &lt;td&gt;2015-06-12&lt;/td&gt;
    &lt;/tr&gt;

&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1053794.htmlTechArticlephp uses js to sort tables, phpjs table sorting! DOCTYPEhtmlhead meta http-equiv="Content-type" content ="text/html" charset="utf-8" title sort table/title style * { margin : 0...
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

Hot Article

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 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?

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

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

Aptos Price Forecast 2024-2030

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

How to sort WPS mobile version tables easily with one operation How to sort WPS mobile version tables easily with one operation Feb 09, 2024 pm 08:06 PM

How to sort WPS mobile version tables easily with one operation

See all articles