• node32-综合案例图书管理9


     

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Documenttitle>
    7. head>
    8. <style>
    9. .grid .total {
    10. height: 30px;
    11. line-height: 30px;
    12. background-color: palegoldenrod;
    13. }
    14. style>
    15. <body>
    16. <div id="app">
    17. <div class="grid">
    18. <div>
    19. <h1>图书管理h1>
    20. <div class="book">
    21. <div>
    22. <label for="id">
    23. 编号:
    24. label>
    25. <input type="text" id="id" v-model='id' :disabled='flag'>
    26. <label for="name">
    27. 名称:
    28. label>
    29. <input type="text" id="name" v-model='name'>
    30. <button @click='handle'>提交button>
    31. div>
    32. div>
    33. div>
    34. <div class="total">
    35. <span>图书总数:span>
    36. <span>{{total}}span>
    37. div>
    38. <table>
    39. <thead>
    40. <tr>
    41. <th>编号th>
    42. <th>名称th>
    43. <th>时间th>
    44. <th>操作th>
    45. tr>
    46. thead>
    47. <tbody>
    48. <tr :key='item.id' v-for='item in books'>
    49. <td>{{item.id}}td>
    50. <td>{{item.name}}td>
    51. <td>{{date|format('yyyy-MM-dd')}}td>
    52. <td><a href="" @click.prevent='toEdit(item.id)'>修改a>
    53. <span>|span>
    54. <a href="" @click.prevent='deleteBook(item.id)'>删除a>
    55. td>
    56. tr>
    57. tbody>
    58. table>
    59. div>
    60. div>
    61. <script type="text/javascript" src="./js/vue.js">script>
    62. <script>
    63. Vue.filter('format', function(value, arg) {
    64. //console.log(arg);
    65. if (arg == 'yyyy-MM-dd') {
    66. var ret = '';
    67. ret += value.getFullYear() + '-' + (value.getMonth() + 1) + '-' + value.getDate();
    68. return ret;
    69. }
    70. return value;
    71. })
    72. var vm = new Vue({
    73. el: '#app',
    74. data: {
    75. flag: false,
    76. id: '',
    77. name: '',
    78. date: new Date(),
    79. books: [{
    80. id: 1,
    81. name: '三国演义',
    82. }, {
    83. id: 2,
    84. name: '三国演义',
    85. date: ''
    86. }, {
    87. id: 3,
    88. name: '三国演义',
    89. date: ''
    90. }, {
    91. id: 4,
    92. name: '三国演义',
    93. date: ''
    94. }]
    95. },
    96. methods: {
    97. handle: function() {
    98. if (this.flag) {
    99. //编辑操作
    100. //就是根据当前的id更新数组中的数据
    101. this.books.some((item) => {
    102. if (item.id == this.id) {
    103. item.name = this.name;
    104. //完成遍历之后 终止循环
    105. return true;
    106. }
    107. });
    108. this.flag = false;
    109. } else {
    110. //添加图书
    111. var book = {};
    112. book.id = this.id;
    113. book.name = this.name;
    114. book.date = '';
    115. this.books.push(book);
    116. //清空表单
    117. this.id = '';
    118. this.name = '';
    119. }
    120. this.id = '';
    121. this.name = '';
    122. },
    123. toEdit: function(id) {
    124. //禁止修改id
    125. this.flag = true;
    126. console.log(id);
    127. //根据id查询要编辑的数据
    128. var book = this.books.filter(function(item) {
    129. return item.id == id;
    130. });
    131. console.log(book);
    132. this.id = book[0].id;
    133. this.name = book[0].name;
    134. },
    135. deleteBook: function(id) {
    136. //根据id从数组中查找索引
    137. /* this.books.findIndex(function(item) {
    138. return item.id == id;
    139. }); */
    140. /* this.books.splice(index, 1); */
    141. //------
    142. //通过数组filter
    143. this.books = this.books.filter(function(item) {
    144. return item.id != id;
    145. });
    146. }
    147. },
    148. computed: {
    149. total: function() {
    150. return this.books.length;
    151. }
    152. }
    153. })
    154. script>
    155. body>
    156. html>

    运行结果

     

  • 相关阅读:
    【PostgreSQL】PostgreSQL 15移除了Stats Collector
    C++11的shared_ptr共享的智能指针
    jxTMS设计思想之能力扩展
    【Linux】解决缓存锁问题:无法获得锁 /var/lib/dpkg/lock-frontend
    DC2DC电源设计注意事项--1,Feedback
    RestCloud ETL实践之无标识位实现增量数据同步
    TPCH_Q4 的分析优化,对子查询中的 Semi-join 优化
    不要忽视web渗透测试在项目中起到的重要性
    C++经典41问(2个小时快速掌握C++)
    【开发利器】使用OpenCV算子工作流高效开发
  • 原文地址:https://blog.csdn.net/qq_41632427/article/details/126566062