repeated-items.html 2.2 KB

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