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

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles