3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js"><div id="cls-Ext.ListView"></div>/**
\r
9 * @class Ext.ListView
\r
10 * @extends Ext.DataView
\r
11 * <p>Ext.ListView is a fast and light-weight implentation of a
\r
12 * {@link Ext.grid.GridPanel Grid} like view with the following characteristics:</p>
\r
13 * <div class="mdetail-params"><ul>
\r
14 * <li>resizable columns</li>
\r
15 * <li>selectable</li>
\r
16 * <li>column widths are initially proportioned by percentage based on the container
\r
17 * width and number of columns</li>
\r
18 * <li>uses templates to render the data in any required format</li>
\r
19 * <li>no horizontal scrolling</li>
\r
20 * <li>no editing</li>
\r
22 * <p>Example usage:</p>
\r
24 // consume JSON of this form:
\r
28 "name":"dance_fever.jpg",
\r
30 "lastmod":1236974993000,
\r
31 "url":"images\/thumbs\/dance_fever.jpg"
\r
34 "name":"zack_sink.jpg",
\r
36 "lastmod":1236974993000,
\r
37 "url":"images\/thumbs\/zack_sink.jpg"
\r
41 var store = new Ext.data.JsonStore({
\r
42 url: 'get-images.php',
\r
46 {name:'size', type: 'float'},
\r
47 {name:'lastmod', type:'date', dateFormat:'timestamp'}
\r
52 var listView = new Ext.ListView({
\r
55 emptyText: 'No images to display',
\r
56 reserveScrollOffset: true,
\r
62 header: 'Last Modified',
\r
64 dataIndex: 'lastmod',
\r
65 tpl: '{lastmod:date("m-d h:i a")}'
\r
69 tpl: '{size:fileSize}', // format using Ext.util.Format.fileSize()
\r
74 // put it in a Panel so it looks pretty
\r
75 var panel = new Ext.Panel({
\r
81 title:'Simple ListView <i>(0 items selected)</i>',
\r
84 panel.render(document.body);
\r
86 // little bit of feedback
\r
87 listView.on('selectionchange', function(view, nodes){
\r
88 var l = nodes.length;
\r
89 var s = l != 1 ? 's' : '';
\r
90 panel.setTitle('Simple ListView <i>('+l+' item'+s+' selected)</i>');
\r
94 * @param {Object} config
\r
97 Ext.ListView = Ext.extend(Ext.DataView, {
\r
98 <div id="prop-Ext.ListView-disableHeaders"></div>/**
\r
99 * Set this property to <tt>true</tt> to disable the header click handler disabling sort
\r
100 * (defaults to <tt>false</tt>).
\r
102 * @property disableHeaders
\r
104 <div id="cfg-Ext.ListView-hideHeaders"></div>/**
\r
105 * @cfg {Boolean} hideHeaders
\r
106 * <tt>true</tt> to hide the {@link #internalTpl header row} (defaults to <tt>false</tt> so
\r
107 * the {@link #internalTpl header row} will be shown).
\r
109 <div id="cfg-Ext.ListView-itemSelector"></div>/**
\r
110 * @cfg {String} itemSelector
\r
111 * Defaults to <tt>'dl'</tt> to work with the preconfigured <b><tt>{@link Ext.DataView#tpl tpl}</tt></b>.
\r
112 * This setting specifies the CSS selector (e.g. <tt>div.some-class</tt> or <tt>span:first-child</tt>)
\r
113 * that will be used to determine what nodes the ListView will be working with.
\r
115 itemSelector: 'dl',
\r
116 <div id="cfg-Ext.ListView-selectedClass"></div>/**
\r
117 * @cfg {String} selectedClass The CSS class applied to a selected row (defaults to
\r
118 * <tt>'x-list-selected'</tt>). An example overriding the default styling:
\r
120 .x-list-selected {background-color: yellow;}
\r
124 selectedClass:'x-list-selected',
\r
125 <div id="cfg-Ext.ListView-overClass"></div>/**
\r
126 * @cfg {String} overClass The CSS class applied when over a row (defaults to
\r
127 * <tt>'x-list-over'</tt>). An example overriding the default styling:
\r
129 .x-list-over {background-color: orange;}
\r
133 overClass:'x-list-over',
\r
134 <div id="cfg-Ext.ListView-reserveScrollOffset"></div>/**
\r
135 * @cfg {Boolean} reserveScrollOffset
\r
136 * By default will defer accounting for the configured <b><tt>{@link #scrollOffset}</tt></b>
\r
137 * for 10 milliseconds. Specify <tt>true</tt> to account for the configured
\r
138 * <b><tt>{@link #scrollOffset}</tt></b> immediately.
\r
140 <div id="cfg-Ext.ListView-scrollOffset"></div>/**
\r
141 * @cfg {Number} scrollOffset The amount of space to reserve for the scrollbar (defaults to
\r
142 * <tt>19</tt> pixels)
\r
145 <div id="cfg-Ext.ListView-columnResize"></div>/**
\r
146 * @cfg {Boolean/Object} columnResize
\r
147 * Specify <tt>true</tt> or specify a configuration object for {@link Ext.ListView.ColumnResizer}
\r
148 * to enable the columns to be resizable (defaults to <tt>true</tt>).
\r
150 columnResize: true,
\r
151 <div id="cfg-Ext.ListView-columns"></div>/**
\r
152 * @cfg {Array} columns An array of column configuration objects, for example:
\r
158 tpl: '{size:fileSize}',
\r
162 * Acceptable properties for each column configuration object are:
\r
163 * <div class="mdetail-params"><ul>
\r
164 * <li><b><tt>align</tt></b> : String<div class="sub-desc">Set the CSS text-align property
\r
165 * of the column. Defaults to <tt>'left'</tt>.</div></li>
\r
166 * <li><b><tt>dataIndex</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
\r
167 * {@link Ext.grid.Column#dataIndex dataIndex} for details.</div></li>
\r
168 * <li><b><tt>header</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
\r
169 * {@link Ext.grid.Column#header header} for details.</div></li>
\r
170 * <li><b><tt>tpl</tt></b> : String<div class="sub-desc">Specify a string to pass as the
\r
171 * configuration string for {@link Ext.XTemplate}. By default an {@link Ext.XTemplate}
\r
172 * will be implicitly created using the <tt>dataIndex</tt>.</div></li>
\r
173 * <li><b><tt>width</tt></b> : Number<div class="sub-desc">Percentage of the container width
\r
174 * this column should be allocated. Columns that have no width specified will be
\r
175 * allocated with an equal percentage to fill 100% of the container width. To easily take
\r
176 * advantage of the full container width, leave the width of at least one column undefined.
\r
177 * Note that if you do not want to take up the full width of the container, the width of
\r
178 * every column needs to be explicitly defined.</div></li>
\r
181 <div id="cfg-Ext.ListView-columnSort"></div>/**
\r
182 * @cfg {Boolean/Object} columnSort
\r
183 * Specify <tt>true</tt> or specify a configuration object for {@link Ext.ListView.Sorter}
\r
184 * to enable the columns to be sortable (defaults to <tt>true</tt>).
\r
187 <div id="cfg-Ext.ListView-internalTpl"></div>/**
\r
188 * @cfg {String/Array} internalTpl
\r
189 * The template to be used for the header row. See {@link #tpl} for more details.
\r
192 initComponent : function(){
\r
193 if(this.columnResize){
\r
194 this.colResizer = new Ext.ListView.ColumnResizer(this.colResizer);
\r
195 this.colResizer.init(this);
\r
197 if(this.columnSort){
\r
198 this.colSorter = new Ext.ListView.Sorter(this.columnSort);
\r
199 this.colSorter.init(this);
\r
201 if(!this.internalTpl){
\r
202 this.internalTpl = new Ext.XTemplate(
\r
203 '<div class="x-list-header"><div class="x-list-header-inner">',
\r
204 '<tpl for="columns">',
\r
205 '<div style="width:{width}%;text-align:{align};"><em unselectable="on" id="',this.id, '-xlhd-{#}">',
\r
209 '<div class="x-clear"></div>',
\r
211 '<div class="x-list-body"><div class="x-list-body-inner">',
\r
216 this.tpl = new Ext.XTemplate(
\r
217 '<tpl for="rows">',
\r
219 '<tpl for="parent.columns">',
\r
220 '<dt style="width:{width}%;text-align:{align};"><em unselectable="on">',
\r
221 '{[values.tpl.apply(parent)]}',
\r
224 '<div class="x-clear"></div>',
\r
229 var cs = this.columns, allocatedWidth = 0, colsWithWidth = 0, len = cs.length;
\r
230 for(var i = 0; i < len; i++){
\r
233 c.tpl = new Ext.XTemplate('{' + c.dataIndex + '}');
\r
234 }else if(Ext.isString(c.tpl)){
\r
235 c.tpl = new Ext.XTemplate(c.tpl);
\r
237 c.align = c.align || 'left';
\r
238 if(Ext.isNumber(c.width)){
\r
240 allocatedWidth += c.width;
\r
244 // auto calculate missing column widths
\r
245 if(colsWithWidth < len){
\r
246 var remaining = len - colsWithWidth;
\r
247 if(allocatedWidth < 100){
\r
248 var perCol = ((100-allocatedWidth) / remaining);
\r
249 for(var j = 0; j < len; j++){
\r
251 if(!Ext.isNumber(c.width)){
\r
257 Ext.ListView.superclass.initComponent.call(this);
\r
260 onRender : function(){
\r
261 Ext.ListView.superclass.onRender.apply(this, arguments);
\r
263 this.internalTpl.overwrite(this.el, {columns: this.columns});
\r
265 this.innerBody = Ext.get(this.el.dom.childNodes[1].firstChild);
\r
266 this.innerHd = Ext.get(this.el.dom.firstChild.firstChild);
\r
268 if(this.hideHeaders){
\r
269 this.el.dom.firstChild.style.display = 'none';
\r
273 getTemplateTarget : function(){
\r
274 return this.innerBody;
\r
277 <div id="method-Ext.ListView-collectData"></div>/**
\r
278 * <p>Function which can be overridden which returns the data object passed to this
\r
279 * view's {@link #tpl template} to render the whole ListView. The returned object
\r
280 * shall contain the following properties:</p>
\r
281 * <div class="mdetail-params"><ul>
\r
282 * <li><b>columns</b> : String<div class="sub-desc">See <tt>{@link #columns}</tt></div></li>
\r
283 * <li><b>rows</b> : String<div class="sub-desc">See
\r
284 * <tt>{@link Ext.DataView}.{@link Ext.DataView#collectData collectData}</div></li>
\r
286 * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
\r
287 * @param {Number} startIndex the index number of the Record being prepared for rendering.
\r
288 * @return {Object} A data object containing properties to be processed by a repeating
\r
289 * XTemplate as described above.
\r
291 collectData : function(){
\r
292 var rs = Ext.ListView.superclass.collectData.apply(this, arguments);
\r
294 columns: this.columns,
\r
299 verifyInternalSize : function(){
\r
301 this.onResize(this.lastSize.width, this.lastSize.height);
\r
306 onResize : function(w, h){
\r
307 var bd = this.innerBody.dom;
\r
308 var hd = this.innerHd.dom
\r
312 var bdp = bd.parentNode;
\r
313 if(Ext.isNumber(w)){
\r
314 var sw = w - this.scrollOffset;
\r
315 if(this.reserveScrollOffset || ((bdp.offsetWidth - bdp.clientWidth) > 10)){
\r
316 bd.style.width = sw + 'px';
\r
317 hd.style.width = sw + 'px';
\r
319 bd.style.width = w + 'px';
\r
320 hd.style.width = w + 'px';
\r
321 setTimeout(function(){
\r
322 if((bdp.offsetWidth - bdp.clientWidth) > 10){
\r
323 bd.style.width = sw + 'px';
\r
324 hd.style.width = sw + 'px';
\r
329 if(Ext.isNumber(h == 'number')){
\r
330 bdp.style.height = (h - hd.parentNode.offsetHeight) + 'px';
\r
334 updateIndexes : function(){
\r
335 Ext.ListView.superclass.updateIndexes.apply(this, arguments);
\r
336 this.verifyInternalSize();
\r
339 findHeaderIndex : function(hd){
\r
341 var pn = hd.parentNode, cs = pn.parentNode.childNodes;
\r
342 for(var i = 0, c; c = cs[i]; i++){
\r
350 setHdWidths : function(){
\r
351 var els = this.innerHd.dom.getElementsByTagName('div');
\r
352 for(var i = 0, cs = this.columns, len = cs.length; i < len; i++){
\r
353 els[i].style.width = cs[i].width + '%';
\r
358 Ext.reg('listview', Ext.ListView);</pre>
\r