PDA

View Full Version : AutoComplete feature in Ext-JS Grid



emmy mathew
13 Jul 2010, 2:00 AM
I would like to implement AutoComplete feature in Ext-JS Grid.So i used editor: new fm.ComboBox where
fm is var fm = Ext.form;
var grid = new Ext.grid.GridPanel({
store: store,

plugins: [editor],

colModel: new Ext.ux.grid.LockingColumnModel([

editor: new fm.ComboBox({
typeAhead: true,
triggerAction: 'all',
transform: 'light',
hideTrigger: true,
lazyRender: true,
listClass: 'x-combo-list-small'
})

But this gives error "c is null" in firebug console.Does anyone know why it is so?

13 Jul 2010, 4:25 AM
1) that code is incomplete
2) You really need to start posting your code in code tags.

emmy mathew
13 Jul 2010, 5:03 AM
In the Company ComboBox the options are displayed in terms of price.ie When I click on Add button ,new row is added.I have to implement autocomplete feature in that particular combo.That is I have to show the company names through auto complete feature.Now it is showing price in the combo box and as I click Update,corresponding company name gets updated.

Ext.onReady(function(){


Ext.QuickTips.init();


// turn on validation errors beside the field globally
Ext.form.Field.prototype.msgTarget = 'side';


var dataGrid = Ext.data.Record.create([{
name: 'company',
type: 'string'
}, {
name: 'price',
type: 'float'
}, {
name: 'change',
type: 'float'

},{
name: 'pctChange',
type: 'float'
},{
name: 'lastChange',
type: 'date'
}]);

Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
var Checkbox = new Ext.grid.CheckboxSelectionModel();
Ext.getBody().on("contextmenu", Ext.emptyFn, null, {preventDefault: true});
var myData = [
['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am'],
['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am'],
['American Express Company',52.55,0.01,0.02,'9/1 12:00am'],
['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am'],
['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am'],
['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am'],
['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am'],
['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am'],
['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am'],
['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am'],
['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am'],
['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am'],
['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am'],
['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am'],
['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am'],
['International Business Machines',81.41,0.44,0.54,'9/1 12:00am'],
['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am'],
['JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am'],
['McDonald\'s Corporation',36.76,0.86,2.40,'9/1 12:00am'],
['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am'],
['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am'],
['Pfizer Inc',27.96,0.4,1.45,'9/1 12:00am'],
['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am'],
['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am'],
['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am'],
['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am'],
['Verizon Communications',35.57,0.39,1.11,'9/1 12:00am'],
['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am']
];



// example of custom renderer function
function change(val,metadata){
if(val > 1){
// return '<span style="color:green;">' + val + '</span>';
metadata.css = 'green';
}else if(val < 0){
//return '<span style="color:red;">' + val + '</span>';
metadata.css = 'red';
}
return val;
}

// example of custom renderer function
function pctChange(val,metadata){
if(val > 1){
//return '<span style="color:green;">' + val + '%</span>';
metadata.css = 'green';
}else if(val < 0){
// return '<span style="color:red;">' + val + '%</span>';
metadata.css = 'red';
}
return val;
}

// create the data store
var store = new Ext.data.SimpleStore({
autoDestroy: true,
fields: [
{name: 'company',type:'string'},
{name: 'price', type: 'float'},
{name: 'change', type: 'float'},
{name: 'pctChange', type: 'float'},
{name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
]
});
store.loadData(myData);

var fm = Ext.form;

var cp = new Ext.state.CookieProvider({
expires: new Date(new Date().getTime()+(1000*60*60*24*30)) //30 days
});
Ext.state.Manager.setProvider(cp);

var editor = new Ext.ux.grid.RowEditor({
saveText: 'Update'
});
// create the Grid
var grid = new Ext.grid.GridPanel({
store: store,

plugins: [editor],

colModel: new Ext.ux.grid.LockingColumnModel([


{id:'company',header: "Company", width: 160, sortable: true, dataIndex: 'company',

editor:new fm.ComboBox ({

xtype: 'combo',
allowBlank: false,
store: myData,
listClass: 'x-combo-list-small',
triggerAction: 'all',
typeAhead: true,
lazyRender: true
//transform: 'company'


})
},
{id:'price',header: "Price", width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},
{id:'change',header: "Change", width: 75, sortable: true, renderer: change, dataIndex: 'change'},
{id:'perChange',header: "% Change", width: 75, sortable: true, renderer: pctChange, dataIndex: 'pctChange'},
{id:'lstUpdated',header: "Last Updated", width: 85, sortable: true, dataIndex: 'lastChange'}
]),

deferRowRender : true,
trackMouseOver : true,
stateful: true,
stateEvents: ['columnresize', 'columnmove', 'show', 'hide']
,viewConfig:{
forceFit:true

},

initStateEvents : function(){
this.colModel.on('hiddenchange', function(){ this.saveState; });
}
,buttons:[{
scope:this
,text:'Submit'
,handler:function() {
Ext.each(grid.colModel.config,
function(col, index, allCols) {
var colIndex = grid.colModel.findColumnIndex(col.dataIndex);
alert(col.header + ' - ' + colIndex +' - '+col.id);

});
}
},{
scope:this
,text:'Cancel'
,handler:function() {
Ext.MessageBox.confirm('Confirm Action', 'Message', function(btn){
if(btn == 'yes'){
return true;
}
}, this);
}


}],
buttonAlign:'center',
stripeRows: true,
autoExpandColumn: 'company',
height:350,
width:600,
enableDragDrop:true,
view: new Ext.ux.grid.LockingGridView()
,listeners: {

render: function() {
Ext.getBody().on("rowcontextmenu", Ext.emptyFn, null, {preventDefault: true});
}

,contextmenu:function(e) {
if (!this.contextMenu) {
this.contextMenu = new Ext.menu.Menu({
id: 'gridCtxMenu',
items: [{

iconCls: 'add',
text: 'Add Company',
handler: function(){
var e = new dataGrid({
company: 'A',
price: 75,
change: 0.49,
pctChange: 1.35,
lastChange: 56.88


});
editor.stopEditing();
store.insert(0, e);
grid.getView().refresh();
grid.getSelectionModel().selectRow(0);
editor.startEditing(0);
}
},{
text: 'Remove Company',
iconCls: 'remove',
handler: function(){
editor.stopEditing();
var s = grid.getSelectionModel().getSelections();
for(var i = 0, r; r = s[i]; i++){
store.remove(r);
}
}
}]
});
}


var xy = e.getXY();
this.contextMenu.showAt(xy);
}

,render: function(g){

var ddrow = new Ext.ux.dd.GridReorderDropTarget(g, {
copy: false
,listeners: {
beforerowmove: function(objThis, oldIndex, newIndex, records){}
,afterrowmove: function(objThis, oldIndex, newIndex, records){}
,beforerowcopy: function(objThis, oldIndex, newIndex, records){}
,afterrowcopy: function(objThis, oldIndex, newIndex, records){}
}
});

Ext.dd.ScrollManager.register(g.getView().getEditorParent());
}
,beforedestroy: function(g){

Ext.dd.ScrollManager.unregister(g.getView().getEditorParent());
}
}

});


grid.render('grid-example');

});