The solution sketched out below definitely has potential for improvement, but can serve as a diving board for the interested.
Demo Application
If 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 Solution
And 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
This would work just like the edit links in my original example, but ready to insert a new employee. Like magic :-)
The Elaborate Solution
The 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 Later
If 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-Event
APEX 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 Dialog
The 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 :-)
Good read as usual! Based on this and previous posts, it seems like you are on your way to becoming the "Dynamic Action Guru" :-)
ReplyDeleteMonkey On Oracle: More On Modal Pop Ups >>>>> Download Now
Delete>>>>> Download Full
Monkey On Oracle: More On Modal Pop Ups >>>>> Download LINK
>>>>> Download Now
Monkey On Oracle: More On Modal Pop Ups >>>>> Download Full
>>>>> Download LINK K6
I have more than 1 button and it calls different pages. useing your e.g. I can call only one page, I can not open other page.
ReplyDeletee.g Button1 link to page 4
button2 link to page 5
@Prasanta
ReplyDeleteYou can open as many as you like, but you have to edit the jQuery Selector expression to include all buttons where you want the dialog.
If you see in my demo application, the last part of the jQuery selector (for the "Modal Dialog - Open" dynamic action) chooses all buttons with value Create. You can create similar selectors for ALL your buttons.
If you have buttons with value Button1, Button2, etc, include them in the jQuery selector: button[value=Button1],button[value=Button2],etc, or maybe button[value^=Button] (for all buttons with value starting with Button).
Good luck!
Thank you very much for the info but the problem is , on page load code it is not changing the button title, it is only changing for one button, because button2 is conditional and not visible on the first time.
ReplyDeletevar origAction = $('button[value=Create]').attr('onclick').toString();
@PKP
ReplyDeleteIf you look at the onload dynamic action, it fires once on page load. If the button does not exist at that time, it will not be picked up.
You can move the true event from page load action to onclick action to execute the code there. Or maybe attach it to the event that creates the new button in the first place. Then you probably can also tap into the triggeringElement variables and make the javascript more generic.
When I look at the code I wrote in the demo application, I see it's more than a little crude (and naive). If I have some time later, I will update it.
hi,
ReplyDeleteI imported the app in my workspace but, it does not work in IE!! i got error in "var origAction = $('button[value=Create]').attr('onclick').toString();" , telling me that .attr(.) null
thank you
@moufix
ReplyDeleteI'm guessing you are using IE6? It is not tested with IE6. It works in IE8.
I do not have the time now to change the code for IE6 compatibility, but it should be possible.
I was on IE8. However, I clear the IE cache, and it works now perfectly.
ReplyDeletethank you dud/
I love your blog entry on doing Modal windows and I am able to get it to work for existing items to be edited using my set of themes. However, getting the create button to work is proving an issue..
ReplyDeleteIf you have a few free minutes could I have you look at my sample page and see if anything stands out for you?
Please e-mail me and I will send along temporary account access information..
Thank you,
Tony Miller
Webster, TX
This comment has been removed by a blog administrator.
ReplyDeleteHi everyone.
ReplyDeletePlease help me.
When I close the popup window, my report refreshes but if I choose other item, the popUp window doesn't work and the link sends me to the page.
What could be the solution?.
Thanks.
@Mery
ReplyDeleteIt works the first time, but not after refresh.
This is typical if you forgot to set "live" for the Event Scope (under Advanced) for the Dynamic Action.
After the report refreshes, jQuery looses all triggers, unless you tell it to continualy poll for all matching trigger objects.
Hi Håvard Kristiansen
ReplyDeleteThank you so much for your help. It works now.
Thanks
Looks great!
ReplyDeleteOne quick question...How do you manage validations? A validation error usually requires a page refresh. Won't the popup be gone when the page refreshes or does the iframe manage that?
@yodahart
ReplyDeleteValidation in a popup window works just as in an ordinary page. If a validation error occurs, the branch that closes the popup will not be reached.
Be sure to test it with failed validations, because you are dealing with a limited amount of space in a popup, and inline messages tend to lead to user scrolling.
Hi and thank you so much for ur helpful post
ReplyDeleteI have a question.In my page I need to do this for several table.Let's say I need to create employee, department, order.So in one page I will have several create buttons.how can make 3 of them working?Thank you so much in advance
@Zahra
ReplyDeleteThis is not a problem. You just have to make sure that your jQuery selector catches all the buttons. See the second and third comment on this post.
This comment has been removed by the author.
ReplyDeleteDear Havard,
ReplyDeleteThank you for ur replay.This is weird problem that I have.Is it possible due to having different template I get errors?
For instance : if I click on buttons on the page no matter if they r in same or various regions I get alert of their address due to
function redirect (url) {
alert(url);}
or if I don't comment background: "black" when I click on button nothing will happen.i am sorry if my questions are too basics
Thank you in advance.
Hi Havard,
ReplyDeleteI tried this and this is gr8 :). But can we put a validation in the page which is popping up as modal page. I tried to add a validation, but it is not working.
Thanks,
Jyothish
@zahra
ReplyDeleteI am very unsure of what you are trying to do, or what is wrong with your page. I suggest you set up an example on apex.oracle.com and ask for help on the APEX form on forums.oracle.com. Best I can do for now, sorry about that.
@TJ
Page validation on the parent page will not occur when launching a modal page using this method. You can create any validations necessary on the modal page though. Both on render time, and on page processing.
Hello,
ReplyDeleteI have imported the application and I have to use some features and it worked perfectly in Firefox, but in IE the pop up is not launched. I only receive an alert message with the link. Could you help me to sole the problem?
Thank you,
Ruxandra
@Ruxandra
ReplyDeleteThe pop up in the sample app works in IE8. If you have modified it, and it does not work, then import again and redo your changes one at a time until it fails.
jQuery is very good at handling different browsers, and jQuery is responsible for this pop up code. My guess is the problem lies somewhere else.
If you get stuck, set up an example at apex.oracle.com and ask for help with it.
Good luck :-)
PS: Beware of console.log, it does not work in IE (unless you have the IE developer console open). This has caught me off guard more than once...
Hi again,
ReplyDeleteJust a question. This example is done using a standard APEX report. How hard would it be to modify it to work with an Interactive Report?
@tony
ReplyDeleteShould be no sweat with an Interactive Report.
Remember: The key is to make your jQuery selector to include all anchors/buttons you want to affect, the rest just works.
Ok, just a comment or concern.. In using your dynamic actions, I have a standard report calling a form. When the report initially loads I have pagination going from page 1-15 and so on. After I return from the form (cancel/create/save) my report is defaulting to a pagination of 1-10 items, as apposed to 1-15..
ReplyDeleteAny ideas as to what in the dynamic actions could be doing this?
Found it!! The standard report had the perform partial page refresh checked. As soon as I removed this the page is formatting fine.. Any ideas?
ReplyDeleteTony, I had a problem like that once. You are using Delta theme, right? The standard Delta theme had a bug in that it reset lines per page to 10 after a PPR regardless of the original setting. Many a happy hour was spent tracking that down!
DeleteMonkey, great post, very instructive, but I haven't been able to work out the best way to pass back the ID of an entity created in the modal page. What would be your best practice recommendation for doing that?
Many thanks
@Tony
ReplyDeleteSounds like the APEX session repository is not refreshed properly to reflect your report changes. This should work with Enable Partial Page Refresh.
This method is great, and I've made use of it throughout my application. My problem is that my service provider just upgraded to 4.1 and the HTP.P close mechanism isn't working anymore. I was hoping to test out your simple app, but the download link isn't working.
ReplyDeleteI have no idea what is up with 4.1 and closing, but the download should work now.
ReplyDeleteHi,
ReplyDeleteJust wondering why I can't download the demo?
I get this:
Error Error processing branch.
ORA-01653: unable to extend table KOMPETANSEGRUPPE.SAMPLES_LOG by 128 in tablespace FLOW_2906
Thanks
@happy86
ReplyDeleteShould work again now
Monkey, great post, very instructive, but I haven't been able to work out the best way to pass back the ID of an entity created in the modal page. What would be your best practice recommendation for doing that?
ReplyDeleteAnd for the record... nothing wrong with the Quick Solution philosophy, I reckon. The "great" is the enemy of the "good".
Many thanks
great help! could you check your download though. Each time I have tried to download, I receive app 115 but it does not contain the MODAL DIALOG WITH CREATE page. Thanks!
ReplyDeleteKaren
Uploaded a new version, should work now.
ReplyDeleteThis sample is getting quite old, and always was a bit of a clunky hack. I would check out more recent samples or plugins.
Thank you!
ReplyDeleteI just downloaded. Sadly, I still cannot get my button to open a modal pop-up...though the link works perfectly. The button redirect to a page, that is defined as popup...but alas. I will keep fiddling. thanks again for your help.
Hi Håvard - love the solution, but I have an IE8 issue that's driving me wild. I thought you might be able to provide some direction. The thing that really bothers me though is your apex.oracle.com app does not reproduce the issue.
ReplyDeleteThis seems like a common complaint:
'attr(...)' is null or not an object
I've triple checked my example against yours; checked the browser source produced at runtime; tried some alternatives... I just can't beat it.
It's stopping the button action from opening the modal dialog, instead it redirects the page.
Any thoughts or suggestions are appreciated! cheers
Hi Scott,
ReplyDeleteSorry for the late response, haven't had much time lately.
To start on the basic side:
- You are sure there are no caching issues involved (as moufix commented on above)?
- Can you catch anything in an alert? console.log is pretty poor option when leaving page :)
- If you change the button link to something trivial like #, can you catch the triggering event?
Just a few thoughts...
Good Luck!
Please help me, very good post, but when saved in the popup does not update the region
ReplyDeleteHi Havard,
ReplyDeleteI've done everything as it is in the post and I've also downloaded application and copied all parts from it. Your application is working perfect. But mine is not working. I get a message: "The owner of the homepage doesn't allow to show its content in iframe." And I have a link to a page I want to open in the iframe.
Could you help me with it? Is it because of version of script or may be APEX version?
I'm using APEX 4.1.0.00.32
Thank you in advance.
Your problem is probably caused by a new security setting in 4.1. Look in application properties, security tab, browser properties section, and adjust your setting for "Embed in frames".
DeleteThank you, Rumpelstiltskin! Thank you a lot!
DeleteI spent 2 days to fix it. It is working now!!!
I've used this tutorial for normal page to create an employee, it is working fine. But when I tried to use Tabular form for pop-up page it is not working. Who knows what is the problem? is it because of tabular form or I made some mistake? If it is because of tabular form could anybody help to show how to use tabular form as a pop-up page? Thank you inadvance!
ReplyDeleteMonkey On Oracle: More On Modal Pop Ups >>>>> Download Now
ReplyDelete>>>>> Download Full
Monkey On Oracle: More On Modal Pop Ups >>>>> Download LINK
>>>>> Download Now
Monkey On Oracle: More On Modal Pop Ups >>>>> Download Full
>>>>> Download LINK a2