Vue实现图书管理案例


Posted in Vue.js onJanuary 20, 2021

本文实例为大家分享了Vue实现图书管理的具体代码,供大家参考,具体内容如下

案例效果

Vue实现图书管理案例

案例思路

1、图书列表

  • 实现静态列表效果
  • 基于数据实现模板效果
  • 处理每行的操作按钮

2、添加图书

  • 实现表单的静态效果
  • 添加图书表单域数据绑定
  • 添加按钮事件绑定
  • 实现添加业务逻辑

3、修改图书

  • 修改信息填充到表单
  • 修改后重新提交到表单
  • 重用添加和修改方法

4、删除图书

  • 删除按钮绑定时间处理方法
  • 实现删除业务逻辑

5、常用特性应用场景

  • 过滤器(格式化日期)
  • 自定义指令(获取表单焦点)
  • 计算属性(统计图书数量)
  • 侦听器(验证图书和编号的存在性)
  • 生命周期(图书数据处理)

代码

基本样式

<style type="text/css">
    .grid {
      margin: auto;
      width: 550px;
      text-align: center;
    }

    .grid table {
      width: 100%;
      border-collapse: collapse;
    }

    .grid th,
    td {
      padding: 10;
      border: 1px dashed orange;
      height: 35px;
    }

    .grid th {
      background-color: orange;
    }

    .grid .book {
      width: 550px;
      padding-bottom: 10px;
      padding-top: 5px;
      background-color: lawngreen;
    }

    .grid .total {
      height: 30px;
      line-height: 30px;
      background-color: lawngreen;
      border-top: 1px solid orange;
    }
</style>

静态布局

<div id="app">
    <div class='grid'>
      <div>
        <h1>图书管理</h1>
        <div class="book">
          <div>
            <label for='id'>
              编号:
            </label>
            <input type="text" id="id" v-model='id' :disabled='flag' v-focus>
            <label for="name">
              名称:
            </label>
            <input type="text" id='name' v-model='name'>
            <button @click='handle' :disabled='submitFlag'>提交</button>
          </div>
        </div>
      </div>
      <div class='total'>
        <span>图书总数:</span><span>{{total}}</span>
      </div>
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>名称</th>
            <th>时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr :key="item.id" v-for="item in books">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.date | format('yyyy-MM-dd hh:MM:ss')}}</td>
            <td><a href="" @click.prevent='toEdit(item.id)'>修改</a>
              <span>|</span>
              <a href="" @click.prevent='deleteBook(item.id)'>删除</a>
            </td>
          </tr>

        </tbody>
      </table>
    </div>
</div>

效果实现

<script type="text/javascript" src="../js/vue.js"></script>
  <script type="text/javascript">
    Vue.directive('focus', {
      inserted: function (el) {
        el.focus();
      }
    })
    Vue.filter('format', function (value, arg) {
      function dateFormat(date, format) {
        if (typeof date === "string") {
          var mts = date.match(/(\/Date\((\d +)\)\/)/);
          if (mts && mts.length >= 3) {
            date = parseInt(mts[2]);
          }
        }
        date = new Date(date);
        if (!date || date.toUTCString() == "Invalid Date") {
          return "";
        }
        var map = {
          "M": date.getMonth() + 1, //月份
          "d": date.getDate(), //日
          "h": date.getHours(), //小时
          "m": date.getMinutes(), //分
          "s": date.getSeconds(), //秒
          "q": Math.floor((date.getMonth() + 3) / 3), //季度
          "S": date.getMilliseconds() //毫秒

        };
        format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
          var v = map[t];
          if (v != undefined) {
            if (all.length > 1) {
              v = '0' + v;
              v = v.substr(v.length - 2);
            }
            return v;
          } else if (t === 'y') {
            return (date.getFullYear() + '').substr(4 - all.length);
          }
          return all;
        });
        return format;
      }
      return dateFormat(value, arg);
    })
    var vm = new Vue({
      el: '#app',
      data: {
        flag: false,
        submitFlag: false,
        id: '',
        name: '',
        books: []
      },
      methods: {
        handle: function () {
          if (this.flag) {
            // 编辑操作
            // 就是根据当前id去更新数组中对应的数据
            this.books.some((item) => {
              if (item.id == this.id) {
                item.name = this.name
                // 完成更新操作后终止循环
                return true;
              }
            })
            this.flag = false;
          } else {
            // 添加图书
            var book = {};
            book.id = this.id;
            book.name = this.name;
            this.data = '';
            this.books.push(book);
          }
          // 清空表单
          this.id = '';
          this.name = '';
        }, toEdit: function (id) {
          // 禁止修改id
          this.flag = true;
          // 根据id查询出要编辑的数据
          var book = this.books.filter(function (item) {
            return item.id == id;
          });
          console.log(book)
          // 把获取到的id提交到表单
          this.id = book[0].id;
          this.name = book[0].name;
        },
        deleteBook: function (id) {
          // 删除图书
          // 根据id从数组中查找元素的索引
          // var index = this.books.findIndex(function (item) {
          //   return item.id == id;
          // });
          // 根据索引删除数组元素
          // this.books.splice(index, 1)
          // -----------------
          // 方法2 通过filter方法进行删除
          this.books = this.books.filter(function (item) {
            return item.id != id;
          })
        }
      },
      computed: {
        total: function () {
          // 计算图书的总数
          return this.books.length;
        }
      },
      watch: {
        name: function (val) {
          // 验证图书名称是否已经存在
          var flag = this.books.some(function (item) { return item.name == val; })
          if (flag) {
            // 图书名存在
            this.submitFlag = true
          } else {
            // 图书名不存在
            this.submitFlag = false
          }
        }

      },
      mounted: function () {
        // 该生命周期钩子函数被出发的时候。模板已经可以使用
        // 一般此时用于获取后台数据,然后把数据填充到模板
        var data = [{
          id: 1,
          name: '三国演义',
          date: 252597867777

        }, {
          id: 2,
          name: '水浒传',
          date: 564634563453
        }, {
          id: 3,
          name: '红楼梦',
          date: 345435345343
        }, {
          id: 4,
          name: '西游记',
          date: 345345346533
        }]
        this.books = data
      }
    });
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
vue-video-player 断点续播的实现
Feb 01 Vue.js
Vue 实现可视化拖拽页面编辑器
Feb 01 Vue.js
vue中h5端打开app(判断是安卓还是苹果)
Feb 26 Vue.js
vue项目实现分页效果
Mar 24 Vue.js
vue3如何优雅的实现移动端登录注册模块
Mar 29 Vue.js
Vue实现动态查询规则生成组件
May 27 Vue.js
vue+element ui实现锚点定位
Jun 29 Vue.js
vue项目中的支付功能实现(微信支付和支付宝支付)
Feb 18 Vue.js
vue cli4中mockjs在dev环境和build环境的配置详情
Apr 06 Vue.js
vue elementUI表格控制对应列
Apr 13 Vue.js
vue-treeselect的基本用法以及解决点击无法出现拉下菜单
Apr 30 Vue.js
详解vue之自行实现派发与广播(dispatch与broadcast)
Jan 19 #Vue.js
vue二选一tab栏切换新做法实现
Jan 19 #Vue.js
vue-resource 拦截器interceptors使用详解
Jan 18 #Vue.js
vue element el-transfer增加拖拽功能
Jan 15 #Vue.js
Vue实现多页签组件
Jan 14 #Vue.js
如何在vue中使用HTML 5 拖放API
Jan 14 #Vue.js
Vue中引入svg图标的两种方式
Jan 14 #Vue.js
You might like
php数据库的增删改查 php与javascript之间的交互
2017/08/31 PHP
Jquery 获取表单text,areatext,radio,checkbox,select值的代码
2009/11/12 Javascript
Dom 是什么的详细说明
2010/10/25 Javascript
在iframe里的页面编写js,实现在父窗口上创建动画效果展开和收缩的div(不变动iframe父窗口代码)
2011/12/20 Javascript
深入理解JavaScript系列(3) 全面解析Module模式
2012/01/15 Javascript
悬浮数字的实现案例
2014/02/19 Javascript
JavaScript数据类型详解
2015/04/01 Javascript
微信小程序排坑指南详解
2018/05/23 Javascript
浅谈javascript中的prototype和__proto__的理解
2019/04/07 Javascript
vuex的使用和简易实现
2021/01/07 Vue.js
[07:06]2018DOTA2国际邀请赛寻真——卫冕冠军Team Liquid
2018/08/10 DOTA
Django实现图片文字同时提交的方法
2015/05/26 Python
python如何获取服务器硬件信息
2017/05/11 Python
解决出现Incorrect integer value: '' for column 'id' at row 1的问题
2017/10/29 Python
python中使用%与.format格式化文本方法解析
2017/12/27 Python
PowerBI和Python关于数据分析的对比
2019/07/11 Python
python爬虫 猫眼电影和电影天堂数据csv和mysql存储过程解析
2019/09/05 Python
Python编译为二进制so可执行文件实例
2019/12/23 Python
python print 格式化输出,动态指定长度的实现
2020/04/12 Python
Python中zipfile压缩文件模块的基本使用教程
2020/06/14 Python
python编程的核心知识点总结
2021/02/08 Python
解析HTML5的存储功能和web SQL的相关操作方法
2016/02/19 HTML / CSS
AVON雅芳官网:世界上最大的美容化妆品公司之一
2016/11/02 全球购物
如何减少垃圾回收让内存更加有效使用
2013/10/18 面试题
大学生毕业自我鉴定范文
2013/11/03 职场文书
培训专员岗位职责
2014/02/26 职场文书
行政专员求职信范文
2014/05/03 职场文书
初中新生军训方案
2014/05/13 职场文书
税务干部群众路线教育实践活动自我剖析材料
2014/09/21 职场文书
2014年酒店年度工作总结
2014/12/10 职场文书
新教师个人工作总结
2015/02/06 职场文书
大学生求职信怎么写
2015/03/19 职场文书
歌咏比赛口号大全
2015/12/25 职场文书
浅谈redis五大数据结构和使用场景
2021/04/12 Redis
MySQL中rank() over、dense_rank() over、row_number() over用法介绍
2022/03/23 MySQL
《遗弃》开发商删推文要跑路?官方回应:还在开发
2022/04/03 其他游戏