// example of custom renderer function function italic(value){ return '<i>' + value + '</i>'; }
// example of custom renderer function function change(val){ if(val > 0){ return '<span style="color:green;">' + val + '</span>'; }else if(val < 0){ return '<span style="color:red;">' + val + '</span>'; } return val; } // example of custom renderer function function pctChange(val){ if(val > 0){ return '<span style="color:green;">' + val + '%</span>'; }else if(val < 0){ return '<span style="color:red;">' + val + '%</span>'; } return val; }
// render rating as "A", "B" or "C" depending upon numeric value. function rating(v) { if (v == 0) return "A" if (v == 1) return "B" if (v == 2) return "C" }
// the DefaultColumnModel expects this blob to define columns. It can be extended to provide // custom or reusable ColumnModels var colModel = new Ext.grid.ColumnModel([ {id:'company',header: "Company", 160, sortable: true, locked:false, dataIndex: 'company'}, {header: "Price", 55, sortable: true, renderer: Ext.util.Format.usMoney, dataIndex: 'price'}, {header: "Change", 55, sortable: true, renderer: change, dataIndex: 'change'}, {header: "% Change", 65, sortable: true, renderer: pctChange, dataIndex: 'pctChange'}, {header: "Last Updated", 80, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}, {header: "Rating", 40, sortable: true, renderer: rating, dataIndex: 'rating'} ]);
bd.createChild({tag: 'h2', html: 'Using a Grid with a Form'});
/* * Here is where we create the Form */ var gridForm = new Ext.FormPanel({ id: 'company-form', frame: true, labelAlign: 'left', title: 'Company data', bodyStyle:'padding:5px', 750, layout: 'column', // Specifies that the items will now be arranged in columns items: [{ columnWidth: 0.60, layout: 'fit', items: { xtype: 'grid', ds: ds, cm: colModel, sm: new Ext.grid.RowSelectionModel({ singleSelect: true, listeners: { rowselect: function(sm, row, rec) { Ext.getCmp("company-form").getForm().loadRecord(rec); } } }), autoExpandColumn: 'company', height: 350, title:'Company Data', border: true, listeners: { render: function(g) { g.getSelectionModel().selectRow(0); }, delay: 10 // Allow rows to be rendered. } } },{ columnWidth: 0.4, xtype: 'fieldset', labelWidth: 90, title:'Company details', defaults: { 140, border:false}, // Default config options for child items defaultType: 'textfield', autoHeight: true, bodyStyle: Ext.isIE ? 'padding:0 0 5px 15px;' : 'padding:10px 15px;', border: false, style: { "margin-left": "10px", // when you add custom margin in IE 6... "margin-right": Ext.isIE6 ? (Ext.isStrict ? "-10px" : "-13px") : "0" // you have to adjust for it somewhere else }, items: [{ fieldLabel: 'Name', name: 'company' },{ fieldLabel: 'Price', name: 'price' },{ fieldLabel: '% Change', name: 'pctChange' },{ xtype: 'datefield', fieldLabel: 'Last Updated', name: 'lastChange' }, { xtype: 'panel', layout: 'table', layoutConfig: { columns: 4 }, anchor: '100%', defaults: { border: false, layout: 'form', labelWidth: 15, style: { paddingRight: '10px' } },
// A radio group: A setValue on any of the following 'radio' inputs using the numeric // 'rating' field checks the radio instance which has the matching inputValue. items: [{ cellCls: 'x-form-item', xtype: 'label', text: 'Rating', 98 }, { items: { xtype: 'radio', name: 'rating', inputValue: '0', fieldLabel: 'A' } }, { items: { xtype: 'radio', name: 'rating', inputValue: '1', fieldLabel: 'B' } }, { items: { xtype: 'radio', name: 'rating', inputValue: '2', fieldLabel: 'C' } }] }] }], renderTo: bd });
// Create Panel view code. Ignore. createCodePanel('form-grid.js', 'View code to create this Form'); }); </script>