【vue】设计一个表格,增删改查,分页,固定列,特殊字符校验

简介: 【vue】设计一个表格,增删改查,分页,固定列,特殊字符校验


前言

前端二面试题,话不多说看题~

功能

1.表格

2.删除,编辑,新增

代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        html,
        body {
            height: 100%;
            width: 100%;
        }
        #d1 {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.5);
        }
        .hide {
            display: none;
        }
        .content {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: flex-start;
        }
        #d2 {
            height: 300px;
            width: 600px;
            background-color: white;
            position: fixed;
            top: 50%;
            left: 50%;
            margin-left: -300px;
            margin-top: -120px;
        }
    </style>
</head>
<body>
    <div id="d1" class=" c1 hide"></div>
    <div id="d2" class=" c1 hide"></div>
    <div class="content">
        <input type="button" class="create" value="新增">
        <table border="1px" Cellspacing="0" Cellpadding="10">
            <thead>
                <tr class="title">
                    <th>#</th>
                    <th>姓名</th>
                    <th>爱好</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td class="name">EGon</td>
                    <td class='fav'>街舞</td>
                    <td><input type="button" class="add" value="编辑"> <input type="button" class="delete" value="删除">
                    </td>
                </tr>
                <tr>
                    <td>2</td>
                    <td class="name">ALEX</td>
                    <td class='fav'>理发</td>
                    <td><input type="button" class="add" value="编辑"> <input type="button" class="delete" value="删除">
                    </td>
                </tr>
                <tr>
                    <td>3</td>
                    <td class="name">小强</td>
                    <td class='fav'>二人转</td>
                    <td><input type="button" class="add" value="编辑"> <input type="button" class="delete" value="删除">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <script src="http://unpkg.com/jquery"></script>
    <script>
        $(".create").on("click", function () {
            $(".c1").removeClass("hide")
            $("#d2").html("<p>姓名:<input class='named' type='text'></p><p>爱好:<input class='fave' type='text'></p> <p><input class='submit'  type='button' value='提交'><input  class='cancle' type='button' value='取消'></p>");
        })
        $("body").on("click", ".cancle", function () {
            $(".c1").addClass("hide")
        })
        $("body").on("click", ".submit", function () {
            if ($(".named").val() == "") {
                var nameVal = $(".named").val();
                var favVal = $(".fave").val();
                var xuHao = parseInt($("tr:last").children().first().text()) + 1
                console.log(xuHao, nameVal, favVal)
                var trEle = document.createElement("tr");
                $("tbody").append(trEle)
                $(trEle).html("<td class='q1'></td>, <td class='q2' ></td> ,<td class='q3'></td> ,<td><input type='button' value='编辑'><input type='button' class='delete' value='删除'></td>");
                $(".q1").text(xuHao);
                $(".q2").text(nameVal);
                $(".q3").text(favVal);
                $(".c1").addClass("hide");
            }
            else {
                var nameVal = $(".named").val();
                var favVal = $(".fave").val();
            }
        })
        $("body").on("click", ".delete", function () {
            $(this).parent().parent().remove()
        })
        $("body").on("click", ".add", function () {
            $(this).data("name", $(this).parent().siblings(".name").html());
            console.log($(this).data("name"));
            $(this).data("fav", $(this).parent().siblings(".fav").html());
            console.log($(this).data("fav"));
            $(".c1").removeClass("hide");
            $("#d2").html("<p>姓名:<input class='named' type='text'></p><p>爱好:<input class='fave' type='text'></p> <p><input class='submit'  type='button' value='提交'><input  class='cancle' type='button' value='取消'></p>");
            $(".named")[0].value = $(this).data("name");
            $(".fave")[0].value = $(this).data("fav");
            var nameVal = $(".named").val();
            var favVal = $(".fave").val();
            console.log(nameVal, favVal)
            var a1 = $(this).parent().siblings(".name")
            var a2 = $(this).parent().siblings(".fav")
            $("body").on("click", ".submit", function () {
                var nameVal = $(".named").val();
                var favVal = $(".fave").val();
                a1.html(nameVal)
                a2.html(favVal)
                console.log(nameVal, favVal)
                $(".c1").addClass("hide");
            })
        })
    </script>
</body>
</html>

效果图

目录
相关文章
|
5月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
487 2
|
4月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
409 137
|
8月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
945 0
|
7月前
|
人工智能 JSON JavaScript
VTJ.PRO 首发 MasterGo 设计智能识别引擎,秒级生成 Vue 代码
VTJ.PRO发布「AI MasterGo设计稿识别引擎」,成为全球首个支持解析MasterGo原生JSON文件并自动生成Vue组件的AI工具。通过双引擎架构,实现设计到代码全流程自动化,效率提升300%,助力企业降本增效,引领“设计即生产”新时代。
557 1
|
7月前
|
JavaScript 安全
在 Vue 中,如何在回调函数中正确使用 this?
在 Vue 中,如何在回调函数中正确使用 this?
373 0
|
10月前
|
JavaScript
vue实现任务周期cron表达式选择组件
vue实现任务周期cron表达式选择组件
1204 4
|
8月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能
|
9月前
|
JavaScript 数据可视化 前端开发
基于 Vue 与 D3 的可拖拽拓扑图技术方案及应用案例解析
本文介绍了基于Vue和D3实现可拖拽拓扑图的技术方案与应用实例。通过Vue构建用户界面和交互逻辑,结合D3强大的数据可视化能力,实现了力导向布局、节点拖拽、交互事件等功能。文章详细讲解了数据模型设计、拖拽功能实现、组件封装及高级扩展(如节点类型定制、连接样式优化等),并提供了性能优化方案以应对大数据量场景。最终,展示了基础网络拓扑、实时更新拓扑等应用实例,为开发者提供了一套完整的实现思路和实践经验。
1199 78
|
10月前
|
缓存 JavaScript 前端开发
Vue 基础语法介绍
Vue 基础语法介绍
|
8月前
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
576 17