Adding Custom Buttons to <cfwindow/>

Word Count: 525

The other day I wrote a short article on ColdFusion 8 custom grid toolbars. One reader had a question that I would like to share with you. Trond writes -

Do you know if a similar toolbar can be added to the bottom of a cfwindow?
The answer to this question is two fold. No there is not a panel are on the bottom like there is in the grid or even in the window components in Ext 2.0. This does not prevent you from customizing your windows though. You can add buttons to the window and I will show you how quickly you can do so. After the page loads I am using the ajaxOnLoad method to call my init method.

The first thing I need to do is create my window. You can see I have setup some basic properties on my window and using initShow=true will cause the window to show on page load.

<cfwindow name="MyWindow" center="true" closable="true" draggable="true" height="200" modal="true"
          initShow="false" minHeight="100" minWidth="200" resizable="true" title="Notify Administrator" width="400">

      Type Message here
   </cfwindow>

   <cfset ajaxonload("init")>
Here is the only part you need to wrap your head around. When we use the cfwindow tag ColdFusion imports all of the Ext framework libraries necessary to create the window. The button classes are not part of that include so we need to explicitly import them.
<script type="text/javascript" src="/CFIDE/scripts/ajax/ext/package/button/button.js"></script>
Now that we have our button class this is very simple. Here we have our init method that should run after the page is setup. Because there is no attribute of cfwindow for adding buttons we need to create an a event listener on the window. To do that we must first get the window object. After we have the window object we can say here is what we want you to do before you show the window component. The action we want it to do is to add a button. The window has a method for adding a button. The button takes a button config and you can learn more about that in the docs but its pretty straight forward. Finally we add a handler to our button that will call the method sayHello when its clicked.
<script language="JavaScript">
   function init(){
      myWindow = ColdFusion.Window.getWindowObject('MyWindow');
         
      myWindow.addButton({
         text:"My Button",
         cls:"x-btn-text-icon",
         icon:"add.png",
         handler:sayHello
      });
   }
   function sayHello(){
      alert("hello");
   }
   </script>

Comments

#1 Posted By: Scott Bennett Posted On: 3/7/08 12:58 PM
Nice! I played around with adding a button to a cfwindow in an article I wrote back in early December:
http://www.coldfusionguy.com/ColdFusion/blog/index...

but I didn't notice you could use assign a button icon and a class and all that. Pretty cool stuff, Thanks.
#2 Posted By: Dan Vega Posted On: 3/7/08 1:03 PM |
Author Comment
Nice! I actually read that article Scott, good stuff. The addButton() method takes either a string or a config object. Passing the string will simply put the text on the button but if you pass a config object you have all kinds of options. Check out the documentation for the button class
#3 Posted By: Trond Ulseth Posted On: 4/1/08 8:42 AM
Thanx a lot for this (and all your other posts on the new cf8 ajax/ext stuff). It has really helped me a lot.
#4 Posted By: Trond Ulseth Posted On: 4/7/08 10:49 AM
Follow up question :)

Is it possible to add a textfield to the bottom of the window as well?

I want to have a textfield where the user can type his email address, and a button for posting the form inside the window.

I have tried this, but get errors saying its's not a method:

var text = new Ext.form.TextField({
id:"to",
fieldLabel:"To",
width:275,
allowBlank:false,
blankText:"Please enter a to address"
   })

myDownloadDrawingsWindow.addField(text);

Thanx again for all the great posts,
Trond
#6 Posted By: poplop Posted On: 6/18/08 12:32 PM
Pretty cool stuff, Thanks.
http://ru-t.com/about antimaulnetizm ??????????????


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.