Home >Backend Development >PHP Tutorial >php uses js to sort tables

php uses js to sort tables

WBOY
WBOYOriginal
2016-07-29 09:15:281141browse

<!DOCTYPE>
<html>
<head>
    <meta http-equiv="Content-type" c 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  alt="" />
                
                
                <p>
                    以上就介绍了php使用js对表格进行排序,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。</p>
                <p>
                    </p>
Statement:
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