Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5.  
  6. <title>
  7. Rendering Large Datasets With Angular 2 Beta 3
  8. </title>
  9.  
  10. <link rel="stylesheet" type="text/css" href="./demo.css"></link>
  11. </head>
  12. <body>
  13.  
  14. <h1>
  15. Rendering Large Datasets With Angular 2 Beta 3
  16. </h1>
  17.  
  18. <my-app>
  19. Loading...
  20. </my-app>
  21.  
  22. <!-- Load demo scripts. -->
  23. <script type="text/javascript" src="../../vendor/angularjs-2-beta/3/es6-shim.min.js"></script>
  24. <script type="text/javascript" src="../../vendor/angularjs-2-beta/3/Rx.umd.min.js"></script>
  25. <script type="text/javascript" src="../../vendor/angularjs-2-beta/3/angular2-polyfills.min.js"></script>
  26. <!-- CAUTION: This demo does not work with the minified UMD code. -->
  27. <script type="text/javascript" src="../../vendor/angularjs-2-beta/3/angular2-all.umd.js"></script>
  28. <!-- AlmondJS - minimal implementation of RequireJS. -->
  29. <script type="text/javascript" src="../../vendor/angularjs-2-beta/3/almond.js"></script>
  30. <script type="text/javascript">
  31.  
  32. // Enabling production mode to make sure there's no performance degradation
  33. // due to assertions and other checks the framework might be doing during the
  34. // execution of the application.
  35. ng.core.enableProdMode();
  36.  
  37. // Defer bootstrapping until all of the components have been declared.
  38. // --
  39. // NOTE: Not all components have to be required here since they will be
  40. // implicitly required by other components.
  41. requirejs(
  42. [ "AppComponent" ],
  43. function run( AppComponent ) {
  44.  
  45. ng.platform.browser.bootstrap( AppComponent );
  46.  
  47. }
  48. );
  49.  
  50.  
  51. // --------------------------------------------------------------------------- //
  52. // --------------------------------------------------------------------------- //
  53.  
  54.  
  55. // I provide the root App component.
  56. define(
  57. "AppComponent",
  58. function registerAppComponent() {
  59.  
  60. // Configure the app component definition.
  61. ng.core
  62. .Component({
  63. selector: "my-app",
  64. template:
  65. `
  66. <form>
  67. <strong>Filter Data</strong>:
  68.  
  69. <input
  70. type="text"
  71. [(ngModel)]="form.filter"
  72. (input)="handleFilterChange( $event.target.value )"
  73. />
  74.  
  75. <span *ngIf="form.filter">
  76. &mdash;
  77. Filtering <strong>{{ form.filter }}</strong>
  78. over {{ dataPoints }} data points,
  79. {{ visibleCount }} found.
  80. </span>
  81.  
  82. <a *ngIf="grid.length" (click)="unmountGrid()">Unmount Grid</a>
  83. <a *ngIf="! grid.length" (click)="remountGrid()">Remount Grid</a>
  84. </form>
  85.  
  86. <table width="100%" cellspacing="2" [class.filtered]="form.filter">
  87. <tr *ngFor="#row of grid">
  88. <td>
  89. {{ row.id }}
  90. </td>
  91. <td
  92. *ngFor="#item of row.items"
  93. class="item"
  94. [class.hidden]="item.isHiddenByFilter">
  95. {{ item.value }}
  96. </td>
  97. </tr>
  98. </table>
  99. `
  100. })
  101. .Class({
  102. constructor: AppController
  103. })
  104. ;
  105.  
  106. return( AppController );
  107.  
  108.  
  109. // I control the App component.
  110. function AppController() {
  111.  
  112. var vm = this;
  113.  
  114. // We'll start out with a grid with 10,000 items.
  115. vm.grid = generateGrid( 1000, 10 );
  116.  
  117. // Calculate the number of data-points that may have filtering.
  118. vm.dataPoints = ( vm.grid.length * vm.grid[ 0 ].items.length );
  119.  
  120. // I hold the number of items that are visible based on filtering.
  121. vm.visibleCount = 0;
  122.  
  123. // I hold the form data for use with ngModel.
  124. vm.form = {
  125. filter: ""
  126. };
  127.  
  128. // Expose the public API.
  129. vm.handleFilterChange = handleFilterChange;
  130. vm.remountGrid = remountGrid;
  131. vm.unmountGrid = unmountGrid;
  132.  
  133.  
  134. // ---
  135. // PUBLIC METHODS.
  136. // ---
  137.  
  138.  
  139. // I update the visibility of the items when the filter is updated.
  140. // --
  141. // CAUTION: The actual value of input is being maintained via ngModel.
  142. // We're just responding to the change event.
  143. function handleFilterChange( newValue ) {
  144.  
  145. // Reset the visible count. As we iterate of the items checking
  146. // for visibility, we can increment this count as necessary.
  147. vm.visibleCount = 0;
  148.  
  149. // We are pre-calculating the column count here because we are
  150. // assuming a uniform column distribution in the grid.
  151. var rowCount = vm.grid.length;
  152. var columnCount = ( vm.grid.length && vm.grid[ 0 ].items.length );
  153.  
  154. for ( var r = 0 ; r < rowCount ; r++ ) {
  155.  
  156. var row = vm.grid[ r ];
  157.  
  158. for ( var c = 0 ; c < columnCount ; c++ ) {
  159.  
  160. var item = row.items[ c ];
  161.  
  162. // The item is hidden if the given filter text cannot be
  163. // found in the value of the item.
  164. item.isHiddenByFilter = ( newValue && ( item.value.indexOf( newValue ) === -1 ) );
  165.  
  166. // If the item isn't hidden, track it as part of the visible
  167. // set of data.
  168. if ( ! item.isHiddenByFilter ) {
  169.  
  170. vm.visibleCount++;
  171.  
  172. }
  173.  
  174. }
  175.  
  176. }
  177.  
  178. }
  179.  
  180.  
  181. // I repopulate the grid with data. This will help separate processing
  182. // performance characteristics from page-load processing.
  183. function remountGrid() {
  184.  
  185. vm.grid = generateGrid( 1000, 10 );
  186. vm.dataPoints = ( vm.grid.length * vm.grid[ 0 ].items.length );
  187.  
  188. vm.visibleCount = 0;
  189. vm.form.filter = "";
  190.  
  191. }
  192.  
  193.  
  194. // I clear the grid of data. This will help separate processing
  195. // performance characteristics from page-load processing.
  196. function unmountGrid() {
  197.  
  198. vm.grid = [];
  199. vm.dataPoints = 0;
  200.  
  201. vm.visibleCount = 0;
  202. vm.form.filter = "";
  203.  
  204. }
  205.  
  206.  
  207. // ---
  208. // PRIVATE METHODS.
  209. // ---
  210.  
  211.  
  212. // I generate a grid of items with the given dimensions. The grid is
  213. // represented as a two dimensional grid, of sorts. Each row has an
  214. // object that has an items collection.
  215. function generateGrid( rowCount, columnCount ) {
  216.  
  217. var valuePoints = [
  218. "Daenerys", "Jon", "Sansa", "Arya", "Stannis", "Gregor", "Tyrion",
  219. "Theon", "Joffrey", "Ramsay", "Cersei", "Bran", "Margaery",
  220. "Melisandre", "Daario", "Jamie", "Eddard", "Myrcella", "Robb",
  221. "Jorah", "Petyr", "Tommen", "Sandor", "Oberyn", "Drogo", "Ygritte"
  222. ];
  223.  
  224. var valueIndex = 0;
  225.  
  226. var grid = [];
  227.  
  228. for ( var r = 0 ; r < rowCount ; r++ ) {
  229.  
  230. var row = {
  231. id: r,
  232. items: []
  233. };
  234.  
  235. for ( var c = 0 ; c < columnCount ; c++ ) {
  236.  
  237. row.items.push({
  238. id: ( r + "-" + c ),
  239. value: valuePoints[ valueIndex ],
  240. isHiddenByFilter: false
  241. });
  242.  
  243. if ( ++valueIndex >= valuePoints.length ) {
  244.  
  245. valueIndex = 0;
  246.  
  247. }
  248.  
  249. }
  250.  
  251. grid.push( row );
  252.  
  253. }
  254.  
  255. return( grid );
  256.  
  257. }
  258.  
  259. }
  260.  
  261. }
  262. );
  263.  
  264. </script>
  265.  
  266. </body>
  267. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement