3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.list.ListView
9 * @extends Ext.DataView
10 * <p>Ext.list.ListView is a fast and light-weight implentation of a
11 * {@link Ext.grid.GridPanel Grid} like view with the following characteristics:</p>
12 * <div class="mdetail-params"><ul>
13 * <li>resizable columns</li>
15 * <li>column widths are initially proportioned by percentage based on the container
16 * width and number of columns</li>
17 * <li>uses templates to render the data in any required format</li>
18 * <li>no horizontal scrolling</li>
21 * <p>Example usage:</p>
23 // consume JSON of this form:
27 "name":"dance_fever.jpg",
29 "lastmod":1236974993000,
30 "url":"images\/thumbs\/dance_fever.jpg"
33 "name":"zack_sink.jpg",
35 "lastmod":1236974993000,
36 "url":"images\/thumbs\/zack_sink.jpg"
40 var store = new Ext.data.JsonStore({
41 url: 'get-images.php',
45 {name:'size', type: 'float'},
46 {name:'lastmod', type:'date', dateFormat:'timestamp'}
51 var listView = new Ext.list.ListView({
54 emptyText: 'No images to display',
55 reserveScrollOffset: true,
61 header: 'Last Modified',
64 tpl: '{lastmod:date("m-d h:i a")}'
68 tpl: '{size:fileSize}', // format using Ext.util.Format.fileSize()
73 // put it in a Panel so it looks pretty
74 var panel = new Ext.Panel({
80 title:'Simple ListView <i>(0 items selected)</i>',
83 panel.render(document.body);
85 // little bit of feedback
86 listView.on('selectionchange', function(view, nodes){
88 var s = l != 1 ? 's' : '';
89 panel.setTitle('Simple ListView <i>('+l+' item'+s+' selected)</i>');
93 * @param {Object} config
96 Ext.list.ListView = Ext.extend(Ext.DataView, {
98 * Set this property to <tt>true</tt> to disable the header click handler disabling sort
99 * (defaults to <tt>false</tt>).
101 * @property disableHeaders
104 * @cfg {Boolean} hideHeaders
105 * <tt>true</tt> to hide the {@link #internalTpl header row} (defaults to <tt>false</tt> so
106 * the {@link #internalTpl header row} will be shown).
109 * @cfg {String} itemSelector
110 * Defaults to <tt>'dl'</tt> to work with the preconfigured <b><tt>{@link Ext.DataView#tpl tpl}</tt></b>.
111 * This setting specifies the CSS selector (e.g. <tt>div.some-class</tt> or <tt>span:first-child</tt>)
112 * that will be used to determine what nodes the ListView will be working with.
116 * @cfg {String} selectedClass The CSS class applied to a selected row (defaults to
117 * <tt>'x-list-selected'</tt>). An example overriding the default styling:
119 .x-list-selected {background-color: yellow;}
123 selectedClass:'x-list-selected',
125 * @cfg {String} overClass The CSS class applied when over a row (defaults to
126 * <tt>'x-list-over'</tt>). An example overriding the default styling:
128 .x-list-over {background-color: orange;}
132 overClass:'x-list-over',
134 * @cfg {Boolean} reserveScrollOffset
135 * By default will defer accounting for the configured <b><tt>{@link #scrollOffset}</tt></b>
136 * for 10 milliseconds. Specify <tt>true</tt> to account for the configured
137 * <b><tt>{@link #scrollOffset}</tt></b> immediately.
140 * @cfg {Number} scrollOffset The amount of space to reserve for the scrollbar (defaults to
141 * <tt>undefined</tt>). If an explicit value isn't specified, this will be automatically
144 scrollOffset : undefined,
146 * @cfg {Boolean/Object} columnResize
147 * Specify <tt>true</tt> or specify a configuration object for {@link Ext.list.ListView.ColumnResizer}
148 * to enable the columns to be resizable (defaults to <tt>true</tt>).
152 * @cfg {Array} columns An array of column configuration objects, for example:
158 tpl: '{size:fileSize}',
162 * Acceptable properties for each column configuration object are:
163 * <div class="mdetail-params"><ul>
164 * <li><b><tt>align</tt></b> : String<div class="sub-desc">Set the CSS text-align property
165 * of the column. Defaults to <tt>'left'</tt>.</div></li>
166 * <li><b><tt>dataIndex</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
167 * {@link Ext.grid.Column#dataIndex dataIndex} for details.</div></li>
168 * <li><b><tt>header</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
169 * {@link Ext.grid.Column#header header} for details.</div></li>
170 * <li><b><tt>tpl</tt></b> : String<div class="sub-desc">Specify a string to pass as the
171 * configuration string for {@link Ext.XTemplate}. By default an {@link Ext.XTemplate}
172 * will be implicitly created using the <tt>dataIndex</tt>.</div></li>
173 * <li><b><tt>width</tt></b> : Number<div class="sub-desc">Percentage of the container width
174 * this column should be allocated. Columns that have no width specified will be
175 * allocated with an equal percentage to fill 100% of the container width. To easily take
176 * advantage of the full container width, leave the width of at least one column undefined.
177 * Note that if you do not want to take up the full width of the container, the width of
178 * every column needs to be explicitly defined.</div></li>
182 * @cfg {Boolean/Object} columnSort
183 * Specify <tt>true</tt> or specify a configuration object for {@link Ext.list.ListView.Sorter}
184 * to enable the columns to be sortable (defaults to <tt>true</tt>).
188 * @cfg {String/Array} internalTpl
189 * The template to be used for the header row. See {@link #tpl} for more details.
193 * IE has issues when setting percentage based widths to 100%. Default to 99.
195 maxColumnWidth: Ext.isIE ? 99 : 100,
197 initComponent : function(){
198 if(this.columnResize){
199 this.colResizer = new Ext.list.ColumnResizer(this.colResizer);
200 this.colResizer.init(this);
203 this.colSorter = new Ext.list.Sorter(this.columnSort);
204 this.colSorter.init(this);
206 if(!this.internalTpl){
207 this.internalTpl = new Ext.XTemplate(
208 '<div class="x-list-header"><div class="x-list-header-inner">',
209 '<tpl for="columns">',
210 '<div style="width:{[values.width*100]}%;text-align:{align};"><em unselectable="on" id="',this.id, '-xlhd-{#}">',
214 '<div class="x-clear"></div>',
216 '<div class="x-list-body"><div class="x-list-body-inner">',
221 this.tpl = new Ext.XTemplate(
224 '<tpl for="parent.columns">',
225 '<dt style="width:{[values.width*100]}%;text-align:{align};">',
226 '<em unselectable="on"<tpl if="cls"> class="{cls}</tpl>">',
227 '{[values.tpl.apply(parent)]}',
230 '<div class="x-clear"></div>',
236 var cs = this.columns,
242 for(var i = 0; i < len; i++){
245 c.xtype = c.xtype ? (/^lv/.test(c.xtype) ? c.xtype : 'lv' + c.xtype) : 'lvcolumn';
249 allocatedWidth += c.width*100;
250 if(allocatedWidth > this.maxColumnWidth){
251 c.width -= (allocatedWidth - this.maxColumnWidth) / 100;
258 cs = this.columns = columns;
260 // auto calculate missing column widths
261 if(colsWithWidth < len){
262 var remaining = len - colsWithWidth;
263 if(allocatedWidth < this.maxColumnWidth){
264 var perCol = ((this.maxColumnWidth-allocatedWidth) / remaining)/100;
265 for(var j = 0; j < len; j++){
273 Ext.list.ListView.superclass.initComponent.call(this);
276 onRender : function(){
280 Ext.list.ListView.superclass.onRender.apply(this, arguments);
282 this.internalTpl.overwrite(this.el, {columns: this.columns});
284 this.innerBody = Ext.get(this.el.dom.childNodes[1].firstChild);
285 this.innerHd = Ext.get(this.el.dom.firstChild.firstChild);
287 if(this.hideHeaders){
288 this.el.dom.firstChild.style.display = 'none';
292 getTemplateTarget : function(){
293 return this.innerBody;
297 * <p>Function which can be overridden which returns the data object passed to this
298 * view's {@link #tpl template} to render the whole ListView. The returned object
299 * shall contain the following properties:</p>
300 * <div class="mdetail-params"><ul>
301 * <li><b>columns</b> : String<div class="sub-desc">See <tt>{@link #columns}</tt></div></li>
302 * <li><b>rows</b> : String<div class="sub-desc">See
303 * <tt>{@link Ext.DataView}.{@link Ext.DataView#collectData collectData}</div></li>
305 * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
306 * @param {Number} startIndex the index number of the Record being prepared for rendering.
307 * @return {Object} A data object containing properties to be processed by a repeating
308 * XTemplate as described above.
310 collectData : function(){
311 var rs = Ext.list.ListView.superclass.collectData.apply(this, arguments);
313 columns: this.columns,
318 verifyInternalSize : function(){
320 this.onResize(this.lastSize.width, this.lastSize.height);
325 onResize : function(w, h){
326 var body = this.innerBody.dom,
327 header = this.innerHd.dom,
328 scrollWidth = w - Ext.num(this.scrollOffset, Ext.getScrollBarWidth()) + 'px',
334 parentNode = body.parentNode;
336 if(this.reserveScrollOffset || ((parentNode.offsetWidth - parentNode.clientWidth) > 10)){
337 body.style.width = scrollWidth;
338 header.style.width = scrollWidth;
340 body.style.width = w + 'px';
341 header.style.width = w + 'px';
342 setTimeout(function(){
343 if((parentNode.offsetWidth - parentNode.clientWidth) > 10){
344 body.style.width = scrollWidth;
345 header.style.width = scrollWidth;
351 parentNode.style.height = Math.max(0, h - header.parentNode.offsetHeight) + 'px';
355 updateIndexes : function(){
356 Ext.list.ListView.superclass.updateIndexes.apply(this, arguments);
357 this.verifyInternalSize();
360 findHeaderIndex : function(header){
361 header = header.dom || header;
362 var parentNode = header.parentNode,
363 children = parentNode.parentNode.childNodes,
366 for(; c = children[i]; i++){
374 setHdWidths : function(){
375 var els = this.innerHd.dom.getElementsByTagName('div'),
377 columns = this.columns,
378 len = columns.length;
381 els[i].style.width = (columns[i].width*100) + '%';
386 Ext.reg('listview', Ext.list.ListView);
388 // Backwards compatibility alias
389 Ext.ListView = Ext.list.ListView;