bcamp1973
14 Nov 2007, 8:48 AM
I'm *very* new to ExtJS so i'm just learning the ropes. My Javascript (and programming skills in general) are fairly basic. I'm attempting to build a grid where the last column contains one or more buttons. The data for the grid is being pulled from an xml file. I'm guessing the buttons would be a plugin? How would i accomplish that?
Here's my xml...
<catalog>
<attribute>
<name>First Name</name>
<type>Text</type>
<min></min>
<max>30</max>
<defaultVal></defaultVal>
<required>true</required>
<action></action>
</attribute>
<attribute>
...
</attribute>
</catalog>
and my javascript...
Ext.onReady(function(){
Ext.QuickTips.init();
// shorthand alias
var fm=Ext.form;
// custom column plugin example
var checkColumn=new Ext.grid.CheckColumn({
header:"Required?",
dataIndex:'required',
width:75
});
// the column model has information about grid columns
// dataIndex maps the column to the specific data field in
// the data store (created below)
var cm=new Ext.grid.ColumnModel([{
id:'name',
header:"Name",
dataIndex:'name',
width:220,
editor:new fm.TextField({
allowBlank:false
})
},{
header:"Type",
dataIndex:'type',
width:130,
editor:new Ext.form.ComboBox({
typeAhead:true,
triggerAction:'all',
transform:'type',
lazyRender:true,
listClass:'x-combo-list-small'
})
},{
header:"Min",
dataIndex:'min',
width:50,
align:'center',
editor:new fm.TextField({
allowBlank:true
})
},{
header:"Max",
dataIndex:'max',
width:50,
align:'center',
editor:new fm.TextField({
allowBlank:true
})
},{
header:"Default",
dataIndex:'defaultVal',
width:80,
align:'left',
editor:new fm.TextField({
allowBlank:true
})
},
checkColumn,
{
header:"Action",
dataIndex:'action',
width:80,
align:'left'
}
]);
// by default columns are sortable
cm.defaultSortable=true;
// this could be inline, but we want to define the Plant record
// type so we can add records dynamically
var Attribute=Ext.data.Record.create([
// the "name" below matches the tag name to read, except "availDate"
// which is mapped to the tag "availability"
{name:'name', type:'string'},
{name:'botanical', type:'string'},
{name:'type'},
{name:'min'},
{name:'max'},
{name:'defaultVal'},
{name:'required', type:'bool'},
{name:'action'}
]);
// create the Data Store
var store=new Ext.data.Store({
url:'assets/xml/attributes.xml',
reader:new Ext.data.XmlReader({record:'attribute'}, Attribute),
sortInfo:{field:'name', direction:'ASC'}
});
// create the editor grid
var grid=new Ext.grid.EditorGridPanel({
store:store,
cm:cm,
renderTo:'editor-grid',
autoWidth:true,
height:300,
autoExpandColumn:'name',
title:'Edit Attributes?',
frame:true,
plugins:checkColumn,
clicksToEdit:1,
tbar:[{
text:'Add Attribute',
handler:function(){
var p=new Attribute({
name:'New Attribute Name',
type:'Text',
min:0,
max:0,
defaultVal:'',
required:false,
action:'?'
});
grid.stopEditing();
store.insert(0, p);
grid.startEditing(0, 0);
}
}]
});
// trigger the data store load
store.load();
});
Ext.grid.CheckColumn=function(config){
Ext.apply(this, config);
if(!this.id){
this.id=Ext.id();
}
this.renderer=this.renderer.createDelegate(this);
};
Ext.grid.CheckColumn.prototype={
init:function(grid){
this.grid=grid;
this.grid.on('render', function(){
var view=this.grid.getView();
view.mainBody.on('mousedown', this.onMouseDown, this);
}, this);
},
onMouseDown:function(e, t){
if(t.className && t.className.indexOf('x-grid3-cc-'+this.id) != -1){
e.stopEvent();
var index=this.grid.getView().findRowIndex(t);
var record=this.grid.store.getAt(index);
record.set(this.dataIndex, !record.data[this.dataIndex]);
}
},
renderer:function(v, p, record){
p.css += ' x-grid3-check-col-td';
return '<div class="x-grid3-check-col'+(v?'-on':'')+' x-grid3-cc-'+this.id+'">*</div>';
}
};
Here's my xml...
<catalog>
<attribute>
<name>First Name</name>
<type>Text</type>
<min></min>
<max>30</max>
<defaultVal></defaultVal>
<required>true</required>
<action></action>
</attribute>
<attribute>
...
</attribute>
</catalog>
and my javascript...
Ext.onReady(function(){
Ext.QuickTips.init();
// shorthand alias
var fm=Ext.form;
// custom column plugin example
var checkColumn=new Ext.grid.CheckColumn({
header:"Required?",
dataIndex:'required',
width:75
});
// the column model has information about grid columns
// dataIndex maps the column to the specific data field in
// the data store (created below)
var cm=new Ext.grid.ColumnModel([{
id:'name',
header:"Name",
dataIndex:'name',
width:220,
editor:new fm.TextField({
allowBlank:false
})
},{
header:"Type",
dataIndex:'type',
width:130,
editor:new Ext.form.ComboBox({
typeAhead:true,
triggerAction:'all',
transform:'type',
lazyRender:true,
listClass:'x-combo-list-small'
})
},{
header:"Min",
dataIndex:'min',
width:50,
align:'center',
editor:new fm.TextField({
allowBlank:true
})
},{
header:"Max",
dataIndex:'max',
width:50,
align:'center',
editor:new fm.TextField({
allowBlank:true
})
},{
header:"Default",
dataIndex:'defaultVal',
width:80,
align:'left',
editor:new fm.TextField({
allowBlank:true
})
},
checkColumn,
{
header:"Action",
dataIndex:'action',
width:80,
align:'left'
}
]);
// by default columns are sortable
cm.defaultSortable=true;
// this could be inline, but we want to define the Plant record
// type so we can add records dynamically
var Attribute=Ext.data.Record.create([
// the "name" below matches the tag name to read, except "availDate"
// which is mapped to the tag "availability"
{name:'name', type:'string'},
{name:'botanical', type:'string'},
{name:'type'},
{name:'min'},
{name:'max'},
{name:'defaultVal'},
{name:'required', type:'bool'},
{name:'action'}
]);
// create the Data Store
var store=new Ext.data.Store({
url:'assets/xml/attributes.xml',
reader:new Ext.data.XmlReader({record:'attribute'}, Attribute),
sortInfo:{field:'name', direction:'ASC'}
});
// create the editor grid
var grid=new Ext.grid.EditorGridPanel({
store:store,
cm:cm,
renderTo:'editor-grid',
autoWidth:true,
height:300,
autoExpandColumn:'name',
title:'Edit Attributes?',
frame:true,
plugins:checkColumn,
clicksToEdit:1,
tbar:[{
text:'Add Attribute',
handler:function(){
var p=new Attribute({
name:'New Attribute Name',
type:'Text',
min:0,
max:0,
defaultVal:'',
required:false,
action:'?'
});
grid.stopEditing();
store.insert(0, p);
grid.startEditing(0, 0);
}
}]
});
// trigger the data store load
store.load();
});
Ext.grid.CheckColumn=function(config){
Ext.apply(this, config);
if(!this.id){
this.id=Ext.id();
}
this.renderer=this.renderer.createDelegate(this);
};
Ext.grid.CheckColumn.prototype={
init:function(grid){
this.grid=grid;
this.grid.on('render', function(){
var view=this.grid.getView();
view.mainBody.on('mousedown', this.onMouseDown, this);
}, this);
},
onMouseDown:function(e, t){
if(t.className && t.className.indexOf('x-grid3-cc-'+this.id) != -1){
e.stopEvent();
var index=this.grid.getView().findRowIndex(t);
var record=this.grid.store.getAt(index);
record.set(this.dataIndex, !record.data[this.dataIndex]);
}
},
renderer:function(v, p, record){
p.css += ' x-grid3-check-col-td';
return '<div class="x-grid3-check-col'+(v?'-on':'')+' x-grid3-cc-'+this.id+'">*</div>';
}
};