3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
8 * @class Ext.ListView
\r
9 * @extends Ext.DataView
\r
10 * <p>Ext.ListView is a fast and light-weight implentation of a
\r
11 * {@link Ext.grid.GridPanel Grid} like view with the following characteristics:</p>
\r
12 * <div class="mdetail-params"><ul>
\r
13 * <li>resizable columns</li>
\r
14 * <li>selectable</li>
\r
15 * <li>column widths are initially proportioned by percentage based on the container
\r
16 * width and number of columns</li>
\r
17 * <li>uses templates to render the data in any required format</li>
\r
18 * <li>no horizontal scrolling</li>
\r
19 * <li>no editing</li>
\r
21 * <p>Example usage:</p>
\r
23 // consume JSON of this form:
\r
27 "name":"dance_fever.jpg",
\r
29 "lastmod":1236974993000,
\r
30 "url":"images\/thumbs\/dance_fever.jpg"
\r
33 "name":"zack_sink.jpg",
\r
35 "lastmod":1236974993000,
\r
36 "url":"images\/thumbs\/zack_sink.jpg"
\r
40 var store = new Ext.data.JsonStore({
\r
41 url: 'get-images.php',
\r
45 {name:'size', type: 'float'},
\r
46 {name:'lastmod', type:'date', dateFormat:'timestamp'}
\r
51 var listView = new Ext.ListView({
\r
54 emptyText: 'No images to display',
\r
55 reserveScrollOffset: true,
\r
61 header: 'Last Modified',
\r
63 dataIndex: 'lastmod',
\r
64 tpl: '{lastmod:date("m-d h:i a")}'
\r
68 tpl: '{size:fileSize}', // format using Ext.util.Format.fileSize()
\r
73 // put it in a Panel so it looks pretty
\r
74 var panel = new Ext.Panel({
\r
80 title:'Simple ListView <i>(0 items selected)</i>',
\r
83 panel.render(document.body);
\r
85 // little bit of feedback
\r
86 listView.on('selectionchange', function(view, nodes){
\r
87 var l = nodes.length;
\r
88 var s = l != 1 ? 's' : '';
\r
89 panel.setTitle('Simple ListView <i>('+l+' item'+s+' selected)</i>');
\r
93 * @param {Object} config
\r
96 Ext.ListView = Ext.extend(Ext.DataView, {
\r
98 * Set this property to <tt>true</tt> to disable the header click handler disabling sort
\r
99 * (defaults to <tt>false</tt>).
\r
101 * @property disableHeaders
\r
104 * @cfg {Boolean} hideHeaders
\r
105 * <tt>true</tt> to hide the {@link #internalTpl header row} (defaults to <tt>false</tt> so
\r
106 * the {@link #internalTpl header row} will be shown).
\r
109 * @cfg {String} itemSelector
\r
110 * Defaults to <tt>'dl'</tt> to work with the preconfigured <b><tt>{@link Ext.DataView#tpl tpl}</tt></b>.
\r
111 * This setting specifies the CSS selector (e.g. <tt>div.some-class</tt> or <tt>span:first-child</tt>)
\r
112 * that will be used to determine what nodes the ListView will be working with.
\r
114 itemSelector: 'dl',
\r
116 * @cfg {String} selectedClass The CSS class applied to a selected row (defaults to
\r
117 * <tt>'x-list-selected'</tt>). An example overriding the default styling:
\r
119 .x-list-selected {background-color: yellow;}
\r
123 selectedClass:'x-list-selected',
\r
125 * @cfg {String} overClass The CSS class applied when over a row (defaults to
\r
126 * <tt>'x-list-over'</tt>). An example overriding the default styling:
\r
128 .x-list-over {background-color: orange;}
\r
132 overClass:'x-list-over',
\r
134 * @cfg {Boolean} reserveScrollOffset
\r
135 * By default will defer accounting for the configured <b><tt>{@link #scrollOffset}</tt></b>
\r
136 * for 10 milliseconds. Specify <tt>true</tt> to account for the configured
\r
137 * <b><tt>{@link #scrollOffset}</tt></b> immediately.
\r
140 * @cfg {Number} scrollOffset The amount of space to reserve for the scrollbar (defaults to
\r
141 * <tt>undefined</tt>). If an explicit value isn't specified, this will be automatically
\r
144 scrollOffset : undefined,
\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
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
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
188 * @cfg {String/Array} internalTpl
\r
189 * The template to be used for the header row. See {@link #tpl} for more details.
\r
193 * IE has issues when setting percentage based widths to 100%. Default to 99.
\r
195 maxWidth: Ext.isIE ? 99 : 100,
\r
197 initComponent : function(){
\r
198 if(this.columnResize){
\r
199 this.colResizer = new Ext.ListView.ColumnResizer(this.colResizer);
\r
200 this.colResizer.init(this);
\r
202 if(this.columnSort){
\r
203 this.colSorter = new Ext.ListView.Sorter(this.columnSort);
\r
204 this.colSorter.init(this);
\r
206 if(!this.internalTpl){
\r
207 this.internalTpl = new Ext.XTemplate(
\r
208 '<div class="x-list-header"><div class="x-list-header-inner">',
\r
209 '<tpl for="columns">',
\r
210 '<div style="width:{width}%;text-align:{align};"><em unselectable="on" id="',this.id, '-xlhd-{#}">',
\r
214 '<div class="x-clear"></div>',
\r
216 '<div class="x-list-body"><div class="x-list-body-inner">',
\r
221 this.tpl = new Ext.XTemplate(
\r
222 '<tpl for="rows">',
\r
224 '<tpl for="parent.columns">',
\r
225 '<dt style="width:{width}%;text-align:{align};"><em unselectable="on">',
\r
226 '{[values.tpl.apply(parent)]}',
\r
229 '<div class="x-clear"></div>',
\r
234 var cs = this.columns,
\r
235 allocatedWidth = 0,
\r
236 colsWithWidth = 0,
\r
239 for(var i = 0; i < len; i++){
\r
240 var c = Ext.apply({}, cs[i]);
\r
242 c.tpl = new Ext.XTemplate('{' + c.dataIndex + '}');
\r
243 }else if(Ext.isString(c.tpl)){
\r
244 c.tpl = new Ext.XTemplate(c.tpl);
\r
246 c.align = c.align || 'left';
\r
247 if(Ext.isNumber(c.width)){
\r
249 allocatedWidth += c.width;
\r
254 cs = this.columns = columns;
\r
255 // auto calculate missing column widths
\r
256 if(colsWithWidth < len){
\r
257 var remaining = len - colsWithWidth;
\r
258 if(allocatedWidth < this.maxWidth){
\r
259 var perCol = ((this.maxWidth-allocatedWidth) / remaining);
\r
260 for(var j = 0; j < len; j++){
\r
262 if(!Ext.isNumber(c.width)){
\r
268 Ext.ListView.superclass.initComponent.call(this);
\r
271 onRender : function(){
\r
273 cls: 'x-list-wrap'
\r
275 Ext.ListView.superclass.onRender.apply(this, arguments);
\r
277 this.internalTpl.overwrite(this.el, {columns: this.columns});
\r
279 this.innerBody = Ext.get(this.el.dom.childNodes[1].firstChild);
\r
280 this.innerHd = Ext.get(this.el.dom.firstChild.firstChild);
\r
282 if(this.hideHeaders){
\r
283 this.el.dom.firstChild.style.display = 'none';
\r
287 getTemplateTarget : function(){
\r
288 return this.innerBody;
\r
292 * <p>Function which can be overridden which returns the data object passed to this
\r
293 * view's {@link #tpl template} to render the whole ListView. The returned object
\r
294 * shall contain the following properties:</p>
\r
295 * <div class="mdetail-params"><ul>
\r
296 * <li><b>columns</b> : String<div class="sub-desc">See <tt>{@link #columns}</tt></div></li>
\r
297 * <li><b>rows</b> : String<div class="sub-desc">See
\r
298 * <tt>{@link Ext.DataView}.{@link Ext.DataView#collectData collectData}</div></li>
\r
300 * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
\r
301 * @param {Number} startIndex the index number of the Record being prepared for rendering.
\r
302 * @return {Object} A data object containing properties to be processed by a repeating
\r
303 * XTemplate as described above.
\r
305 collectData : function(){
\r
306 var rs = Ext.ListView.superclass.collectData.apply(this, arguments);
\r
308 columns: this.columns,
\r
313 verifyInternalSize : function(){
\r
315 this.onResize(this.lastSize.width, this.lastSize.height);
\r
320 onResize : function(w, h){
\r
321 var bd = this.innerBody.dom;
\r
322 var hd = this.innerHd.dom
\r
326 var bdp = bd.parentNode;
\r
327 if(Ext.isNumber(w)){
\r
328 var sw = w - Ext.num(this.scrollOffset, Ext.getScrollBarWidth());
\r
329 if(this.reserveScrollOffset || ((bdp.offsetWidth - bdp.clientWidth) > 10)){
\r
330 bd.style.width = sw + 'px';
\r
331 hd.style.width = sw + 'px';
\r
333 bd.style.width = w + 'px';
\r
334 hd.style.width = w + 'px';
\r
335 setTimeout(function(){
\r
336 if((bdp.offsetWidth - bdp.clientWidth) > 10){
\r
337 bd.style.width = sw + 'px';
\r
338 hd.style.width = sw + 'px';
\r
343 if(Ext.isNumber(h)){
\r
344 bdp.style.height = (h - hd.parentNode.offsetHeight) + 'px';
\r
348 updateIndexes : function(){
\r
349 Ext.ListView.superclass.updateIndexes.apply(this, arguments);
\r
350 this.verifyInternalSize();
\r
353 findHeaderIndex : function(hd){
\r
355 var pn = hd.parentNode, cs = pn.parentNode.childNodes;
\r
356 for(var i = 0, c; c = cs[i]; i++){
\r
364 setHdWidths : function(){
\r
365 var els = this.innerHd.dom.getElementsByTagName('div');
\r
366 for(var i = 0, cs = this.columns, len = cs.length; i < len; i++){
\r
367 els[i].style.width = cs[i].width + '%';
\r
372 Ext.reg('listview', Ext.ListView);