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">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.ListView"></div>/**
\r
15 * @class Ext.ListView
\r
16 * @extends Ext.DataView
\r
17 * <p>Ext.ListView is a fast and light-weight implentation of a
\r
18 * {@link Ext.grid.GridPanel Grid} like view with the following characteristics:</p>
\r
19 * <div class="mdetail-params"><ul>
\r
20 * <li>resizable columns</li>
\r
21 * <li>selectable</li>
\r
22 * <li>column widths are initially proportioned by percentage based on the container
\r
23 * width and number of columns</li>
\r
24 * <li>uses templates to render the data in any required format</li>
\r
25 * <li>no horizontal scrolling</li>
\r
26 * <li>no editing</li>
\r
28 * <p>Example usage:</p>
\r
30 // consume JSON of this form:
\r
34 "name":"dance_fever.jpg",
\r
36 "lastmod":1236974993000,
\r
37 "url":"images\/thumbs\/dance_fever.jpg"
\r
40 "name":"zack_sink.jpg",
\r
42 "lastmod":1236974993000,
\r
43 "url":"images\/thumbs\/zack_sink.jpg"
\r
47 var store = new Ext.data.JsonStore({
\r
48 url: 'get-images.php',
\r
52 {name:'size', type: 'float'},
\r
53 {name:'lastmod', type:'date', dateFormat:'timestamp'}
\r
58 var listView = new Ext.ListView({
\r
61 emptyText: 'No images to display',
\r
62 reserveScrollOffset: true,
\r
68 header: 'Last Modified',
\r
70 dataIndex: 'lastmod',
\r
71 tpl: '{lastmod:date("m-d h:i a")}'
\r
75 tpl: '{size:fileSize}', // format using Ext.util.Format.fileSize()
\r
80 // put it in a Panel so it looks pretty
\r
81 var panel = new Ext.Panel({
\r
87 title:'Simple ListView <i>(0 items selected)</i>',
\r
90 panel.render(document.body);
\r
92 // little bit of feedback
\r
93 listView.on('selectionchange', function(view, nodes){
\r
94 var l = nodes.length;
\r
95 var s = l != 1 ? 's' : '';
\r
96 panel.setTitle('Simple ListView <i>('+l+' item'+s+' selected)</i>');
\r
100 * @param {Object} config
\r
103 Ext.ListView = Ext.extend(Ext.DataView, {
\r
104 <div id="prop-Ext.ListView-disableHeaders"></div>/**
\r
105 * Set this property to <tt>true</tt> to disable the header click handler disabling sort
\r
106 * (defaults to <tt>false</tt>).
\r
108 * @property disableHeaders
\r
110 <div id="cfg-Ext.ListView-hideHeaders"></div>/**
\r
111 * @cfg {Boolean} hideHeaders
\r
112 * <tt>true</tt> to hide the {@link #internalTpl header row} (defaults to <tt>false</tt> so
\r
113 * the {@link #internalTpl header row} will be shown).
\r
115 <div id="cfg-Ext.ListView-itemSelector"></div>/**
\r
116 * @cfg {String} itemSelector
\r
117 * Defaults to <tt>'dl'</tt> to work with the preconfigured <b><tt>{@link Ext.DataView#tpl tpl}</tt></b>.
\r
118 * This setting specifies the CSS selector (e.g. <tt>div.some-class</tt> or <tt>span:first-child</tt>)
\r
119 * that will be used to determine what nodes the ListView will be working with.
\r
121 itemSelector: 'dl',
\r
122 <div id="cfg-Ext.ListView-selectedClass"></div>/**
\r
123 * @cfg {String} selectedClass The CSS class applied to a selected row (defaults to
\r
124 * <tt>'x-list-selected'</tt>). An example overriding the default styling:
\r
126 .x-list-selected {background-color: yellow;}
\r
130 selectedClass:'x-list-selected',
\r
131 <div id="cfg-Ext.ListView-overClass"></div>/**
\r
132 * @cfg {String} overClass The CSS class applied when over a row (defaults to
\r
133 * <tt>'x-list-over'</tt>). An example overriding the default styling:
\r
135 .x-list-over {background-color: orange;}
\r
139 overClass:'x-list-over',
\r
140 <div id="cfg-Ext.ListView-reserveScrollOffset"></div>/**
\r
141 * @cfg {Boolean} reserveScrollOffset
\r
142 * By default will defer accounting for the configured <b><tt>{@link #scrollOffset}</tt></b>
\r
143 * for 10 milliseconds. Specify <tt>true</tt> to account for the configured
\r
144 * <b><tt>{@link #scrollOffset}</tt></b> immediately.
\r
146 <div id="cfg-Ext.ListView-scrollOffset"></div>/**
\r
147 * @cfg {Number} scrollOffset The amount of space to reserve for the scrollbar (defaults to
\r
148 * <tt>undefined</tt>). If an explicit value isn't specified, this will be automatically
\r
151 scrollOffset : undefined,
\r
152 <div id="cfg-Ext.ListView-columnResize"></div>/**
\r
153 * @cfg {Boolean/Object} columnResize
\r
154 * Specify <tt>true</tt> or specify a configuration object for {@link Ext.ListView.ColumnResizer}
\r
155 * to enable the columns to be resizable (defaults to <tt>true</tt>).
\r
157 columnResize: true,
\r
158 <div id="cfg-Ext.ListView-columns"></div>/**
\r
159 * @cfg {Array} columns An array of column configuration objects, for example:
\r
165 tpl: '{size:fileSize}',
\r
169 * Acceptable properties for each column configuration object are:
\r
170 * <div class="mdetail-params"><ul>
\r
171 * <li><b><tt>align</tt></b> : String<div class="sub-desc">Set the CSS text-align property
\r
172 * of the column. Defaults to <tt>'left'</tt>.</div></li>
\r
173 * <li><b><tt>dataIndex</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
\r
174 * {@link Ext.grid.Column#dataIndex dataIndex} for details.</div></li>
\r
175 * <li><b><tt>header</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
\r
176 * {@link Ext.grid.Column#header header} for details.</div></li>
\r
177 * <li><b><tt>tpl</tt></b> : String<div class="sub-desc">Specify a string to pass as the
\r
178 * configuration string for {@link Ext.XTemplate}. By default an {@link Ext.XTemplate}
\r
179 * will be implicitly created using the <tt>dataIndex</tt>.</div></li>
\r
180 * <li><b><tt>width</tt></b> : Number<div class="sub-desc">Percentage of the container width
\r
181 * this column should be allocated. Columns that have no width specified will be
\r
182 * allocated with an equal percentage to fill 100% of the container width. To easily take
\r
183 * advantage of the full container width, leave the width of at least one column undefined.
\r
184 * Note that if you do not want to take up the full width of the container, the width of
\r
185 * every column needs to be explicitly defined.</div></li>
\r
188 <div id="cfg-Ext.ListView-columnSort"></div>/**
\r
189 * @cfg {Boolean/Object} columnSort
\r
190 * Specify <tt>true</tt> or specify a configuration object for {@link Ext.ListView.Sorter}
\r
191 * to enable the columns to be sortable (defaults to <tt>true</tt>).
\r
194 <div id="cfg-Ext.ListView-internalTpl"></div>/**
\r
195 * @cfg {String/Array} internalTpl
\r
196 * The template to be used for the header row. See {@link #tpl} for more details.
\r
200 * IE has issues when setting percentage based widths to 100%. Default to 99.
\r
202 maxWidth: Ext.isIE ? 99 : 100,
\r
204 initComponent : function(){
\r
205 if(this.columnResize){
\r
206 this.colResizer = new Ext.ListView.ColumnResizer(this.colResizer);
\r
207 this.colResizer.init(this);
\r
209 if(this.columnSort){
\r
210 this.colSorter = new Ext.ListView.Sorter(this.columnSort);
\r
211 this.colSorter.init(this);
\r
213 if(!this.internalTpl){
\r
214 this.internalTpl = new Ext.XTemplate(
\r
215 '<div class="x-list-header"><div class="x-list-header-inner">',
\r
216 '<tpl for="columns">',
\r
217 '<div style="width:{width}%;text-align:{align};"><em unselectable="on" id="',this.id, '-xlhd-{#}">',
\r
221 '<div class="x-clear"></div>',
\r
223 '<div class="x-list-body"><div class="x-list-body-inner">',
\r
228 this.tpl = new Ext.XTemplate(
\r
229 '<tpl for="rows">',
\r
231 '<tpl for="parent.columns">',
\r
232 '<dt style="width:{width}%;text-align:{align};"><em unselectable="on">',
\r
233 '{[values.tpl.apply(parent)]}',
\r
236 '<div class="x-clear"></div>',
\r
241 var cs = this.columns,
\r
242 allocatedWidth = 0,
\r
243 colsWithWidth = 0,
\r
246 for(var i = 0; i < len; i++){
\r
247 var c = Ext.apply({}, cs[i]);
\r
249 c.tpl = new Ext.XTemplate('{' + c.dataIndex + '}');
\r
250 }else if(Ext.isString(c.tpl)){
\r
251 c.tpl = new Ext.XTemplate(c.tpl);
\r
253 c.align = c.align || 'left';
\r
254 if(Ext.isNumber(c.width)){
\r
256 allocatedWidth += c.width;
\r
261 cs = this.columns = columns;
\r
262 // auto calculate missing column widths
\r
263 if(colsWithWidth < len){
\r
264 var remaining = len - colsWithWidth;
\r
265 if(allocatedWidth < this.maxWidth){
\r
266 var perCol = ((this.maxWidth-allocatedWidth) / remaining);
\r
267 for(var j = 0; j < len; j++){
\r
269 if(!Ext.isNumber(c.width)){
\r
275 Ext.ListView.superclass.initComponent.call(this);
\r
278 onRender : function(){
\r
280 cls: 'x-list-wrap'
\r
282 Ext.ListView.superclass.onRender.apply(this, arguments);
\r
284 this.internalTpl.overwrite(this.el, {columns: this.columns});
\r
286 this.innerBody = Ext.get(this.el.dom.childNodes[1].firstChild);
\r
287 this.innerHd = Ext.get(this.el.dom.firstChild.firstChild);
\r
289 if(this.hideHeaders){
\r
290 this.el.dom.firstChild.style.display = 'none';
\r
294 getTemplateTarget : function(){
\r
295 return this.innerBody;
\r
298 <div id="method-Ext.ListView-collectData"></div>/**
\r
299 * <p>Function which can be overridden which returns the data object passed to this
\r
300 * view's {@link #tpl template} to render the whole ListView. The returned object
\r
301 * shall contain the following properties:</p>
\r
302 * <div class="mdetail-params"><ul>
\r
303 * <li><b>columns</b> : String<div class="sub-desc">See <tt>{@link #columns}</tt></div></li>
\r
304 * <li><b>rows</b> : String<div class="sub-desc">See
\r
305 * <tt>{@link Ext.DataView}.{@link Ext.DataView#collectData collectData}</div></li>
\r
307 * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
\r
308 * @param {Number} startIndex the index number of the Record being prepared for rendering.
\r
309 * @return {Object} A data object containing properties to be processed by a repeating
\r
310 * XTemplate as described above.
\r
312 collectData : function(){
\r
313 var rs = Ext.ListView.superclass.collectData.apply(this, arguments);
\r
315 columns: this.columns,
\r
320 verifyInternalSize : function(){
\r
322 this.onResize(this.lastSize.width, this.lastSize.height);
\r
327 onResize : function(w, h){
\r
328 var bd = this.innerBody.dom;
\r
329 var hd = this.innerHd.dom
\r
333 var bdp = bd.parentNode;
\r
334 if(Ext.isNumber(w)){
\r
335 var sw = w - Ext.num(this.scrollOffset, Ext.getScrollBarWidth());
\r
336 if(this.reserveScrollOffset || ((bdp.offsetWidth - bdp.clientWidth) > 10)){
\r
337 bd.style.width = sw + 'px';
\r
338 hd.style.width = sw + 'px';
\r
340 bd.style.width = w + 'px';
\r
341 hd.style.width = w + 'px';
\r
342 setTimeout(function(){
\r
343 if((bdp.offsetWidth - bdp.clientWidth) > 10){
\r
344 bd.style.width = sw + 'px';
\r
345 hd.style.width = sw + 'px';
\r
350 if(Ext.isNumber(h)){
\r
351 bdp.style.height = (h - hd.parentNode.offsetHeight) + 'px';
\r
355 updateIndexes : function(){
\r
356 Ext.ListView.superclass.updateIndexes.apply(this, arguments);
\r
357 this.verifyInternalSize();
\r
360 findHeaderIndex : function(hd){
\r
362 var pn = hd.parentNode, cs = pn.parentNode.childNodes;
\r
363 for(var i = 0, c; c = cs[i]; i++){
\r
371 setHdWidths : function(){
\r
372 var els = this.innerHd.dom.getElementsByTagName('div');
\r
373 for(var i = 0, cs = this.columns, len = cs.length; i < len; i++){
\r
374 els[i].style.width = cs[i].width + '%';
\r
379 Ext.reg('listview', Ext.ListView);</pre>