PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/source/ListView.html

https://github.com/paparent/extjs
HTML | 381 lines | 364 code | 17 blank | 0 comment | 0 complexity | bfc3151ed3999788b761c8443e065ce6 MD5 | raw file
  1. <html>
  2. <head>
  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>
  6. </head>
  7. <body onload="prettyPrint();">
  8. <pre class="prettyprint lang-js">/*!
  9. * Ext JS Library 3.0.3
  10. * Copyright(c) 2006-2009 Ext JS, LLC
  11. * licensing@extjs.com
  12. * http://www.extjs.com/license
  13. */
  14. <div id="cls-Ext.ListView"></div>/**
  15. * @class Ext.ListView
  16. * @extends Ext.DataView
  17. * <p>Ext.ListView is a fast and light-weight implentation of a
  18. * {@link Ext.grid.GridPanel Grid} like view with the following characteristics:</p>
  19. * <div class="mdetail-params"><ul>
  20. * <li>resizable columns</li>
  21. * <li>selectable</li>
  22. * <li>column widths are initially proportioned by percentage based on the container
  23. * width and number of columns</li>
  24. * <li>uses templates to render the data in any required format</li>
  25. * <li>no horizontal scrolling</li>
  26. * <li>no editing</li>
  27. * </ul></div>
  28. * <p>Example usage:</p>
  29. * <pre><code>
  30. // consume JSON of this form:
  31. {
  32. "images":[
  33. {
  34. "name":"dance_fever.jpg",
  35. "size":2067,
  36. "lastmod":1236974993000,
  37. "url":"images\/thumbs\/dance_fever.jpg"
  38. },
  39. {
  40. "name":"zack_sink.jpg",
  41. "size":2303,
  42. "lastmod":1236974993000,
  43. "url":"images\/thumbs\/zack_sink.jpg"
  44. }
  45. ]
  46. }
  47. var store = new Ext.data.JsonStore({
  48. url: 'get-images.php',
  49. root: 'images',
  50. fields: [
  51. 'name', 'url',
  52. {name:'size', type: 'float'},
  53. {name:'lastmod', type:'date', dateFormat:'timestamp'}
  54. ]
  55. });
  56. store.load();
  57. var listView = new Ext.ListView({
  58. store: store,
  59. multiSelect: true,
  60. emptyText: 'No images to display',
  61. reserveScrollOffset: true,
  62. columns: [{
  63. header: 'File',
  64. width: .5,
  65. dataIndex: 'name'
  66. },{
  67. header: 'Last Modified',
  68. width: .35,
  69. dataIndex: 'lastmod',
  70. tpl: '{lastmod:date("m-d h:i a")}'
  71. },{
  72. header: 'Size',
  73. dataIndex: 'size',
  74. tpl: '{size:fileSize}', // format using Ext.util.Format.fileSize()
  75. align: 'right'
  76. }]
  77. });
  78. // put it in a Panel so it looks pretty
  79. var panel = new Ext.Panel({
  80. id:'images-view',
  81. width:425,
  82. height:250,
  83. collapsible:true,
  84. layout:'fit',
  85. title:'Simple ListView <i>(0 items selected)</i>',
  86. items: listView
  87. });
  88. panel.render(document.body);
  89. // little bit of feedback
  90. listView.on('selectionchange', function(view, nodes){
  91. var l = nodes.length;
  92. var s = l != 1 ? 's' : '';
  93. panel.setTitle('Simple ListView <i>('+l+' item'+s+' selected)</i>');
  94. });
  95. * </code></pre>
  96. * @constructor
  97. * @param {Object} config
  98. * @xtype listview
  99. */
  100. Ext.ListView = Ext.extend(Ext.DataView, {
  101. <div id="prop-Ext.ListView-disableHeaders"></div>/**
  102. * Set this property to <tt>true</tt> to disable the header click handler disabling sort
  103. * (defaults to <tt>false</tt>).
  104. * @type Boolean
  105. * @property disableHeaders
  106. */
  107. <div id="cfg-Ext.ListView-hideHeaders"></div>/**
  108. * @cfg {Boolean} hideHeaders
  109. * <tt>true</tt> to hide the {@link #internalTpl header row} (defaults to <tt>false</tt> so
  110. * the {@link #internalTpl header row} will be shown).
  111. */
  112. <div id="cfg-Ext.ListView-itemSelector"></div>/**
  113. * @cfg {String} itemSelector
  114. * Defaults to <tt>'dl'</tt> to work with the preconfigured <b><tt>{@link Ext.DataView#tpl tpl}</tt></b>.
  115. * This setting specifies the CSS selector (e.g. <tt>div.some-class</tt> or <tt>span:first-child</tt>)
  116. * that will be used to determine what nodes the ListView will be working with.
  117. */
  118. itemSelector: 'dl',
  119. <div id="cfg-Ext.ListView-selectedClass"></div>/**
  120. * @cfg {String} selectedClass The CSS class applied to a selected row (defaults to
  121. * <tt>'x-list-selected'</tt>). An example overriding the default styling:
  122. <pre><code>
  123. .x-list-selected {background-color: yellow;}
  124. </code></pre>
  125. * @type String
  126. */
  127. selectedClass:'x-list-selected',
  128. <div id="cfg-Ext.ListView-overClass"></div>/**
  129. * @cfg {String} overClass The CSS class applied when over a row (defaults to
  130. * <tt>'x-list-over'</tt>). An example overriding the default styling:
  131. <pre><code>
  132. .x-list-over {background-color: orange;}
  133. </code></pre>
  134. * @type String
  135. */
  136. overClass:'x-list-over',
  137. <div id="cfg-Ext.ListView-reserveScrollOffset"></div>/**
  138. * @cfg {Boolean} reserveScrollOffset
  139. * By default will defer accounting for the configured <b><tt>{@link #scrollOffset}</tt></b>
  140. * for 10 milliseconds. Specify <tt>true</tt> to account for the configured
  141. * <b><tt>{@link #scrollOffset}</tt></b> immediately.
  142. */
  143. <div id="cfg-Ext.ListView-scrollOffset"></div>/**
  144. * @cfg {Number} scrollOffset The amount of space to reserve for the scrollbar (defaults to
  145. * <tt>undefined</tt>). If an explicit value isn't specified, this will be automatically
  146. * calculated.
  147. */
  148. scrollOffset : undefined,
  149. <div id="cfg-Ext.ListView-columnResize"></div>/**
  150. * @cfg {Boolean/Object} columnResize
  151. * Specify <tt>true</tt> or specify a configuration object for {@link Ext.ListView.ColumnResizer}
  152. * to enable the columns to be resizable (defaults to <tt>true</tt>).
  153. */
  154. columnResize: true,
  155. <div id="cfg-Ext.ListView-columns"></div>/**
  156. * @cfg {Array} columns An array of column configuration objects, for example:
  157. * <pre><code>
  158. {
  159. align: 'right',
  160. dataIndex: 'size',
  161. header: 'Size',
  162. tpl: '{size:fileSize}',
  163. width: .35
  164. }
  165. * </code></pre>
  166. * Acceptable properties for each column configuration object are:
  167. * <div class="mdetail-params"><ul>
  168. * <li><b><tt>align</tt></b> : String<div class="sub-desc">Set the CSS text-align property
  169. * of the column. Defaults to <tt>'left'</tt>.</div></li>
  170. * <li><b><tt>dataIndex</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
  171. * {@link Ext.grid.Column#dataIndex dataIndex} for details.</div></li>
  172. * <li><b><tt>header</tt></b> : String<div class="sub-desc">See {@link Ext.grid.Column}.
  173. * {@link Ext.grid.Column#header header} for details.</div></li>
  174. * <li><b><tt>tpl</tt></b> : String<div class="sub-desc">Specify a string to pass as the
  175. * configuration string for {@link Ext.XTemplate}. By default an {@link Ext.XTemplate}
  176. * will be implicitly created using the <tt>dataIndex</tt>.</div></li>
  177. * <li><b><tt>width</tt></b> : Number<div class="sub-desc">Percentage of the container width
  178. * this column should be allocated. Columns that have no width specified will be
  179. * allocated with an equal percentage to fill 100% of the container width. To easily take
  180. * advantage of the full container width, leave the width of at least one column undefined.
  181. * Note that if you do not want to take up the full width of the container, the width of
  182. * every column needs to be explicitly defined.</div></li>
  183. * </ul></div>
  184. */
  185. <div id="cfg-Ext.ListView-columnSort"></div>/**
  186. * @cfg {Boolean/Object} columnSort
  187. * Specify <tt>true</tt> or specify a configuration object for {@link Ext.ListView.Sorter}
  188. * to enable the columns to be sortable (defaults to <tt>true</tt>).
  189. */
  190. columnSort: true,
  191. <div id="cfg-Ext.ListView-internalTpl"></div>/**
  192. * @cfg {String/Array} internalTpl
  193. * The template to be used for the header row. See {@link #tpl} for more details.
  194. */
  195. /*
  196. * IE has issues when setting percentage based widths to 100%. Default to 99.
  197. */
  198. maxWidth: Ext.isIE ? 99 : 100,
  199. initComponent : function(){
  200. if(this.columnResize){
  201. this.colResizer = new Ext.ListView.ColumnResizer(this.colResizer);
  202. this.colResizer.init(this);
  203. }
  204. if(this.columnSort){
  205. this.colSorter = new Ext.ListView.Sorter(this.columnSort);
  206. this.colSorter.init(this);
  207. }
  208. if(!this.internalTpl){
  209. this.internalTpl = new Ext.XTemplate(
  210. '<div class="x-list-header"><div class="x-list-header-inner">',
  211. '<tpl for="columns">',
  212. '<div style="width:{width}%;text-align:{align};"><em unselectable="on" id="',this.id, '-xlhd-{#}">',
  213. '{header}',
  214. '</em></div>',
  215. '</tpl>',
  216. '<div class="x-clear"></div>',
  217. '</div></div>',
  218. '<div class="x-list-body"><div class="x-list-body-inner">',
  219. '</div></div>'
  220. );
  221. }
  222. if(!this.tpl){
  223. this.tpl = new Ext.XTemplate(
  224. '<tpl for="rows">',
  225. '<dl>',
  226. '<tpl for="parent.columns">',
  227. '<dt style="width:{width}%;text-align:{align};"><em unselectable="on">',
  228. '{[values.tpl.apply(parent)]}',
  229. '</em></dt>',
  230. '</tpl>',
  231. '<div class="x-clear"></div>',
  232. '</dl>',
  233. '</tpl>'
  234. );
  235. };
  236. var cs = this.columns,
  237. allocatedWidth = 0,
  238. colsWithWidth = 0,
  239. len = cs.length,
  240. columns = [];
  241. for(var i = 0; i < len; i++){
  242. var c = Ext.apply({}, cs[i]);
  243. if(!c.tpl){
  244. c.tpl = new Ext.XTemplate('{' + c.dataIndex + '}');
  245. }else if(Ext.isString(c.tpl)){
  246. c.tpl = new Ext.XTemplate(c.tpl);
  247. }
  248. c.align = c.align || 'left';
  249. if(Ext.isNumber(c.width)){
  250. c.width *= 100;
  251. allocatedWidth += c.width;
  252. colsWithWidth++;
  253. }
  254. columns.push(c);
  255. }
  256. cs = this.columns = columns;
  257. // auto calculate missing column widths
  258. if(colsWithWidth < len){
  259. var remaining = len - colsWithWidth;
  260. if(allocatedWidth < this.maxWidth){
  261. var perCol = ((this.maxWidth-allocatedWidth) / remaining);
  262. for(var j = 0; j < len; j++){
  263. var c = cs[j];
  264. if(!Ext.isNumber(c.width)){
  265. c.width = perCol;
  266. }
  267. }
  268. }
  269. }
  270. Ext.ListView.superclass.initComponent.call(this);
  271. },
  272. onRender : function(){
  273. this.autoEl = {
  274. cls: 'x-list-wrap'
  275. };
  276. Ext.ListView.superclass.onRender.apply(this, arguments);
  277. this.internalTpl.overwrite(this.el, {columns: this.columns});
  278. this.innerBody = Ext.get(this.el.dom.childNodes[1].firstChild);
  279. this.innerHd = Ext.get(this.el.dom.firstChild.firstChild);
  280. if(this.hideHeaders){
  281. this.el.dom.firstChild.style.display = 'none';
  282. }
  283. },
  284. getTemplateTarget : function(){
  285. return this.innerBody;
  286. },
  287. <div id="method-Ext.ListView-collectData"></div>/**
  288. * <p>Function which can be overridden which returns the data object passed to this
  289. * view's {@link #tpl template} to render the whole ListView. The returned object
  290. * shall contain the following properties:</p>
  291. * <div class="mdetail-params"><ul>
  292. * <li><b>columns</b> : String<div class="sub-desc">See <tt>{@link #columns}</tt></div></li>
  293. * <li><b>rows</b> : String<div class="sub-desc">See
  294. * <tt>{@link Ext.DataView}.{@link Ext.DataView#collectData collectData}</div></li>
  295. * </ul></div>
  296. * @param {Array} records An Array of {@link Ext.data.Record}s to be rendered into the DataView.
  297. * @param {Number} startIndex the index number of the Record being prepared for rendering.
  298. * @return {Object} A data object containing properties to be processed by a repeating
  299. * XTemplate as described above.
  300. */
  301. collectData : function(){
  302. var rs = Ext.ListView.superclass.collectData.apply(this, arguments);
  303. return {
  304. columns: this.columns,
  305. rows: rs
  306. }
  307. },
  308. verifyInternalSize : function(){
  309. if(this.lastSize){
  310. this.onResize(this.lastSize.width, this.lastSize.height);
  311. }
  312. },
  313. // private
  314. onResize : function(w, h){
  315. var bd = this.innerBody.dom;
  316. var hd = this.innerHd.dom
  317. if(!bd){
  318. return;
  319. }
  320. var bdp = bd.parentNode;
  321. if(Ext.isNumber(w)){
  322. var sw = w - Ext.num(this.scrollOffset, Ext.getScrollBarWidth());
  323. if(this.reserveScrollOffset || ((bdp.offsetWidth - bdp.clientWidth) > 10)){
  324. bd.style.width = sw + 'px';
  325. hd.style.width = sw + 'px';
  326. }else{
  327. bd.style.width = w + 'px';
  328. hd.style.width = w + 'px';
  329. setTimeout(function(){
  330. if((bdp.offsetWidth - bdp.clientWidth) > 10){
  331. bd.style.width = sw + 'px';
  332. hd.style.width = sw + 'px';
  333. }
  334. }, 10);
  335. }
  336. }
  337. if(Ext.isNumber(h)){
  338. bdp.style.height = (h - hd.parentNode.offsetHeight) + 'px';
  339. }
  340. },
  341. updateIndexes : function(){
  342. Ext.ListView.superclass.updateIndexes.apply(this, arguments);
  343. this.verifyInternalSize();
  344. },
  345. findHeaderIndex : function(hd){
  346. hd = hd.dom || hd;
  347. var pn = hd.parentNode, cs = pn.parentNode.childNodes;
  348. for(var i = 0, c; c = cs[i]; i++){
  349. if(c == pn){
  350. return i;
  351. }
  352. }
  353. return -1;
  354. },
  355. setHdWidths : function(){
  356. var els = this.innerHd.dom.getElementsByTagName('div');
  357. for(var i = 0, cs = this.columns, len = cs.length; i < len; i++){
  358. els[i].style.width = cs[i].width + '%';
  359. }
  360. }
  361. });
  362. Ext.reg('listview', Ext.ListView);</pre>
  363. </body>
  364. </html>