Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / examples / grid / binding.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 Ext.require([
16     'Ext.grid.*',
17     'Ext.data.*',
18     'Ext.panel.*',
19     'Ext.layout.container.Border'
20 ]);
21
22 Ext.onReady(function(){
23     Ext.define('Book',{
24         extend: 'Ext.data.Model',
25         fields: [
26             // set up the fields mapping into the xml doc
27             // The first needs mapping, the others are very basic
28             {name: 'Author', mapping: 'ItemAttributes > Author'},
29             'Title',
30             'Manufacturer',
31             'ProductGroup',
32             'DetailPageURL'
33         ]
34     });
35
36     // create the Data Store
37     var store = Ext.create('Ext.data.Store', {
38         model: 'Book',
39         proxy: {
40             // load using HTTP
41             type: 'ajax',
42             url: 'sheldon.xml',
43             // the return will be XML, so lets set up a reader
44             reader: {
45                 type: 'xml',
46                 record: 'Item',
47                 totalProperty  : 'total'
48             }
49         }
50     });
51
52     // create the grid
53     var grid = Ext.create('Ext.grid.Panel', {
54         store: store,
55         columns: [
56             {text: "Author", width: 120, dataIndex: 'Author', sortable: true},
57             {text: "Title", flex: 1, dataIndex: 'Title', sortable: true},
58             {text: "Manufacturer", width: 115, dataIndex: 'Manufacturer', sortable: true},
59             {text: "Product Group", width: 100, dataIndex: 'ProductGroup', sortable: true}
60         ],
61         viewConfig: {
62             forceFit: true
63         },
64         height:210,
65         split: true,
66         region: 'north'
67     });
68         
69     // define a template to use for the detail view
70     var bookTplMarkup = [
71         'Title: <a href="{DetailPageURL}" target="_blank">{Title}</a><br/>',
72         'Author: {Author}<br/>',
73         'Manufacturer: {Manufacturer}<br/>',
74         'Product Group: {ProductGroup}<br/>'
75     ];
76     var bookTpl = Ext.create('Ext.Template', bookTplMarkup);
77
78     Ext.create('Ext.Panel', {
79         renderTo: 'binding-example',
80         frame: true,
81         title: 'Book List',
82         width: 540,
83         height: 400,
84         layout: 'border',
85         items: [
86             grid, {
87                 id: 'detailPanel',
88                 region: 'center',
89                 bodyPadding: 7,
90                 bodyStyle: "background: #ffffff;",
91                 html: 'Please select a book to see additional details.'
92         }]
93     });
94     
95     // update panel body on selection change
96     grid.getSelectionModel().on('selectionchange', function(sm, selectedRecord) {
97         if (selectedRecord.length) {
98             var detailPanel = Ext.getCmp('detailPanel');
99             bookTpl.overwrite(detailPanel.body, selectedRecord[0].data);
100         }
101     });
102
103     store.load();
104 });