repeated-items.html 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Vue.js repeated items</title>
  5. <meta charset="utf-8">
  6. <script src="../../../dist/vue.js"></script>
  7. </head>
  8. <body>
  9. <div id="app">
  10. <p>
  11. <button class="push" v-on="click:push">push</button>
  12. <button class="pop" v-on="click:pop">pop</button>
  13. <button class="shift" v-on="click:shift">shift</button>
  14. <button class="unshift" v-on="click:unshift">unshift</button>
  15. <button class="splice" v-on="click:splice">splice</button>
  16. <button class="remove" v-on="click:remove">remove</button>
  17. <button class="replace" v-on="click:replace">replace</button>
  18. <button class="sort" v-on="click:sort">sort</button>
  19. <button class="reverse" v-on="click:reverse">reverse</button>
  20. </p>
  21. <p>Total items: <span class="count" v-text="items.length"></span></p>
  22. <ul>
  23. <li class="item" v-repeat="items">
  24. {{$index}} {{title}}
  25. </li>
  26. </ul>
  27. </div>
  28. <script>
  29. //Vue.config({debug: true})
  30. var items = [
  31. { title: 'A'},
  32. { title: 'B'},
  33. { title: 'C'}
  34. ]
  35. var demo = new Vue({
  36. el: '#app',
  37. data: {
  38. items: items,
  39. push: function () {
  40. this.items.push({ title: getChar() })
  41. },
  42. pop: function () {
  43. this.items.pop()
  44. },
  45. shift: function () {
  46. this.items.shift()
  47. },
  48. unshift: function () {
  49. this.items.unshift({ title: getChar() })
  50. },
  51. splice: function () {
  52. this.items.splice(1, 1, { title: getChar() }, { title: getChar() })
  53. },
  54. replace: function () {
  55. this.items.replace(getPos(), { title: getChar() })
  56. },
  57. remove: function () {
  58. this.items.remove(getPos())
  59. },
  60. sort: function () {
  61. this.items.sort(function (a, b) {
  62. return a.title.charCodeAt(0) - b.title.charCodeAt(0)
  63. })
  64. },
  65. reverse: function () {
  66. this.items.reverse()
  67. }
  68. }
  69. })
  70. var getChar = (function () {
  71. var count = 0
  72. return function () {
  73. return (count++).toString()
  74. }
  75. })()
  76. function getPos () {
  77. return items.length - 1
  78. }
  79. </script>
  80. </body>
  81. </html>