buffer-grid.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Ext.Loader.setConfig({enabled: true});
  2. Ext.Loader.setPath('Ext.ux', '../ux/');
  3. Ext.require([
  4. 'Ext.grid.*',
  5. 'Ext.data.*',
  6. 'Ext.util.*',
  7. 'Ext.grid.PagingScroller'
  8. ]);
  9. Ext.define('Employee', {
  10. extend: 'Ext.data.Model',
  11. fields: [
  12. {name: 'rating', type: 'int'},
  13. {name: 'salary', type: 'float'},
  14. {name: 'name'}
  15. ]
  16. });
  17. Ext.onReady(function(){
  18. /**
  19. * Returns an array of fake data
  20. * @param {Number} count The number of fake rows to create data for
  21. * @return {Array} The fake record data, suitable for usage with an ArrayReader
  22. */
  23. function createFakeData(count) {
  24. var firstNames = ['Ed', 'Tommy', 'Aaron', 'Abe', 'Jamie', 'Adam', 'Dave', 'David', 'Jay', 'Nicolas', 'Nige'],
  25. lastNames = ['Spencer', 'Maintz', 'Conran', 'Elias', 'Avins', 'Mishcon', 'Kaneda', 'Davis', 'Robinson', 'Ferrero', 'White'],
  26. ratings = [1, 2, 3, 4, 5],
  27. salaries = [100, 400, 900, 1500, 1000000];
  28. var data = [];
  29. for (var i = 0; i < (count || 25); i++) {
  30. var ratingId = Math.floor(Math.random() * ratings.length),
  31. salaryId = Math.floor(Math.random() * salaries.length),
  32. firstNameId = Math.floor(Math.random() * firstNames.length),
  33. lastNameId = Math.floor(Math.random() * lastNames.length),
  34. rating = ratings[ratingId],
  35. salary = salaries[salaryId],
  36. name = Ext.String.format("{0} {1}", firstNames[firstNameId], lastNames[lastNameId]);
  37. data.push({
  38. rating: rating,
  39. salary: salary,
  40. name: name
  41. });
  42. }
  43. return data;
  44. }
  45. // Create the Data Store.
  46. // Because it is buffered, the automatic load will be directed
  47. // through the prefetch mechanism, and be read through the page cache
  48. var store = Ext.create('Ext.data.Store', {
  49. id: 'store',
  50. // allow the grid to interact with the paging scroller by buffering
  51. buffered: true,
  52. // Configure the store with a single page of records which will be cached
  53. pageSize: 5000,
  54. data: createFakeData(5000),
  55. model: 'Employee',
  56. proxy: {
  57. type: 'memory'
  58. }
  59. });
  60. var grid = Ext.create('Ext.grid.Panel', {
  61. width: 700,
  62. height: 500,
  63. title: 'Bufffered Grid of 5,000 random records',
  64. store: store,
  65. loadMask: true,
  66. selModel: {
  67. pruneRemoved: false
  68. },
  69. viewConfig: {
  70. trackOver: false
  71. },
  72. // grid columns
  73. columns:[{
  74. xtype: 'rownumberer',
  75. width: 40,
  76. sortable: false
  77. },{
  78. text: 'Name',
  79. flex:1 ,
  80. sortable: true,
  81. dataIndex: 'name'
  82. },{
  83. text: 'Rating',
  84. width: 125,
  85. sortable: true,
  86. dataIndex: 'rating'
  87. },{
  88. text: 'Salary',
  89. width: 125,
  90. sortable: true,
  91. dataIndex: 'salary',
  92. align: 'right',
  93. renderer: Ext.util.Format.usMoney
  94. }],
  95. renderTo: Ext.getBody()
  96. });
  97. });