I previously wrote
a post on how to use the new APEX 4.0 native features to conjure modal inline dialogs. It was followed by a brief (very brief on my part, to say the least) discussion on how to achieve the same functionality for a create button. In this post I will elaborate a bit on how to do exactly that.
The solution sketched out below definitely has potential for improvement, but can serve as a diving board for the interested.
Demo ApplicationIf you are curious, or just down right bored with long posts, then I have a running copy of the demo application here:
http://apex.oracle.com/pls/apex/f?p=45420:3. The demo application can be downloaded here:
http://apex.oracle.com/pls/apex/f?p=45410:DOWNLOAD. To keep the examples as clean as possible, I have created a new page with the Create-button, so you have both alternatives available.
The Quick SolutionAnd what is wrong with a quick solution? Personally I feel the we programmers (me definitely included) are very good at complicating things. The rest of this post is dedicated to a more generic approach, but in my original comment I suggested this:
- Create an anchor-tag (link) anywhere on the page
- Set href attribute to the URL of your edit form page, let primary key item values remain blank (but be sure to pass them)
- Set the id attribute to callModalDialog
In my sample application that would be: <a id="callModalDialog" href="f?p=&APP_ID.:4:&SESSION.::NO::P4_EMPNO:">Create Employee</a>.
This would work just like the edit links in my original example, but ready to insert a new employee. Like magic :-)
The Elaborate SolutionThe elaborate solution uses more Dynamic Actions, a bit more javascripting and a couple of dirty tricks to accomplish the same as the quick solution. The goal is to make a normal create button to have the same behavior with inline modal dialog, as the edit links does.
The explanation below is based on the original modal dialog page in my sample application. To follow the example and repeat the steps, you need to start off with a copy of page 2 in my sample application.To follow the example, start by creating a region button with the following values (mostly just accept defaults):
Button name: CREATE
The button should take you to the create/edit page, but without the fancy modal dialog.
A Note for LaterIf you name your button to something else than Create (or Text Label/ALT-property for the page button in APEX), you must adjust the following JQuery selectors accordingly for it to work. You may also expect some issues when using an image button (the javascript onclick function extraction may fail).
Removing the Original onclick-EventAPEX uses a javascript function to redirect the browser (also when using anchor button template). There are two things that needs to be done when the page loads; store the original create link, and remove the original button onclick event. I use JQuery and javascript native regexp capabilities to achieve this.
To create a Dynamic Action which fires when the page has finished loading, do the following:
Create a new Dynamic Action at the page level
Choose Advanced
Give it a sensible name ("On Page Load")
Choose Event Page Load
Choose Action Execute Javascript Code, and paste in the javascript code below:
/* get original onclick event */
var origAction = $('button[value=Create]').attr('onclick').toString();
/* get link from original onclick event using regular expression */
var link = origAction.match(/(redirect\((\'|\"))([^\'\)|\"\)]*)/)[3];
/* Remove original onclick event */
$('button[value=Create]').removeAttr('onclick');
/* store link as title attribute of button */
$('button[value=Create]').attr('title', link);
Looks a bit Greek? Even if I have actually included comments? If you are not familiar with JQuery, it definitely will. If you are not familiar with regular expressions, even more so. If you are not familiar with javascript at all, you are allowed to test the code, but not use it in production unless you have truly understood what it means :-) There are very good sources on the web for all the knowledge required.
And why, oh, why store the link value in an element attribute definitely not meant to hold a link value!?! It will overwrite any existing title-values, and makes both setting and retrieving code hard to read. On the other hand, it will keep the value with it's element, and support more than one create button on any given page. There are more than one alternate way of doing this, but hey, feel free to bring suggestions :-)
Anyway, click Create and you are done.
If you run your page now, clicking the Create button will not do anything (the onclick event was removed, but not replaced).
That was the hard part!
Calling the DialogThe edit dialog Dynamic Action is already in place, all that has to be done is to adapt the existing Dynamic Action to include the new create button.
Edit the Modal Dialog Dynamic Action
Include: button[value=Create] as the JQuery Selector expression (total expression will now be: a[id^=callModalDialog],button[value=Create])
Edit the True Action Javascript Expression to be:
/* prevent default behaviour on click */
var e = this.browserEvent;
e.preventDefault();
/* Find page link */
var link;
if (this.triggeringElement.tagName=='A') {
link = this.triggeringElement.href;
} else if (this.triggeringElement.tagName=='BUTTON') {
link = this.triggeringElement.title;
}
/* Trigger JQuery UI dialog */
var horizontalPadding = 30;
var verticalPadding = 30;
$('<iframe id="modalDialog" src="' + link + '" />').dialog({
title: "Edit Employee",
autoOpen: true,
width: 700,
height: 300,
modal: true,
close: function(event, ui) {apex.event.trigger('#P3_AFTER_MODAL','select',''); $(this).remove();},
overlay: {
opacity: 0.5,
background: "black"}
}).width(700 - horizontalPadding).height(300 - verticalPadding);
return false;
The difference from the original javascript code is the extra bit extracting the link to use, which differs from a normal anchor element, and our button with the special id attribute.
Click Apply and you are done.
The Rest?Is the same as detailed in my last post. I have updated the dialog page in the sample application to include Create and Delete buttons, but that is it.
Quick or Slow?Quick sound promising! Simplicity rules! There are some draw-backs, of course. Like where to put the link to find it later (a Display Only item properly named perhaps?)? Like the need to style a link as a button? Like the need to place the link in a region template position? It all adds up.
The generic (more elaborate) approach will work with any create button (well, not with the anchor template). Drop the Dynamic Actions onto the page, and it will work. On the other hand, the generic approach involves more code, more code is harder to maintain and more prone to breaking. Generic code requires a delicate hand (ie takes time), and is generally harder to read than code created for a specific task. This is all in the eye of the beholder, but on more than one occasion I have had the pleasure of revisiting my own old code and though: I didn't need to do that... I digress, I know. Besides, that is too great a topic to just be delegated to a digression :-)
So, should you use the quick link (pun intended :-))? My answer (being a consultant) is a definitive: It depends!
Enjoy :-)