3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">Ext.ns('Ext.ux.grid');
10 <div id="cls-Ext.ux.grid.BufferView"></div>/**
11 * @class Ext.ux.grid.BufferView
12 * @extends Ext.grid.GridView
13 * A custom GridView which renders rows on an as-needed basis.
15 Ext.ux.grid.BufferView = Ext.extend(Ext.grid.GridView, {
16 <div id="cfg-Ext.ux.grid.BufferView-rowHeight"></div>/**
17 * @cfg {Number} rowHeight
18 * The height of a row in the grid.
22 <div id="cfg-Ext.ux.grid.BufferView-borderHeight"></div>/**
23 * @cfg {Number} borderHeight
24 * The combined height of border-top and border-bottom of a row.
28 <div id="cfg-Ext.ux.grid.BufferView-scrollDelay"></div>/**
29 * @cfg {Boolean/Number} scrollDelay
30 * The number of milliseconds before rendering rows out of the visible
31 * viewing area. Defaults to 100. Rows will render immediately with a config
36 <div id="cfg-Ext.ux.grid.BufferView-cacheSize"></div>/**
37 * @cfg {Number} cacheSize
38 * The number of rows to look forward and backwards from the currently viewable
39 * area. The cache applies only to rows that have been rendered already.
43 <div id="cfg-Ext.ux.grid.BufferView-cleanDelay"></div>/**
44 * @cfg {Number} cleanDelay
45 * The number of milliseconds to buffer cleaning of extra rows not in the
50 initTemplates : function(){
51 Ext.ux.grid.BufferView.superclass.initTemplates.call(this);
52 var ts = this.templates;
53 // empty div to act as a place holder for a row
54 ts.rowHolder = new Ext.Template(
55 '<div class="x-grid3-row {alt}" style="{tstyle}"></div>'
57 ts.rowHolder.disableFormats = true;
58 ts.rowHolder.compile();
60 ts.rowBody = new Ext.Template(
61 '<table class="x-grid3-row-table" border="0" cellspacing="0" cellpadding="0" style="{tstyle}">',
62 '<tbody><tr>{cells}</tr>',
63 (this.enableRowBody ? '<tr class="x-grid3-row-body-tr" style="{bodyStyle}"><td colspan="{cols}" class="x-grid3-body-cell" tabIndex="0" hidefocus="on"><div class="x-grid3-row-body">{body}</div></td></tr>' : ''),
66 ts.rowBody.disableFormats = true;
70 getStyleRowHeight : function(){
71 return Ext.isBorderBox ? (this.rowHeight + this.borderHeight) : this.rowHeight;
74 getCalculatedRowHeight : function(){
75 return this.rowHeight + this.borderHeight;
78 getVisibleRowCount : function(){
79 var rh = this.getCalculatedRowHeight();
80 var visibleHeight = this.scroller.dom.clientHeight;
81 return (visibleHeight < 1) ? 0 : Math.ceil(visibleHeight / rh);
84 getVisibleRows: function(){
85 var count = this.getVisibleRowCount();
86 var sc = this.scroller.dom.scrollTop;
87 var start = (sc == 0 ? 0 : Math.floor(sc/this.getCalculatedRowHeight())-1);
89 first: Math.max(start, 0),
90 last: Math.min(start + count + 2, this.ds.getCount()-1)
94 doRender : function(cs, rs, ds, startRow, colCount, stripe, onlyBody){
95 var ts = this.templates, ct = ts.cell, rt = ts.row, rb = ts.rowBody, last = colCount-1;
96 var rh = this.getStyleRowHeight();
97 var vr = this.getVisibleRows();
98 var tstyle = 'width:'+this.getTotalWidth()+';height:'+rh+'px;';
100 var buf = [], cb, c, p = {}, rp = {tstyle: tstyle}, r;
101 for (var j = 0, len = rs.length; j < len; j++) {
103 var rowIndex = (j+startRow);
104 var visible = rowIndex >= vr.first && rowIndex <= vr.last;
106 for (var i = 0; i < colCount; i++) {
109 p.css = i == 0 ? 'x-grid3-cell-first ' : (i == last ? 'x-grid3-cell-last ' : '');
110 p.attr = p.cellAttr = "";
111 p.value = c.renderer(r.data[c.name], p, r, rowIndex, i, ds);
113 if (p.value == undefined || p.value === "") {
116 if (r.dirty && typeof r.modified[c.name] !== 'undefined') {
117 p.css += ' x-grid3-dirty-cell';
119 cb[cb.length] = ct.apply(p);
123 if(stripe && ((rowIndex+1) % 2 == 0)){
124 alt[0] = "x-grid3-row-alt";
127 alt[1] = " x-grid3-dirty-row";
130 if(this.getRowClass){
131 alt[2] = this.getRowClass(r, rowIndex, rp, ds);
133 rp.alt = alt.join(" ");
134 rp.cells = cb.join("");
135 buf[buf.length] = !visible ? ts.rowHolder.apply(rp) : (onlyBody ? rb.apply(rp) : rt.apply(rp));
140 isRowRendered: function(index){
141 var row = this.getRow(index);
142 return row && row.childNodes.length > 0;
145 syncScroll: function(){
146 Ext.ux.grid.BufferView.superclass.syncScroll.apply(this, arguments);
150 // a (optionally) buffered method to update contents of gridview
152 if (this.scrollDelay) {
153 if (!this.renderTask) {
154 this.renderTask = new Ext.util.DelayedTask(this.doUpdate, this);
156 this.renderTask.delay(this.scrollDelay);
162 onRemove : function(ds, record, index, isUpdate){
163 Ext.ux.grid.BufferView.superclass.onRemove.apply(this, arguments);
164 if(isUpdate !== true){
169 doUpdate: function(){
170 if (this.getVisibleRowCount() > 0) {
171 var g = this.grid, cm = g.colModel, ds = g.store;
172 var cs = this.getColumnData();
174 var vr = this.getVisibleRows();
175 for (var i = vr.first; i <= vr.last; i++) {
176 // if row is NOT rendered and is visible, render it
177 if(!this.isRowRendered(i)){
178 var html = this.doRender(cs, [ds.getAt(i)], ds, i, cm.getColumnCount(), g.stripeRows, true);
179 this.getRow(i).innerHTML = html;
186 // a buffered method to clean rows
189 this.cleanTask = new Ext.util.DelayedTask(this.doClean, this);
191 this.cleanTask.delay(this.cleanDelay);
195 if (this.getVisibleRowCount() > 0) {
196 var vr = this.getVisibleRows();
197 vr.first -= this.cacheSize;
198 vr.last += this.cacheSize;
200 var i = 0, rows = this.getRows();
201 // if first is less than 0, all rows have been rendered
202 // so lets clean the end...
206 for(var len = this.ds.getCount(); i < len; i++){
207 // if current row is outside of first and last and
208 // has content, update the innerHTML to nothing
209 if ((i < vr.first || i > vr.last) && rows[i].innerHTML) {
210 rows[i].innerHTML = '';
217 Ext.ux.grid.BufferView.superclass.layout.call(this);