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>Document</title>
    <script src="../lib/vue.js"></script>
    <style>
        table {
            border-collapse: collapse;
            border: 1px solid black;
            text-align: center;
        }
        th {
            border: 1px solid black;
            padding: 10px;
        }
        td {
            padding: 10px;
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <div id="app">
        类别<input type="text" v-model="addbook.bookname"><br />
        作者<input type="text" v-model="addbook.name"><br />
        价格<input type="text" v-model="addbook.price"><br />
        <button @click="addBook()">添加</button><br />
        搜索<input type="text" placeholder="请输入书的类别" v-model="msg"><br />
        <button @click="searchBook()">搜索</button>
        <table>
            <thead>
                <tr>
                    <th>序号</th>
                    <th>类别</th>
                    <th>作者</th>
                    <th>价格</th>
                    <th>时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item,index) in arr1" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.bookname}}</td>
                    <td>{{item.name}}</td>
                    <td>价格:{{item.price}}元</td>
                    <td>{{item.time}}</td>
                    <td><button @click='delBook(index)'>删除</button></td>
                </tr>
            </tbody>
        </table>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                msg: '',
                addbook: {
                    bookname: '',
                    name: '',
                    price: '',
                    time: new Date()
                },
                arr: [
                    { id: 0, bookname: '人文', name: 'xzl', price: 20, time: new Date() },
                    { id: 1, bookname: '政治', name: 'wwh', price: 30, time: new Date() },
                    { id: 3, bookname: '历史', name: 'wz', price: 50, time: new Date() },
                    { id: 4, bookname: '科学', name: 'xzl', price: 60, time: new Date() },
                    { id: 5, bookname: '人文', name: 'wz', price: 80, time: new Date() },
                    { id: 6, bookname: '历史', name: 'wp', price: 20, time: new Date() },
                    { id: 7, bookname: '科学', name: 'wp', price: 90, time: new Date() },
                ],
                arr1: [{ id: 0, bookname: '人文', name: 'xzl', price: 20, time: new Date() },
                { id: 1, bookname: '政治', name: 'wwh', price: 30, time: new Date() },
                { id: 3, bookname: '历史', name: 'wz', price: 50, time: new Date() },
                { id: 4, bookname: '科学', name: 'xzl', price: 60, time: new Date() },
                { id: 5, bookname: '人文', name: 'wz', price: 80, time: new Date() },
                { id: 6, bookname: '历史', name: 'wp', price: 20, time: new Date() },
                { id: 7, bookname: '科学', name: 'wp', price: 90, time: new Date() },]
            },
            methods: {
                // 新增书本
                addBook() {
                    // 通过转json字符串等一些列操作进行深拷贝,切断与v-model的联系写入数组
                    var str = JSON.parse(JSON.stringify(this.addbook))//深拷贝
                    str.id = this.arr.length + 1
                    this.arr1.push(str);
                    this.arr.push(str);
                },
                // 删除书本
                delBook(index) {
                    if (confirm('确定删除吗?')) {
                        this.arr1.splice(index, 1)
                        this.arr.splice(index, 1)
                    }
                },
                // 搜索书本
                searchBook() {
                    var key = this.msg
                    if (key) {
                        // 这里为了实现单机重复搜索的功能
                        // 显示从新数组中循环遍历显示
                        // 搜索从老数组中中搜索,防止只有一个数组导致数据覆盖
                        // 实际项目中都有对应的接口所以并不用考虑这个问题
                        var res = this.arr.filter(item => {
                            return item.bookname.indexOf(key) != -1
                        })
                        this.arr1 = res
                    }
                }
            }
        })
    </script>
</body>
</html>

  • 7
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大聪明码农徐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值