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表单验证之禁止input输入框输入空格
Dec 03 Vue.js
vue+openlayers绘制省市边界线
Dec 24 Vue.js
vue 使用rules对表单字段进行校验的步骤
Dec 25 Vue.js
为什么推荐使用JSX开发Vue3
Dec 28 Vue.js
使用Vue.js和MJML创建响应式电子邮件
Mar 23 Vue.js
Vue过滤器(filter)实现及应用场景详解
Jun 15 Vue.js
vue3获取当前路由地址
Feb 18 Vue.js
Axios代理配置及封装响应拦截处理方式
Apr 07 Vue.js
分享一个vue实现的记事本功能案例
Apr 11 Vue.js
Vue OpenLayer 为地图绘制风场效果
Apr 24 Vue.js
Vue操作Storage本地化存储
Apr 29 Vue.js
解决vue自定义组件@click点击失效问题
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
漫威DC即将合作联动,而双方早已经秘密开始
2020/04/09 欧美动漫
解析PHP 使用curl提交json格式数据
2013/06/29 PHP
php使用curl获取https请求的方法
2015/02/11 PHP
PHP保存session到memcache服务器的方法
2016/01/19 PHP
php中目录操作opendir()、readdir()及scandir()用法示例
2019/06/08 PHP
自定义Laravel (monolog)日志位置,并增加请求ID的实现
2019/10/17 PHP
Javascript中的常见排序算法
2007/03/27 Javascript
javascript 跨浏览器开发经验总结(五) js 事件
2010/05/19 Javascript
基于JQuery的一个简单的鼠标跟随提示效果
2010/09/23 Javascript
用js判断页面是否加载完成实现代码
2012/12/11 Javascript
node.js中Socket.IO的进阶使用技巧
2014/11/04 Javascript
JavaScript的this关键字的理解
2016/06/18 Javascript
javascript函数中的3个高级技巧
2016/09/22 Javascript
利用js编写网页进度条效果
2017/10/08 Javascript
clipboard.js在移动端复制失败的解决方法
2018/06/13 Javascript
vue实现同一个页面可以有多个router-view的方法
2018/09/20 Javascript
详解vuex commit保存数据技巧
2018/12/25 Javascript
D3.js(v3)+react 实现带坐标与比例尺的柱形图 (V3版本)
2019/05/09 Javascript
[01:18]DOTA2超级联赛专访hanci ForLove淘汰感言曝光
2013/06/04 DOTA
python中异常捕获方法详解
2017/03/03 Python
Python进阶-函数默认参数(详解)
2017/05/18 Python
python定时利用QQ邮件发送天气预报的实例
2017/11/17 Python
Python简单实现网页内容抓取功能示例
2018/06/07 Python
Python增强赋值和共享引用注意事项小结
2019/05/28 Python
python实现最大子序和(分治+动态规划)
2019/07/05 Python
Python 实现自动完成A4标签排版打印功能
2020/04/09 Python
python判断是空的实例分享
2020/07/06 Python
HTML5 b和i标记将被赋予真正的语义
2009/07/16 HTML / CSS
菲律宾优惠券网站:MetroDeal
2019/04/12 全球购物
影子教师研修方案
2014/06/14 职场文书
担保书范本
2015/01/20 职场文书
小学生一年级(书信作文)
2019/08/13 职场文书
Python包argparse模块常用方法
2021/06/04 Python
golang fmt格式“占位符”的实例用法详解
2021/07/04 Golang
Netty客户端接入流程NioSocketChannel创建解析
2022/03/25 Java/Android
最新动漫情报:2022年7月新番定档超过30部, OVERLORD骨王第四季也在其中噢
2022/05/04 日漫