ColdFusion 8 Grid Context Menu Part II
In our previous example I showed you how you could create a context menu using cfgrid and some Ext magic. Now I would like to take that example a step further. I good use case for a context menu on the grid is giving the user quick access to view or edit the record just by right clicking on the row. Using our new found knowledge and CFWindow this will turn out really nice.
Most of the code is the same from the last example so if you do not understand it click on the link at the beginning of this article. If you remember from our previous example we created a rowcontextmenu event listener for our grid. The important thing to understand is that this method will pass us the grid object, the row that was clicked and the event object. Using the rowIndex we can get the data for the row being clicked. We can use the grid object, get the datasource (data store) and get the row at the index passed in by our event listener.
function init(){
grid = ColdFusion.Grid.getGridObject("ArtistGrid");
grid.addListener("rowcontextmenu", function(grid, rowIndex, e) {
var record = grid.getDataSource().getAt(rowIndex); // Get the Record
});
}
</script>
var artistId = record.data.ARTISTID;
contextMenu.add({text:"Edit Record",handler:function(){
ColdFusion.Window.create("editArtist","Edit Artist " + artistFullName,"editArtist.cfm?id="+artistId,{
modal:true,
width:600,
height:400,
center:true
});
}});
<head>
<title>Edit Row Example</title>
<script type="text/javascript" src="/CFIDE/scripts/ajax/ext/package/menu/menus.js"></script>
<script type="text/javascript">
function init(){
grid = ColdFusion.Grid.getGridObject("ArtistGrid");
grid.addListener("rowcontextmenu", function(grid, rowIndex, e) {
var record = grid.getDataSource().getAt(rowIndex); // Get the Record
//artist name var artistFullName = record.data.FIRSTNAME + " " + record.data.LASTNAME;
var artistId = record.data.ARTISTID;
var contextMenu = new Ext.menu.Menu();
contextMenu.add({text:"Edit Record",handler:function(){
ColdFusion.Window.create("editArtist","Edit Artist " + artistFullName,"editArtist.cfm?id="+artistId,{
modal:true,
width:600,
height:400,
center:true
});
}});
// Stops the browser context menu from showing. e.stopEvent();
// show menu at contextMenu.showAt(e.xy);
console.log(record);
});
}
</script>
</head>
<body>
<cfajaximport tags="CFWINDOW">
<cfquery name="getArtists" datasource="cfartgallery">
SELECT artistId, firstname, lastname, address, city, state, postalcode, email
FROM Artists
</cfquery>
<cfset args = structNew()>
<cfset args.name = "ArtistGrid">
<cfset args.format = "html">
<cfset args.query = "getArtists">
<cfset args.stripeRows = true>
<cfset args.selectColor = "##D9E8FB">
<cfform>
<cfgrid attributeCollection="#args#">
<cfgridcolumn name="artistid" display="false">
<cfgridcolumn name="firstname" header="First Name">
<cfgridcolumn name="lastname" header="Last Name">
<cfgridcolumn name="email" header="Email Address">
<cfgridcolumn name="address" header="Address">
<cfgridcolumn name="city" header="City">
<cfgridcolumn name="state" header="State">
<cfgridcolumn name="postalcode" header="Zip">
</cfgrid>
</cfform>
<cfset ajaxOnLoad("init")>
<cfsavecontent variable="head">
<link href="/CFIDE/scripts/ajax/ext/resources/css/ytheme-aero.css" rel="stylesheet" type="text/css">
<link href="/CFIDE/scripts/ajax/ext/resources/css/menu.css" type="text/css" rel="stylesheet"/>
</cfsavecontent>
<cfhtmlhead text="#head#">
</body>
</html>
