Vue实现简单的列表增删改查

淩亂°似流年 2022-09-14 15:23 321阅读 0赞
首先引入样式文件bootstrap.min.css和vue.js
  1. <link href="~/Content/css/bootstrap.min.css" rel="stylesheet" />
  2. <script src="~/Content/js/vue.js"></script>
然后搭建出列表的新增输入框以及搜索框和展示列表
  1. <div id="app">
  2. <div class="panel panel-primary">
  3. <div class="panel-heading">
  4. <h3 class="panel-title">添加品牌</h3>
  5. </div>
  6. <div class="panel-body form-inline">
  7. <label>
  8. id:
  9. <input type="text" class="form-control" v-model="newid" />
  10. </label>
  11. <label>
  12. name:
  13. <!-- enter回车保存-->
  14. <input @keyup.enter="add()" type="text" class="form-control" v-model="newname" />
  15. </label>
  16. <!-- 在vue中使用事件绑定机制,可以加()给函数传参 -->
  17. <input @click="add()" type="button" value="添加" class="btn btn-primary" />
  18. <label>
  19. 搜索关键字:
  20. <!-- 自定义指令 v-focus-->
  21. <input v-color="'blue'" v-focus type="text" class="form-control" v-model="keywords" />
  22. </label>
  23. </div>
  24. </div>
  25. <table class="table table-bordered table-hover table-striped">
  26. <thead>
  27. <tr>
  28. <th>id</th>
  29. <th>Name</th>
  30. <th>ctime</th>
  31. <th>操作</th>
  32. </tr>
  33. </thead>
  34. <tbody>
  35. <!-- 之前从list中直接渲染过来,现在自定义search方法,把关键字传递给search -->
  36. <tr v-for="item in search(keywords)" :ket="item">
  37. <td>{
  38. {item.id}}</td>
  39. <td>{
  40. {item.name}}</td>
  41. <td>{
  42. {item.ctime | dateFormat}}</td>
  43. <td>
  44. <a href="" @click.prevent="del(item.id)">删除</a>
  45. </td>
  46. </tr>
  47. </tbody>
  48. </table>
  49. </div>

实现效果
在这里插入图片描述

页面搭建完成后,然后使用vue实现品牌的查询、新增和删除功能。
  1. <script type="text/javascript">
  2. var vm = new Vue({
  3. el: '#app',
  4. data: {
  5. newid: '',
  6. newname: '',
  7. keywords: '',
  8. //品牌数据
  9. list: [
  10. { id: 1, name: '小米', ctime: new Date() },
  11. { id: 2, name: '苹果', ctime: new Date() },
  12. { id: 3, name: '华为', ctime: new Date() },
  13. { id: 4, name: 'vivo', ctime: new Date() },
  14. { id: 5, name: 'oppo', ctime: new Date() },
  15. ]
  16. },
  17. methods: {
  18. //添加点击事件
  19. add() {
  20. //通过数据双向绑定将vue的数据应用到页面上
  21. var newid = this.newid;
  22. var newname = this.newname;
  23. var newdata = new Date();
  24. this.list.push({ id: newid, name: newname, ctime: newdata });
  25. this.newid = this.newname = "";
  26. },
  27. del(id) { //根据id删除数据
  28. var index = this.list.findIndex(item => {
  29. if (item.id == id) {
  30. return true;
  31. }
  32. });
  33. this.list.splice(index, 1);
  34. },
  35. search(keywords) { //根据关键字进行数据搜索
  36. //es6 新方法includes() 判断是否包含字符串 包含返回true
  37. return this.list.filter(item => {
  38. if (item.name.includes(keywords)) {
  39. return item;
  40. }
  41. });
  42. }
  43. }
  44. });
  45. </script>

实现效果
在这里插入图片描述

页面上还需要进行转换,可以使用vue过滤器转换时间格式
  1. <script type="text/javascript">
  2. //定义过滤器,进行时间格式化
  3. Vue.filter('dateFormat', function (datestr) {
  4. var dt = new Date(datestr);
  5. var y = dt.getFullYear();
  6. //padStart自动补0
  7. var m = (dt.getMonth() + 1).toString().padStart(2, '0');
  8. var d = (dt.getDate()).toString().padStart(2, '0');
  9. return `${ y}-${ m}-${ d}`;
  10. });
  11. </script>

实现效果
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,321人围观)

还没有评论,来说两句吧...

相关阅读