4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-grid-Panel'>/**
19 </span> * @author Aaron Conran
20 * @docauthor Ed Spencer
22 * Grids are an excellent way of showing large amounts of tabular data on the client side. Essentially a supercharged
23 * `<table>`, GridPanel makes it easy to fetch, sort and filter large amounts of data.
25 * Grids are composed of two main pieces - a {@link Ext.data.Store Store} full of data and a set of columns to render.
30 * Ext.create('Ext.data.Store', {
31 * storeId:'simpsonsStore',
32 * fields:['name', 'email', 'phone'],
34 * { 'name': 'Lisa', "email":"lisa@simpsons.com", "phone":"555-111-1224" },
35 * { 'name': 'Bart', "email":"bart@simpsons.com", "phone":"555-222-1234" },
36 * { 'name': 'Homer', "email":"home@simpsons.com", "phone":"555-222-1244" },
37 * { 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254" }
48 * Ext.create('Ext.grid.Panel', {
50 * store: Ext.data.StoreManager.lookup('simpsonsStore'),
52 * { header: 'Name', dataIndex: 'name' },
53 * { header: 'Email', dataIndex: 'email', flex: 1 },
54 * { header: 'Phone', dataIndex: 'phone' }
58 * renderTo: Ext.getBody()
61 * The code above produces a simple grid with three columns. We specified a Store which will load JSON data inline.
62 * In most apps we would be placing the grid inside another container and wouldn't need to use the
63 * {@link #height}, {@link #width} and {@link #renderTo} configurations but they are included here to make it easy to get
66 * The grid we created above will contain a header bar with a title ('Simpsons'), a row of column headers directly underneath
67 * and finally the grid rows under the headers.
69 * ## Configuring columns
71 * By default, each column is sortable and will toggle between ASC and DESC sorting when you click on its header. Each
72 * column header is also reorderable by default, and each gains a drop-down menu with options to hide and show columns.
73 * It's easy to configure each column - here we use the same example as above and just modify the columns config:
95 * We turned off sorting and hiding on the 'Name' column so clicking its header now has no effect. We also made the Email
96 * column hidden by default (it can be shown again by using the menu on any other column). We also set the Phone column to
97 * a fixed with of 100px and flexed the Name column, which means it takes up all remaining width after the other columns
98 * have been accounted for. See the {@link Ext.grid.column.Column column docs} for more details.
102 * As well as customizing columns, it's easy to alter the rendering of individual cells using renderers. A renderer is
103 * tied to a particular column and is passed the value that would be rendered into each cell in that column. For example,
104 * we could define a renderer function for the email column to turn each email address into a mailto link:
109 * dataIndex: 'email',
110 * renderer: function(value) {
111 * return Ext.String.format('<a href="mailto:{0}">{1}</a>', value, value);
116 * See the {@link Ext.grid.column.Column column docs} for more information on renderers.
118 * ## Selection Models
120 * Sometimes all you want is to render data onto the screen for viewing, but usually it's necessary to interact with or
121 * update that data. Grids use a concept called a Selection Model, which is simply a mechanism for selecting some part of
122 * the data in the grid. The two main types of Selection Model are RowSelectionModel, where entire rows are selected, and
123 * CellSelectionModel, where individual cells are selected.
125 * Grids use a Row Selection Model by default, but this is easy to customise like so:
127 * Ext.create('Ext.grid.Panel', {
128 * selType: 'cellmodel',
132 * Specifying the `cellmodel` changes a couple of things. Firstly, clicking on a cell now
133 * selects just that cell (using a {@link Ext.selection.RowModel rowmodel} will select the entire row), and secondly the
134 * keyboard navigation will walk from cell to cell instead of row to row. Cell-based selection models are usually used in
135 * conjunction with editing.
139 * Grid has built-in support for in-line editing. There are two chief editing modes - cell editing and row editing. Cell
140 * editing is easy to add to your existing column setup - here we'll just modify the example above to include an editor
141 * on both the name and the email columns:
143 * Ext.create('Ext.grid.Panel', {
145 * store: Ext.data.StoreManager.lookup('simpsonsStore'),
147 * { header: 'Name', dataIndex: 'name', field: 'textfield' },
148 * { header: 'Email', dataIndex: 'email', flex: 1,
150 * xtype: 'textfield',
154 * { header: 'Phone', dataIndex: 'phone' }
156 * selType: 'cellmodel',
158 * Ext.create('Ext.grid.plugin.CellEditing', {
164 * renderTo: Ext.getBody()
167 * This requires a little explanation. We're passing in {@link #store store} and {@link #columns columns} as normal, but
168 * this time we've also specified a {@link Ext.grid.column.Column#field field} on two of our columns. For the Name column
169 * we just want a default textfield to edit the value, so we specify 'textfield'. For the Email column we customized the
170 * editor slightly by passing allowBlank: false, which will provide inline validation.
172 * To support cell editing, we also specified that the grid should use the 'cellmodel' {@link #selType}, and created an
173 * instance of the {@link Ext.grid.plugin.CellEditing CellEditing plugin}, which we configured to activate each editor after a
178 * The other type of editing is row-based editing, using the RowEditor component. This enables you to edit an entire row
179 * at a time, rather than editing cell by cell. Row Editing works in exactly the same way as cell editing, all we need to
180 * do is change the plugin type to {@link Ext.grid.plugin.RowEditing}, and set the selType to 'rowmodel':
182 * Ext.create('Ext.grid.Panel', {
184 * store: Ext.data.StoreManager.lookup('simpsonsStore'),
186 * { header: 'Name', dataIndex: 'name', field: 'textfield' },
187 * { header: 'Email', dataIndex: 'email', flex:1,
189 * xtype: 'textfield',
193 * { header: 'Phone', dataIndex: 'phone' }
195 * selType: 'rowmodel',
197 * Ext.create('Ext.grid.plugin.RowEditing', {
203 * renderTo: Ext.getBody()
206 * Again we passed some configuration to our {@link Ext.grid.plugin.RowEditing} plugin, and now when we click each row a row
207 * editor will appear and enable us to edit each of the columns we have specified an editor for.
209 * ## Sorting & Filtering
211 * Every grid is attached to a {@link Ext.data.Store Store}, which provides multi-sort and filtering capabilities. It's
212 * easy to set up a grid to be sorted from the start:
214 * var myGrid = Ext.create('Ext.grid.Panel', {
216 * fields: ['name', 'email', 'phone'],
217 * sorters: ['name', 'phone']
220 * { text: 'Name', dataIndex: 'name' },
221 * { text: 'Email', dataIndex: 'email' }
225 * Sorting at run time is easily accomplished by simply clicking each column header. If you need to perform sorting on
226 * more than one field at run time it's easy to do so by adding new sorters to the store:
228 * myGrid.store.sort([
229 * { property: 'name', direction: 'ASC' },
230 * { property: 'email', direction: 'DESC' }
233 * See {@link Ext.data.Store} for examples of filtering.
237 * Grid supports the grouping of rows by any field. For example if we had a set of employee records, we might want to
238 * group by the department that each employee works in. Here's how we might set that up:
241 * var store = Ext.create('Ext.data.Store', {
242 * storeId:'employeeStore',
243 * fields:['name', 'senority', 'department'],
244 * groupField: 'department',
245 * data: {'employees':[
246 * { "name": "Michael Scott", "senority": 7, "department": "Manangement" },
247 * { "name": "Dwight Schrute", "senority": 2, "department": "Sales" },
248 * { "name": "Jim Halpert", "senority": 3, "department": "Sales" },
249 * { "name": "Kevin Malone", "senority": 4, "department": "Accounting" },
250 * { "name": "Angela Martin", "senority": 5, "department": "Accounting" }
261 * Ext.create('Ext.grid.Panel', {
262 * title: 'Employees',
263 * store: Ext.data.StoreManager.lookup('employeeStore'),
265 * { header: 'Name', dataIndex: 'name' },
266 * { header: 'Senority', dataIndex: 'senority' }
268 * features: [{ftype:'grouping'}],
271 * renderTo: Ext.getBody()
274 * ## Infinite Scrolling
276 * Grid supports infinite scrolling as an alternative to using a paging toolbar. Your users can scroll through thousands
277 * of records without the performance penalties of renderering all the records on screen at once. The grid should be bound
278 * to a store with a pageSize specified.
280 * var grid = Ext.create('Ext.grid.Panel', {
281 * // Use a PagingGridScroller (this is interchangeable with a PagingToolbar)
282 * verticalScrollerType: 'paginggridscroller',
283 * // do not reset the scrollbar when the view refreshs
284 * invalidateScrollerOnRefresh: false,
285 * // infinite scrolling does not support selection
286 * disableSelection: true,
292 * Grid supports paging through large sets of data via a PagingToolbar or PagingGridScroller (see the Infinite Scrolling section above).
293 * To leverage paging via a toolbar or scroller, you need to set a pageSize configuration on the Store.
296 * var itemsPerPage = 2; // set the number of items you want per page
298 * var store = Ext.create('Ext.data.Store', {
299 * id:'simpsonsStore',
301 * fields:['name', 'email', 'phone'],
302 * pageSize: itemsPerPage, // items per page
305 * url: 'pagingstore.js', // url that will load data with respect to start and limit params
309 * totalProperty: 'total'
314 * // specify segment of data you want to load using params
318 * limit: itemsPerPage
322 * Ext.create('Ext.grid.Panel', {
326 * {header: 'Name', dataIndex: 'name'},
327 * {header: 'Email', dataIndex: 'email', flex:1},
328 * {header: 'Phone', dataIndex: 'phone'}
333 * xtype: 'pagingtoolbar',
334 * store: store, // same store GridPanel is using
338 * renderTo: Ext.getBody()
341 Ext.define('Ext.grid.Panel', {
342 extend: 'Ext.panel.Table',
343 requires: ['Ext.grid.View'],
344 alias: ['widget.gridpanel', 'widget.grid'],
345 alternateClassName: ['Ext.list.ListView', 'Ext.ListView', 'Ext.grid.GridPanel'],
346 viewType: 'gridview',
350 // Required for the Lockable Mixin. These are the configurations which will be copied to the
351 // normal and locked sub tablepanels
352 normalCfgCopy: ['invalidateScrollerOnRefresh', 'verticalScroller', 'verticalScrollDock', 'verticalScrollerType', 'scroll'],
353 lockedCfgCopy: ['invalidateScrollerOnRefresh'],
355 <span id='Ext-grid-Panel-cfg-columnLines'> /**
356 </span> * @cfg {Boolean} [columnLines=false] Adds column line styling
359 initComponent: function() {
362 if (me.columnLines) {
363 me.setColumnLines(me.columnLines);
369 setColumnLines: function(show) {
371 method = (show) ? 'addClsWithUI' : 'removeClsWithUI';
373 me[method]('with-col-lines');