index.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title></title>
  6. <script src="../../dist/vue.min.js"></script>
  7. <link rel="stylesheet" href="style.css">
  8. <link rel="stylesheet" href="demo.css">
  9. </head>
  10. <body>
  11. <div id="el">
  12. <h1>Rendering Dynamic Big Table</h1>
  13. <p>Reference: <a href="http://insin.github.io/ui-lib-samples/large-datasets/index.html">insin/ui-lib-samples/large-datasets</a></p>
  14. <p>
  15. <span>{{ rows }} x {{ cols }}, {{ optimized ? 'with' : 'without' }} optimization. {{ msg }}</span>
  16. </p>
  17. <p>
  18. <button v-if="optimized" @click="loadBase">Disable optimization</button>
  19. <button v-else @click="loadOptimized">Enable optimization (Object.freeze)</button>
  20. <button @click="unmount">Unmount</button>
  21. <button @click="rerender">Rerender with fresh data</button>
  22. </p>
  23. <form>
  24. <strong>Filter Data</strong>:
  25. <input type="text" v-model="filter">
  26. <!--
  27. If the user is filtering the data, we want to offer some insight into
  28. the breadth of the filtering.
  29. -->
  30. <span v-if="filter">
  31. &mdash;
  32. Filtering <strong>{{ filter }}</strong>
  33. over {{ dataPoints }} data points,
  34. {{ visibleCount() }} found.
  35. </span>
  36. </form>
  37. <table width="100%" cellspacing="2" :class="{ filtered: filter }">
  38. <tr v-for="row in grid">
  39. <th>{{ row.id }}</th>
  40. <td v-for="item in row.items"
  41. class="item"
  42. :class="{ hidden: !matches(item) }">
  43. {{ item.value }}
  44. </td>
  45. </tr>
  46. </table>
  47. </div>
  48. <script>
  49. var ROWS = 1000
  50. var COLS = 10
  51. var OPTIMIZED = window.location.hash === '#optimized'
  52. window.onhashchange = function () {
  53. window.location.reload()
  54. }
  55. function generateGrid( rowCount, columnCount ) {
  56. var valuePoints = [
  57. "Daenerys", "Jon", "Sansa", "Arya", "Stannis", "Gregor", "Tyrion",
  58. "Theon", "Joffrey", "Ramsay", "Cersei", "Bran", "Margaery",
  59. "Melisandre", "Daario", "Jamie", "Eddard", "Myrcella", "Robb",
  60. "Jorah", "Petyr", "Tommen", "Sandor", "Oberyn", "Drogo", "Ygritte"
  61. ]
  62. var valueIndex = 0
  63. var grid = []
  64. for ( var r = 0; r < rowCount; r++ ) {
  65. var row = {
  66. id: r,
  67. items: []
  68. }
  69. for ( var c = 0; c < columnCount; c++ ) {
  70. row.items.push({
  71. id: ( r + "-" + c ),
  72. value: valuePoints[ valueIndex ]
  73. })
  74. if ( ++valueIndex >= valuePoints.length ) {
  75. valueIndex = 0
  76. }
  77. }
  78. grid.push(row)
  79. }
  80. return OPTIMIZED ? Object.freeze(grid) : grid
  81. }
  82. var grid = generateGrid(ROWS, COLS)
  83. var s = window.performance.now()
  84. console.profile('a')
  85. var vm = new Vue({
  86. el: '#el',
  87. data: {
  88. cols: COLS,
  89. rows: ROWS,
  90. optimized: OPTIMIZED,
  91. msg: 'loading...',
  92. grid: grid,
  93. dataPoints: grid.length * grid[0].items.length,
  94. filter: ''
  95. },
  96. methods: {
  97. matches: function (item) {
  98. return item.value.toLowerCase().indexOf(this.filter.toLowerCase()) > -1
  99. },
  100. visibleCount: function () {
  101. var count = 0
  102. var grid = this.grid
  103. for (var i = 0, l = grid.length; i < l; i++) {
  104. var row = grid[i].items
  105. for (var j = 0, k = row.length; j < k; j++) {
  106. var item = row[j]
  107. var matched = !this.filter || this.matches(item)
  108. if (matched) {
  109. count++
  110. }
  111. }
  112. }
  113. return count
  114. },
  115. loadBase: function () {
  116. window.location.hash = ''
  117. },
  118. loadOptimized: function () {
  119. window.location.hash = '#optimized'
  120. },
  121. unmount: function () {
  122. console.profile('unmount')
  123. var s = window.performance.now()
  124. this.grid = []
  125. setTimeout(function () {
  126. vm.msg = 'umount took: ' + (window.performance.now() - s).toFixed(2) + 'ms'
  127. console.profileEnd('unmount')
  128. }, 0)
  129. },
  130. rerender: function () {
  131. var grid = generateGrid(1000, 10)
  132. var s = window.performance.now()
  133. console.profile('rerender')
  134. this.grid = grid
  135. setTimeout(function () {
  136. vm.msg = 'rerender took: ' + (window.performance.now() - s).toFixed(2) + 'ms'
  137. console.profileEnd('rerender')
  138. }, 0)
  139. }
  140. }
  141. })
  142. console.profileEnd('a')
  143. setTimeout(function () {
  144. vm.msg = 'initial render took: ' + (window.performance.now() - s).toFixed(2) + 'ms'
  145. }, 0)
  146. </script>
  147. </body>
  148. </html>