apex:actionFunction and Database.Update() unintended page refresh - ajax

So, I've been working on an "edit item" modal for a visualforce page that will allow the user to edit a child component of an object, and refresh the page. As it stands, there is a single URL parameter that contains the parent objects ID. The code is structured very similar to this:
<apex:form id="edit-modal">
<!-- Modal Content-->
<apex:actionFunction action="{!updateModalObject}" name="updateModalObject">
</apex:actionFunction>
</apex:form>
When the user preses a "save" button in the modal, the aforementioned actionfunction is called. The class in the controller looks like this:
public PageReference updateModalObject(){
database.update(modalObject);
return null;
}
When this action completes, the page is refreshing, also dropping the URL parameters and causing the whole thing to sort of.. gum up. I'm not sure which portion of the code is causing the refresh, if it's the actionfunction or if it's the update in the controller.

You said:
When the user preses a "save" button...
I think this is causing the page refresh. Do you use the command button without reRender tag? The solution might look like this:
Just try this trick out - add return false after executing your JavaScript function:
<apex:commandButton value="Save" onclick="updateModalObject(); return false;"/>
Other way to avoid page reload is to add a dummy reRender tag:
<apex:commandButton value="Save" onclick="updateModalObject()" reRender="none" />
or
<apex:actionFunction action="{!updateModalObject}" name="updateModalObject" reRender="none">
You must to re-render "something" to avoid the page reload. Otherwise the command button will reload the whole page. In our case we will re-renden "nothing".

Related

codeignighter redirect issues to refresh

I have a registration page.after completing the registration the user will redirect to a new page. In new page when I click the back button I redirect to last page with last data filled up.how to refresh the page to delete form data. The framework is codeignighter.
To refresh or redirect page in codeigniter there is function redirect(YOUR_URL).
Have a Look in Helper class for more details
If you are referring to browser back button keep autocomplete off for the form fields in your first form
You can use autocomplete for the complete form
<form action="/action_page.php" autocomplete="off">
Or specifically to each control as
<input type="email" name="email" autocomplete="off">
You can also make it on for specific controls/entire form if you need it
if your problem still persists you could use
<body onload="document.refresh();">
This shall refresh the page on loading for the first time
include this in your page if required to force reset the form on load if you need you can bind this with a javascript onload function
<script type="text/javascript">
document.getElementById("myForm").reset();
</script>
in case of application back button {if used with href or redirect()}
Shall keep form clean even if autocomplete is not set unless the user takes from browser autofill or you fill them with code

passing input text's value to server side on click of a button in JSF

I have a text box which takes a search value, and i want to send this string to the server side on click of a button. Not by a form submit, by an ajax call.
I had added an actionListener to the input tag itself, which is called on blur. But what i really want is for the user to click the button to trigger the search function.
I got an idea from this question, and implemented it this way:
<h:inputText id="likeMaterial" value="#{createBookingForm.searchText}"></h:inputText>
<a4j:jsFunction name="setParameterAndRerender" actionListener="{bean.searchMaterials}" reRender="searchResult">
<a4j:actionparam name="param1" assignTo="#{createBookingForm.searchText}"/>
</a4j:jsFunction>
<h:commandButton value="Search" onclick="setParameterAndRerender('mySearchText');return false;"></h:commandButton>
The value received at server side is of course, "mySearchText". How do i pass what the user enters? Or how do i bind #{createBookingForm.searchText} before the button's action listener is called?
Im open to any other approach to. I have limitations though : Im working on enhancing a legacy application, built using JSF 1.1. I cant upgrade, not without a fight at least!
Edit : I tried doing it this way, but i get "undefined" on the server side.
Why not use a4j:commandButton instead of h:commandButton? It will execute ajax request and render what you want. No form submit will happen. Loks like what you need.
h:commandButton by default submit the form when clicked. So no need to send specially using a4j:jsFunction or in any other way. You can completely remove your js function unless if you have something else to do. If you want to test that add an action method and print the value of searchText variale in createBookingForm bean.
Hope this helps!!
This is the whole solution
<h:inputText id="likeMaterial" value="#{createBookingForm.searchText}"></h:inputText>
<a4j:commandButton reRender="searchResult" actionListener="#{createBookingForm.searchMaterials}">
<a4j:actionparam name="param1" noEscape="true" value="document.getElementById('likeMaterial').value" assignTo="#{createBookingForm.searchText}" />
</a4j:commandButton>

Launch second Reveal Modal via Ajax (Zurb Foundation 5 + WordPress)

I'm using Foundation 5 & WordPress.
I am trying to launch a second Reveal Modal from an AJAX loaded Reveal Modal. It's not working for me.
I have two divs at the bottom of my page:
<div id="industryModal" class="reveal-modal" data-reveal></div>
<div id="portfolioModal" class="reveal-modal" data-reveal></div>
I launch the first modal with content from another page (so far so good):
<a id="business-services-link" href="/approach/investment-strategy/industry/business-services" data-reveal-id="industryModal" data-reveal-ajax="true">
First Reveal Modal works correctly. I then try to launch a second modal (from the first AJAX loaded content):
<a href="<?php the_permalink(); ?>" data-reveal-id="portfolioModal" data-reveal-ajax="true">
Now I am simply taken to the new page. The content is not loaded into a second modal. I've tried adding the #portfolioModal div on the original page, and on the page loaded into the first modal. In neither case is the third page loaded into the second modal.
Any idea what I'm doing wrong?
You need to call the second modal using javascript on ajax returned modal page.
Try this:
Add the second modal wrapper somewhere in your document first (you cannot reuse the currently opened modal):
<div id="modal-anotherPage" class="reveal-modal auto_width medium" data-reveal></div>
Then set event handler for links inside your current modal:
$('a.linksinsideyourmodal').click(function(e) {
$('#modal-anotherPage').foundation('reveal', 'open', {
url: $(this).attr('href')
});
return false;
});
Note that this only works with foundation version 5.2.0, the one before it somehow doesn't want to return ajax content on second modal.
Your problem might be that
<?php the_permalink(); ?>
returns an address with "http://".
Reveal wont use that address correctly. Reveal will only use ajax with relative URLs, not absolute ones, and it will only work when referencing files on the same server.

In JSF on an ajax call, how do we create a HTML fragment using a different JSF page for rendering?

I have a JSF page in which I have a div which acts as a popup window.
This popup is displayed when the user clicks on a certain button or link, until which it is hidden.
I would like to have another JSF page that provides the content for this div via an AJAX call.
I vaguely remember doing this using Struts Action and JSP fragment.
Is it possible to do this in JSF 2.0?
ADDITIONAL DETAILS:
My scenario is as follows:
I have a page that displays the details of employees as a summary table using the dataTable tag with a class EmployeeInfo as a backing bean that provides a Collection of EmployeeBeans. On this page I have a radio button as the first column in the dataTable. This page has a div that is hidden.
When the radio button is switch on and a certain button is clicked, over an AJAX call we need to hit the backing bean to get the details of the EmployeeBean that has been selected as above and populate the div based on this AJAX call.
The reason why I do not want to have a full submit on the first page and get the second page is, because I want to save the state of any changes that have been done on the first page.
Using a tag, you are able to show or hide content quite easily. This is all rough code typed out quickly but in your bean, imagine you had the following:
public class MyBean {
public boolean renderHidden = false;
public void toggleHidden(AjaxBehaviorEvent event) {
renderHidden = !renderHidden;
}
}
Then in your JSF page, you'd have a link to show hide your popup done as a ui:fragment:
<h:commandLink value="Click Me!">
<f:ajax event="click" listener="#{myBean.toggleHidden}" render="hiddenarea" />
</h:commandLink>
<ui:fragment id="hiddenarea" rendered="#{myBean.renderHidden}">
<div><!-- Content to show/hide here --></div>
</ui:fragment>
That ui:fragment could easily be in another JSF page that you include via ui:include if you need it to be. The important bit is that the f:ajax take is what makes the AJAX call (on a click event in this case) and updates the specified element (hiddenarea).

Visualforce link component for rerendering?

Is there a visualforce component for links? I'd like a link () on my page which can trigger an ajax call to one of the functions in the controller and rerender an element on the page.
This is how I'm doing it right now, but I don't want it to be a button, I need a link:
There are two standard apex link components, an apex:outputLink and an apex:commandLink. Both render anchor tags in html. From what you are asking, it sounds like need the command link, but I've posted information about both of them here.
You can find out more about them in the Visualforce Developer's Guide.
The apex:outputLink should be used when you want to create a standard hyperlink:
This component is rendered in HTML as an anchor tag
with an href attribute. Like its HTML equivalent, the body of an
is the text or image that displays as the link. To
add query string parameters to a link, use nested
components.
<apex:outputLink value="https://www.salesforce.com"
id="theLink">www.salesforce.com</apex:outputLink>
The example above renders the following HTML:
<a id="theLink" name="theLink"
href="https://www.salesforce.com">www.salesforce.com</a>
The apex:commandLink is probably what you need.
... executes an action defined by a controller, and then either
refreshes the current page, or navigates to a different page based on
the PageReference variable that is returned by the action. An
apex:commandLink component must always be a child of an apex:form
component.
<apex:commandLink action="{!save}" value="Save" id="theCommandLink"/>
The example above renders the following HTML:
<a id="thePage:theForm:theCommandLink" href="#" onclick="generatedJs()">Save</a>

Resources