vue实例:实现数据的增删改查(纯前端)

news/2024/7/10 3:25:29 标签: vue, 增删改查
<template>
    <div class="container">
        <div class="input-bar">
            <table>
                <tbody>
                    <td>标题:</td>
                    <td><input type="text" v-model="addObj.title"/></td>
                    <td>发布人:</td>
                    <td><input type="text" v-model="addObj.user"/></td>
                    <td>发布时间:</td>
                    <td><input type="date" v-model="addObj.time"/></td>
                    <td><button @click="add">发布</button></td>
                </tbody>
            </table>
        </div>

        <table width="100%" class="table table-bordered">
            <thead>
                <tr>
                    <td>序号</td>
                    <td>标题</td>
                    <td>发布人</td>
                    <td>发布时间</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(user, index) in users" :key="index">
                    <td>{{index+1}}</td>
                    <td>{{user.title}}</td>
                    <td>{{user.user}}</td>
                    <td>{{user.time}}</td>
                    <td>
                        <button @click="edit(user)">编辑</button> 
                        <button @click="del(index)">删除</button>
                    </td>
                </tr>
            </tbody>   
        </table> 

        <div class="modal" v-if="isShow">
            <h3 class="modal-header">编辑 <span @click="isShow=false">x</span></h3>
            <div class="modal-body">
                <table>
                    <tbody>
                        <tr>
                            <td width="20%">标题:</td>
                            <td width="80%"><input type="text" v-model="editObj.title"/></td>
                        </tr>
                        <tr>
                            <td>发布人:</td>
                            <td><input type="text" v-model="editObj.user"/></td>
                        </tr>
                    </tbody>
                </table>
            </div>   
            <div class="modal-footer">
                <button @click="save">保存</button>
                <button @click="isShow=false">取消</button>
            </div>
        </div>
    </div>   
</template>

<script>
export default {
    data(){
        return {
            isShow: false,
            addObj: {
                title: '',
                user: '',
                time: '',
                id: ''
            },
            editObj: {
                title: '',
                user: ''
            },
            users: [
                {
                    title: 'aaa',
                    user: 'bbb',
                    time: '2019-12-01',
                    id: 0
                },
                {
                    title: 'ccc',
                    user: 'ddd',
                    time: '2019-12-02',
                    id: 1
                }
            ]
        }
    },
    methods: {
        add(){
            if(!this.addObj.title || !this.addObj.user || !this.addObj.time){
                this.$toastr("输入框不能为空!");
                return;
            }

            this.addObj.id = Math.max(...this.users.map(function(v){
                return v.id;
            }))+1;

            this.users.unshift({
                title: this.addObj.title,
                user: this.addObj.user,
                time: this.addObj.time,
                id: this.addObj.id
            });

            this.addObj={};
        },
        del(i){
            this.users.splice(i, 1);
        },
        edit(obj){
            this.isShow = true;
            this.editObj.title = obj.title;
            this.editObj.user = obj.user;
            this.editObj.id = obj.id;
        },
        save(){
            for(let i=0; i<this.users.length; i++){
                if(Number(this.users[i].id)===Number(this.editObj.id)){
                    this.users[i].title = this.editObj.title;
                    this.users[i].user = this.editObj.user;
                }
            }
            this.isShow = false;
        }
    }
}
</script>

<style scoped>
.container{
    padding: 10px;
}
.input-bar{
    padding: 5px;
    background: #f1f1f1;
    margin-bottom: 10px;
}
input{
    display: block;
    width: 100%;
    height: 30px;
    font-size: 14px;
    line-height: 1.42857143;
    color: #555;
    background-color: #fff;
    background-image: none;
    /* border: 1px solid #ccc; */
    border: 1px solid #e4eaec;
    /* border-radius: 4px; */
    border-radius: 2px;
    /* -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); */
    /* box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075); */
    -webkit-transition: border-color ease-in-out .15s, -webkit-box-shadow ease-in-out .15s;
    -o-transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
    transition: border-color ease-in-out .15s, box-shadow ease-in-out .15s;
}
.table, .table > tbody > tr > th, .table > tfoot > tr > th, .table > thead > tr > td, .table > tbody > tr > td, .table > tfoot > tr > td {
    padding: 8px !important;
    margin-bottom: 0 !important;
    /* border-bottom: 1px solid #d4d8da; */
    border-bottom: 1px solid #e4eaec;
    border-collapse: collapse !important;
    border-radius: 1px;
}
.table tbody {
    display: table-row-group;
    vertical-align: middle;
    border-color: inherit;
}
.table tr {
    display: table-row;
    vertical-align: inherit;
    border-color: inherit;
}
.table-bordered {
    border: 1px solid #e4eaec;
}
.table-bordered > thead > tr > th, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > tbody > tr > td, .table-bordered > tfoot > tr > td {
    border: 1px solid #e4eaec;
}
.table-bordered > tbody > tr > td, .table-bordered > tfoot > tr > td {
    border: 1px solid #e4eaec;
}
.modal{
    width: 400px;
    height: 200px;
    position: fixed;
    top: 50%;
    left: 50%;
    margin-top: -100px;
    margin-left: -200px;
    background: #ffffff;
    border: 1px solid #dddddd;
    border-radius: 5px;
    z-index: 100000;
}
.modal-header{
    margin: 0;
    padding: 0;
    font-size: 14px;
    font-weight: normal;
    padding-left: 10px;
    padding-right: 10px;
    line-height: 40px;
}
.modal-header span{
    float: right;
    cursor: pointer;
}
.modal-body{
    padding: 10px;
}
.modal-footer{
    text-align: right;
    padding: 10px;
}
button{
    color: #fff;
    background-color: #36a9e1;
    border-color: #36a9e1;
    display: inline-block;
    padding: 4px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: normal;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid transparent;
    /* border-radius: 4px; */
    border-radius: 2px;
}
</style>

 


http://www.niftyadmin.cn/n/886776.html

相关文章

GO语言的defer 关键字

介绍 Go 语言的 defer 会在当前函数返回前执行传入的函数&#xff0c;它会经常被用于关闭文件描述符、关闭数据库连接以及解锁资源。 作为一个编程语言中的关键字&#xff0c;defer 的实现一定是由编译器和运行时共同完成的&#xff0c;不过在深入源码分析它的实现之前我们还…

加载状态的实现

.shade {position: fixed;top: 0;left: 0;height: 100%;width: 100%;background: #FFFFFF;z-index:99999;filter:alpha(opacity100); /* IE */-moz-opacity: 1; /* Moz FF */opacity: 1; /* 支持CSS3的浏览器&#xff08;FF 1.5也支持&#xff09;*/display: none; }.shade ta…

React类的自定义原型方法中的this指向为什么是undefined?如何解决?(绑定 this 的几种方式)

首先来看下类中定义的原型方法的this不同调用时的指向&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"&…

React中数据子传父的实现

原理&#xff1a;通过props将父组件中的方法传给子组件实现。 父组件&#xff1a; class Parent extends React.Component{constructor(){super();this.state{count: 0}}increase(e){this.setState({count: this.state.counte})}render(){return (<div><p onClick{…

什么是Library

在计算机科学中&#xff0c;library是计算机程序经常用于软件开发的非易失性资源的集合。这些可能包括配置数据&#xff0c;文档&#xff0c;帮助数据&#xff0c;消息模板&#xff0c;预编写的代码和子例程&#xff0c;类&#xff0c;值或类型规范。在IBM OS / 360及其后续版本…

webpack4:基本使用

webpack是基于Node构建&#xff0c;所以wepack支持所有Node API和语法。 即&#xff1a;Chrome浏览器能支持的ECMAScript语法&#xff08;排除DOM、BOM&#xff09;&#xff0c;wbpack都能支持。Chrome不支持ES6&#xff0c;所以webpack也不支持。 创建基本的webpack4.x项目&…

Kubernetes和Docker

定义上的区别 官方定义1&#xff1a;Docker是一个开源的应用容器引擎&#xff0c;开发者可以打包他们的应用及依赖到一个可移植的容器中&#xff0c;发布到流行的Linux机器上&#xff0c;也可实现虚拟化。 官方定义2&#xff1a;k8s是一个开源的容器集群管理系统&#xff0c;…

RAM,ROM,内存还有硬盘的区别

内存&#xff0c;RAM&#xff0c;ROM&#xff0c;CACHE 内存在电脑中起着举足轻重的作用。内存一般采用半导体存储单元&#xff0c;包括随机存储器&#xff08;RAM&#xff09;&#xff0c;只读存储器&#xff08;ROM&#xff09;&#xff0c;以及高速缓存&#xff08;CACHE&a…