Critter’s Code

This is a picture of a bridge and a city. I like bridges and cities. They make me smile.

Jan-6-2008

Using ColdFusion.navigate() from within a cfgrid.

Using ColdFusion.navigate() from within a cfgrid.9.0101

I had what seemed like a simple task. Load a query into an html cfgid. Users could then click on a row (record) and be taken to an edit/update page.

Piece of cake. I have done this many times before. Unfortunately, this was my first time attempting it with ColdFusion 8 and all it’s ajaxy goodness. CFGRIDCOLUMN has an ‘href’ attribute, but I was not able to fire off javascript from it, it would only execute regular links. I wanted my edit screen to open up below the grid in a CFDIV.

After much googling and mucking around, I was able to piece together the following. Which adds a click event to the cell.

   1:  addLinksforaTasks = function()
   2:  {
   3:      gTask = ColdFusion.Grid.getGridObject('agrid_tasks');
   4:      cm = gTask.getColumnModel();
   5:      gTask.on('cellclick',function(grid,rowIndex,columnIndex,e)
   6:      {
   7:      var unitID = ColdFusion.getElementValue('agrid_tasks','taskform','taskid');
   8:      ColdFusion.navigate('atasks.cfm?action=edit&taskid='+unitID,'taskedit');
   9:       }
  10:      )
  11:  }

The hardest part to figure out was how to get the id field value (in this case taskid).  It is such a simple thing that I was sure is used constantly, but I could not find much help on the web. Here is the function explained:  ColdFusion.getElementValue(’grid_name’,'form_name’,'columnid’). That gave me the taksid of the selected row in the grid.

To execute the function when the grid loads you need to add

<cfset ajaxOnLoad(’addLinksforaTasks’)> to the page containing the grid.

One other thing to note, if you are loading a grid into a CFDIV, you need to put all helper functions on the parent page. They do not seem to execute correctly if they are included on the page loaded into the CFDIV.

Anyway… hope that can help someone…

VN:F [1.7.5_995]
Rating: 9.0/10 (1 vote cast)
VN:F [1.7.5_995]
Rating: +1 (from 1 vote)
Posted under Ajax, ColdFusion, Samples
  • itisdesign
    Since your post was helpful, I thought I'd post my code as well. To show "detail" view, of selected grid row, in a cfwindow:

    the JavaScript..

    enableRowClick = function() {
    myGrid = ColdFusion.Grid.getGridObject('NameOfCFGRID');
    cm = myGrid.getColumnModel();
    myGrid.on('cellclick', function(grid,rowIndex,columnIndex,e) {
    ColdFusion.Window.show('NameOfCFWINDOW');
    }
    )
    }

    The cfwindow..

    <cfwindow name="NameOfCFWINDOW" source="cfwindow_source.cfm?id={NameOfCFGRID.myIDColumn}" refreshonshow="true" />

    And, of course, also this: <cfset ajaxOnLoad("enableRowClick") />

    Then, the cfwindow_source.cfm can just dump out the URL scope, to see the id is passed thru.
  • Nice one, mate. Thanks
  • itisdesign
    yvw!
  • itisdesign
    Critter,

    This post saved me time, thank you. I agree that <cfgridcolumn href="" should allow specification of a JavaScript function to execute.

    Thanks!,
    -Aaron Neff
  • Glad to hear it. How can you not love search engines lol
  • itisdesign
    search engines rule :)
blog comments powered by Disqus