Home Web Front-end JS Tutorial Front-end case: Use js to implement table row deletion, sorting, and filtering

Front-end case: Use js to implement table row deletion, sorting, and filtering

Aug 09, 2018 am 10:27 AM


The personnel information table in the following format on the page:

Front-end case: Use js to implement table row deletion, sorting, and filtering

The HTML structure of each row of the table is:

<tr>
    <td><input type="checkbox"></td>
    <td>2</td>
    <td>李斯</td>
    <td>43</td>
    <td>陕西</td></tr>
Copy after login

Assume that the element id of the table is person-list, and the class name of the odd-numbered rows is odd. Please implement the following functions:

1. Select the radio button and the corresponding row will disappear when you click delete;
2. When you click sort, sort each row in the table in ascending order;
3. Click to filter, and the place of birth will become a drop-down box. The option value is the name of the province included in the current table. Select a province to display the personnel information of the corresponding province.

Implementation code:

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>人员信息表格</title>
    <style type="text/css">
        body {            font-family: "arial", sans-serif;        }
        #person-list {            width: 80%;            margin-left: 10%;            margin-right: 10%;        }
        #person-list thead {            font-weight: bold;        }
        #person-list button {            background-color: transparent;            border: 0;            font-weight: bold;            font-size: small;            padding-left: 0;            color: #6ba9ee;        }
        #person-list thead tr td {            border-bottom: 1px #ccc solid;        }
        #person-list tbody tr td:nth-child(2) {            font-weight: bold;        }
        #person-list tbody tr td {            border-top: 1px #ccc solid;            padding-top: 5px;            padding-bottom: 5px;        }
        #person-list tbody tr:nth-child(2n+1) {            background-color: #eee;        }
    </style>
    <script type="text/javascript">
    window.onload=function(){
    if (!document.getElementsByClassName) {//由于较低版本的IE不识别这个。
        document.getElementsByClassName=function(cls){
            var ret=[];            var eles=document.getElementsByTagName(&#39;*&#39;);            for(var i=0,len=eles.length;i<len;i++){//indexOf()返回的是字母在字符串中的下标,>=0代表存在
                if (eles[i].className===cls /*===是严格等于*/
                    ||eles[i].className.indexOf(cls+&#39;&#39;)>=0//当比较&#39;aaa&#39;和&#39;aaa &#39;时
                    ||eles[i].className.indexOf(&#39;&#39;+cls+&#39;&#39;)>=0///比较&#39;aaa&#39;和&#39;bbb aaa ccc&#39;时
                    ||eles[i].className.indexOf(&#39;&#39;+cls)>=0///比较&#39;aaa&#39;和&#39; aaa&#39;时
                    ) {
                    ret.push(eles[i]);
                }
            }            return ret;
        }
    }        var checks = document.getElementsByTagName(&#39;input&#39;);        var tbody = document.getElementsByTagName("tbody")[0];        var trs = tbody.getElementsByTagName(&#39;tr&#39;);        var remove = document.getElementById("remove");        var sort = document.getElementById("sort");        var select = document.getElementById("select");

        remove.onclick = function(){
            //删除选中行
            for(var i = checks.length-1; i >= 0;i--){ //因为removeChild的时候,长度会变化,所以不能以小于length作为判断条件,应该从后往前扫描
                if(checks[i].checked){
                    tbody.removeChild(checks[i].parentNode.parentNode);
                }
            }            //修改序号
            for(var i = 0;i < trs.length; i++){                var td=trs[i].getElementsByTagName("td")[1];
                td.innerHTML=i+1;
                }
                };

        sort.onclick=function(){
            //循环遍历,后面比它小的就插入到它前面去
            for(var i=0;i < trs.length; i++){                var td=trs[i].getElementsByTagName("td")[3];                for(var j=i;j < trs.length;j++){                    var tdd=trs[j].getElementsByTagName("td")[3];                    if((td.innerHTML - tdd.innerHTML)>0){
                        td.parentNode.parentNode.insertBefore(tdd.parentNode,td.parentNode);
                    }
                }
            }            //修改序号
            for(var i=0;i < trs.length;i++){                var td=trs[i].getElementsByTagName("td")[1];
                td.innerHTML=i+1;
            }
        };

        select.onclick=function(){
            //如果已经筛选过,页面中有下拉框了就不要再执行此函数了。
            if(document.getElementsByTagName(&#39;select&#39;).length>0) return false;            var provinces = [];            //把所有的省份取出来,存放到数组里
            for(var i=0;i < trs.length;i++){                var td=trs[i].getElementsByTagName("td")[4];                var prov=td.innerHTML;
                provinces.push(prov);
            }            //去重
            for(var j=0;j< provinces.length;j++){                for(var k=provinces.length;k>j;k--){ //同理,因为长度会发生变化,所以从后往前算
                    if(provinces[j] === provinces[k]){
                        provinces.splice(k,1);
                    }
                }
            }            //创建selectElem下拉框元素,option为省份
            var selectElem = document.createElement("select");            for(var z = 0;z < provinces.length;z++){                var option=document.createElement("option");
                option.innerHTML=provinces[z];
                option.value=provinces[z];
                selectElem.appendChild(option);
            }            var childNodes=select.parentNode.childNodes;            //去掉籍贯两个字
            for(var x= 0; x< childNodes.length;x++){                if(childNodes[x].nodeType === 3){
                    childNodes[x].parentNode.removeChild(childNodes[x]);
                }
            }            //在按钮之前插入select下拉框
            select.parentNode.insertBefore(selectElem,select);            //监控下拉框的option的点击事件,注意是下拉框的onchange,而不是option的onclick
            selectElem.onchange = function(){
                for(var i =0 ;i< trs.length;i++){
                    trs[i].style.display="none" ;                    if(trs[i].getElementsByTagName("td")[4].innerHTML == selectElem.value){
                        trs[i].style.display = "";
                    }
                }
            };
        };
}    </script></head><body>
    <table id="person-list">
        <thead>
        <tr>
            <td>
                <button id="remove">删除</button>
            </td>
            <td>序号</td>
            <td>姓名</td>
            <td>年龄                <button id="sort">排序</button>
            </td>
            <td>籍贯                <button id="select">筛选</button>
            </td>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>
                <input type="checkbox"/>
            </td>
            <td>1</td>
            <td>张三</td>
            <td>24</td>
            <td>北京</td>
        </tr>
        <tr>
            <td><input type="checkbox"/>
            </td>
            <td>2</td>
            <td>李斯</td>
            <td>43</td>
            <td>陕西</td>
        </tr>
        <tr>
            <td><input type="checkbox"/>
            </td>
            <td>3</td>
            <td>韩信</td>
            <td>49</td>
            <td>湖北</td>
        </tr>
        <tr>
            <td><input type="checkbox"/>
            </td>
            <td>4</td>
            <td>宋江</td>
            <td>43</td>
            <td>山东</td>
        </tr>
        <tr>
            <td><input type="checkbox"/>
            </td>
            <td>5</td>
            <td>李逵</td>
            <td>38</td>
            <td>青海</td>
        </tr>
        <tr>
            <td><input type="checkbox"/>
            </td>
            <td>6</td>
            <td>林冲</td>
            <td>42</td>
            <td>北京</td>
        </tr>
        </tbody>
    </table></body></html>
Copy after login

Related recommendations:

How to implement all-select, invert-select and delete tables using javascript

Detailed explanation of table operation classes implemented by JS (add, Delete, sort, move up, move down)

The above is the detailed content of Front-end case: Use js to implement table row deletion, sorting, and filtering. For more information, please follow other related articles on the PHP Chinese website!

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1279
29
C# Tutorial
1257
24
Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript and the Web: Core Functionality and Use Cases JavaScript and the Web: Core Functionality and Use Cases Apr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript in Action: Real-World Examples and Projects JavaScript in Action: Real-World Examples and Projects Apr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding the JavaScript Engine: Implementation Details Understanding the JavaScript Engine: Implementation Details Apr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: Development Environments and Tools Python vs. JavaScript: Development Environments and Tools Apr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

The Role of C/C   in JavaScript Interpreters and Compilers The Role of C/C in JavaScript Interpreters and Compilers Apr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

From Websites to Apps: The Diverse Applications of JavaScript From Websites to Apps: The Diverse Applications of JavaScript Apr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications Compared Python vs. JavaScript: Use Cases and Applications Compared Apr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

See all articles