【Vue实现简单增删改查】

利用bootstrap3简易布局Vue操作

DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <script src="./vue-2.4.0.js">script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.js">script>
    <script src="./bootstrap/js/bootstrap.min.js">script>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css">
head>

<body>
    <div id="app">
        
        <div class="container">
            
            <div class="modal fade" id="modal-id">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×button>
                            <h4 class="modal-title">修改窗口h4>
                        div>
                        <div class="modal-body">
                            
                            <input type="text" class="form-control" id="" placeholder="请输入修改后的品牌名称" v-model="newName">
                        div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭button>
                            <button type="button" class="btn btn-primary" @click="confirm">确认修改button>
                        div>
                    div>
                div>
            div>

            
            <div class="panel panel-info">
                <div class="panel-heading">
                    <h3 class="panel-title">添加品牌h3>
                div>
                <div class="panel-body">
                    <form action="" method="POST" class="form-inline" role="form">
                        <div class="form-group">
                            <label class="" for="">Id:label>
                            <input type="text" class="form-control" id="" placeholder="请输入品牌Id" v-model="carId">
                        div>
                        <div class="form-group">
                            <label class="" for="">Name:label>
                            <input type="text" class="form-control" id="" placeholder="请输入品牌名称" v-model="carName">
                        div>
                        <div class="form-group">
                            <label class="" for="">搜索:label>
                            <input type="text" class="form-control" id="" placeholder="请输入品牌名称关键字" v-model="keyWord"
                                @keyup="searchWord">
                        div>
                        <button type="button" class="btn btn-primary" @click="add">添加button>

                    form>
                    
                    <table class="table table-hover table-bordered">
                        <thead>
                            <tr>
                                <th>Idth>
                                <th>Nameth>
                                <th>Ctimeth>
                                <th>Operationth>
                            tr>
                        thead>
                        <tbody>
                            <tr v-for="(item,index) in getCarList()" :key="item.carId">
                                <td>{{item.carId}}td>
                                <td>{{item.carName}}td>
                                <td>{{item.cTime}}td>
                                <td>
                                    <button type="button" class="btn btn-default" @click="deleteCar(item)">删除button>
                                    <button type="button" @click="changeCar(item)">
                                        <a class="btn btn-primary" data-toggle="modal" href='#modal-id'>修改a>
                                    button>
                                td>
                            tr>
                        tbody>
                    table>
                div>
            div>

        div>
    div>
body>
<script>
    let vm = new Vue({
        el: '#app',
        data: {
            name: '',
            newName: '',
            carId: '',
            carName: '',
            keyWord: '',
            carList: [
                {
                    carId: 1,
                    carName: '奔驰',
                    cTime: new Date()
                },
                {
                    carId: 2,
                    carName: '宝马',
                    cTime: new Date()
                },
            ]

        },
        methods: {
            // 添加
            add() {
                let carobj = {
                    carId: this.carId,
                    carName: this.carName,
                    cTime: new Date()
                }
                // 判断重复id和添加
                // if(this.carList.some(item => item.carId == this.carId)){
                //     alert('已有此ID')
                // }else {
                //     this.carList.push(carobj)
                // }    
                if (this.carList.find(item => item.carId == this.carId)) {
                    alert('已有此ID')
                } else {
                    this.carList.push(carobj)
                }
                // 赋空
                this.carId = ''
                this.carName = ''
            },
            // 删除
            // deleteCar(item) {
            // this.carList.splice(this.carList.indexOf(item), 1)
            // },
            // 搜索
            searchWord() {
                                                                                                                                                                                                                                                                                                                                                                                                                           
            },
            getCarList() {
                return this.carList.filter(item => {
                    return item.carName.includes(this.keyWord)
                })

            },
            // 更改
            changeCar(item) {
                this.name = this.carList.indexOf(item);
            },
            confirm() { 
                this.carList[this.name].this.carName = this.newName
            }

        }
    })
script>

html>

你可能感兴趣的:(vue.js)