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


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>

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>

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
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
Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 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.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 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 ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 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.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 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.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 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.

See all articles

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 Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.