How tos set active index of tabview to zero only after calling a tabview change listener - jsf-2

<p:tabView widgetVar="addSystemWizard" style="width:980px;height:400px;" dynamic="true" activeIndex="#{testBean.messagesTab.activeIndex}" >
<p:ajax event="tabChange" listener="#{testBean.onTabChange}" update=":tabview:sysMsg"/>
<p:tab id="generalInfo" title="1. General Information"> `enter code here`
private TabView messagesTab;
public TabView getMessagesTab () {
if(messagesTab==null){
messagesTab= new TabView();
}
return messagesTab;
}
public void setMessagesTab(TabView messagesTab ) {
this.messagesTab = messagesTab;
}
public void onTabChange(TabChangeEvent event){
if(test.getName()==null || test.getName().equals("")){
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, event.getTab().getId() ,"please enter all required fields");
FacesContext.getCurrentInstance().addMessage(null, msg);
int idx=0;
messagesTab= (TabView) event.getComponent();
messagesTab.setActiveIndex(idx);
}
}
I am calling a tabchange event on tabview, I have name attribute im my page,if that attribute is blank or null I have to stay on same tab, otherwise I ahve to go next tab, basically performing validation.. I have posted the above code to do that, but its not working!

I found this blog post that describes how to use a p:remoteCommand and javascript to accomplish this.
Trying various approaches to the question above (with p:ajax event="tabChange" and so on), I came up with a clean and well working solution. The code below turns on the PrimeFaces TabView into a "wizard mode". That means, the user stays on the same tab when he / she is trying to switch tabs and has validation errors within the current tab. The main idea is to use p:remoteCommand for a deferred decision about tab switching. If no validation errors were present, the switching occurs by the TabView widget's method select.
XHTML:
<p:tabView ... widgetVar="tv_widget" styleClass="tv_selector">
...
</p:tabView>
<p:remoteCommand name="tv_remoteCommand" process="#(.tv_selector)" update="#(.tv_selector)" global="false"
oncomplete="handleTabChangeTV(xhr, status, args, tv_widget)"/>
<h:panelGroup id="tv_script" styleClass="tv_selector">
<script type="text/javascript">
$(function(){
bindOnClickTV(tv_remoteCommand, tv_widget);
});
</script>
</h:panelGroup>
Javascript:
function bindOnClickTV(rc, widget) {
// unbind PrimeFaces click handler and bind our own
widget.jq.find('.ui-tabs-nav > li').unbind('click.tabview').
off('click.custom-tabview').on('click.custom-tabview', function (e) {
var element = $(this);
if ($(e.target).is(':not(.ui-icon-close)')) {
var index = element.index();
if (!element.hasClass('ui-state-disabled') && index != widget.cfg.selected) {
// buffer clicked tab
widget.clickedTab = element.index();
// call remote command
rc();
}
}
e.preventDefault();
});
}
function handleTabChangeTV(xhr, status, args, widget) {
if (!args.validationFailed) {
widget.select(widget.clickedTab);
}
}

Related

localization(i18n) in sapui5 for fragment.xml file in not appearing

I have a button (create Application) if i click on a button a fragmented dialog will be appearing . here am able to show fragmented dialog .but internalization(i18n) is not appearing for the fields. (For xml files able to show i18n but for fragment.xml file not able to show i18n/)
component.js:
createContent : function() {
// create root view
var oView = sap.ui.view({
id : "app",
viewName : "sap.gss.program.view.App",
type : "JS",
viewData : { component : this }
});
var i18nModel = new sap.ui.model.resource.ResourceModel({
bundleUrl : "i18n/appTexts_fr.properties"
});
oView.setModel(i18nModel, "i18n");
return oView;
}
Controller.js:
createApplication: function (oEvent) {
if (!this.oDialogFragment) {
this.oDialogFragment = sap.ui.xmlfragment("sap.gss.program.view.myFragment",
this);
}
this.oDialogFragment.open();
}
fragment.xml:
<core:FragmentDefinition
xmlns="sap.m"
xmlns:core="sap.ui.core"
xmlns:app="http://schemas.sap.com/sapui5/extension/sap.ui.core.CustomData/1">
<Dialog
title="{i18n>Title}"
class="sapUiPopupWithPadding" >
<HBox>
<Text text="{i18n>Description_TEXT}" > </Text>
</HBox>
<beginButton>
<Button text="{i18n>Ok}" press="DialogButton" />
</beginButton>
<endButton>
<Button text="{i18n>Cancel}" press="CloseButton" />
</endButton>
</Dialog>
</core:FragmentDefinition>
You can use the dependents aggregation, to connect up the dialog to the view; you don't need to set any models explicitly.
So in your case you would do this:
createApplication: function (oEvent) {
if (!this.oDialogFragment) {
this.oDialogFragment = sap.ui.xmlfragment("sap.gss.program.view.myFragment", this);
}
this.getView().addDependent(oDialogFragment); // <--
this.oDialogFragment.open();
}
See my answer to 'What is the usage of "dependents" aggregation in SAPUI5?' for more details.
you should set the i18n resource model for the dialog fragment as well.
createApplication: function(oEvent) {
if (!this.oDialogFragment) {
this.oDialogFragment = sap.ui.xmlfragment("sap.gss.program.view.myFragment", this);
var i18nModel = new sap.ui.model.resource.ResourceModel({
bundleUrl : "i18n/appTexts_fr.properties"
});
this.oDialogFragment.setModel(i18nModel, "i18n");
}
this.oDialogFragment.open();
}
Its often the easiest way esp. for a ResourceModel to just set it globally:
sap.ui.getCore().setModel(i18nModel, "i18n");
Now you can reference it from everywhere in your application and bind to it like you did, no need to ever set it again on view- or even control-level.
I had same problem, so setup model in Component globally and locally. It is working correctly.
sap.ui.getCore().setModel(i18nModel, "i18n");
this.setModel(i18nModel, "i18n");

Redirect to new page in JSF 2.0 by clicking on series of bar chart

I am new to JSF.I am building a xhtml page in which i am displaying a Bar chart.
I wanted to redirect it to new xhtml page using navigation rules when i click on one of the bar of Bar Chart.
Code Sample :
Bar Chart Code:
<p:layoutUnit position="south" size="350" resizable="true"
closable="true" collapsible="true">
<p:growl id="barChartGrowl" showDetail="true" />
<p:barChart orientation="vertical" id="tkChart"
value="#{chartCreateService.categoryChartModel}"
legendPosition="ne" title="Human Capital Model Analysis" minY="0"
maxY="100" yaxisLabel="Score" style="height:300px;width:700px"
barMargin="50" animate="true">
<p:ajax event="itemSelect" listener="#{userInput.barChartClicked}"
update="barChartGrowl" />
</p:barChart>
</p:layoutUnit>
Bean :
public String barChartClicked(ItemSelectEvent event) {
try {
System.out.println("In Bar Chart Clicked...");
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
"Item selected", "Item Index: " + event.getItemIndex() + ", Series Index:" + event.getSeriesIndex());
FacesContext.getCurrentInstance().addMessage(null, msg);
int itemIndex = event.getItemIndex();
int seriesIndex = event.getSeriesIndex();
System.out.println("Item Index :"+ itemIndex);
System.out.println("Series Index :"+ seriesIndex);
objSummeryAttributeCharts.displaySelectedGroupResults(seriesIndex,itemIndex,objResults,objChart.getCategoryChartModel());
} catch (Exception e) {
e.printStackTrace();
}
return "showAttributeResults";
}
Faces-config Entry :
navigation-rule>
<from-view-id>/pages/peerGroupAnalysis.jsf</from-view-id>
<navigation-case>
<from-outcome>showAttributeResults</from-outcome>
<to-view-id>/pages/categoryResults.jsf</to-view-id>
</navigation-case>
</navigation-rule>
Every thing is working fine and i am getting item index and series index of the bar on which user clicked in the bean properly..but it is not redirecting to another page.
Is there any other way to redirect to a new page in JSF or m i doing something wrong??
Pleas help...
Thanks..
You don't normally navigate on ajax requests, you normally update the dom.
If you need to navigate, you might need to use a redirect, e.g.
FacesContext ctx = FacesContext.getCurrentInstance();
ExternalContext extContext = ctx.getExternalContext();
String url = extContext.encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/pages/categoryResults.jsf"));
try {
extContext.redirect(url);
} catch (IOException e) {
e.printStackTrace();
}

how to get which tab is clicked when accordion is loaded intially on a page

I have an accordion panel with tabs containing name of students. I select on one of the tab and choose edit. I am calling a tab change listener and getting student object and trying to edit it works fine.
But My problem is when for the first time Accordion loads, even though I select on the tab,and click on edit, I will get not get student object. Its giving me null pointer exception. since Tab change event does not get called for the first time it loads. How do I solve this?
private StudBean formBean;
public StudBean getFormBean() {
if(formBean==null){
formBean= new StudBean();
}
return formBean;
}
public void onTabChange(TabChangeEvent event) {
formBean = (StudBean) event.getData();
}
public String edit(){
formBean = testDelegate.getDetails(formBean.getName(),formBean.getId());
return "/ui/EditStudent?faces-redirect=true";
}
<p:commandButton process="#this" value="edit" action="#{testBean.edit}" >
<p:accordionPanel value="#{testBean.students}" var="stud">
<p:ajax event="tabChange" listener="#{testBean.onTabChange}" immediate="true"/>
<p:tab title="#{stud.name}">

commandbutton not executing action if other attributes are bind to managed bean

i'm really new to jsf.
I need to enable,disable, render or not render some buttons on a page depending on the logged user privileges. The page is bound to a request scoped managed bean , i bind the rendered or disabled attribute of the commandbutton to a "visible" property of the managed bean, and an actionlistener to a managedbean method which has the only purpose to navigate to another page.
If i bind one of the above properties to the visibile property, the navigation method doesn't get called, and the same page gets rerendered with the button disabled or not rendered.
The more urget need is the answer to "how do i disable or hide the button?" of course,
But , since i guess the problem is due to jsf page life cycle and the bean scope, i'd like also to be direct to some tutorial a bit more advanced than the hello world that can be found around.
below are part of the code.
Thank you in advance for any help
the xhtml page
<ui:define name="content">
<p:layoutUnit position="center" header="Dettaglio Pratica" scrollable="true">
<h:form id="formDettaglioPratica">
<!-- i've used javascript to redirect -->
<p:commandButton styleClass="commandButton" value="Modifica pratica" ajax="false"
rendered="#{praticaCtrl.visible}" onclick="navigateToChange();return false;"/>
<p:commandButton id="backButton" styleClass="commandButton" value="Torna alla lista"
onclick="navigateToHome();return false;"></p:commandButton>
<!-- never redirect to page defined in method checkIn -->
<p:commandButton id="checkinButton" styleClass="commandButton" value="Rilascia pratica" ajax="false"
actionListener="#{praticaCtrl.checkIn}" disabled="#{praticaCtrl.visible}"></p:commandButton>
<!-- printing output values -->
<h:inputHidden id="idPratica" value="#{praticaCtrl.idPratica}"></h:inputHidden>
<h:inputHidden id="idBox" value="#{praticaCtrl.box}"></h:inputHidden>
</h:form>
</p:layoutUnit>
</ui:define>
the relevant part of the managed bean
public boolean isVisible() {
return isVisible;
}
public void setVisible(boolean isVisible) {
this.isVisible = isVisible;
}
public void setUser(IUserProvider user) {
try{
if(pratica.getUtenteCheckOut() == null
|| (pratica.getUtenteCheckOut().getMatricola().equalsIgnoreCase(user.getUser().getMatricola()))
)
setVisible(true);
else
setVisible(false);
this.user = user;
}
catch(Exception ex)
{log.error(ex);}
}
public void checkIn(ActionEvent event)
{
try{
log.info(String.format("modifica della pratica %s", idPratica));
ajaxRedirect(String.format("../Box/ListaRichieste.xhtml?box=%s",box));
}
catch(Exception ex)
{
log.error(ex.getMessage(),ex);
}
}
this is the ajaxRedirect method,we don't use the navigation rules neither methods return string
protected void ajaxRedirect(String url) throws IOException {
FacesContext.getCurrentInstance().getExternalContext()
.redirect(url);
}
the answer for the described behaviour can be found at : http://forum.primefaces.org/viewtopic.php?f=3&t=13269&p=39996 ,which refers a BalusC answer on a similar question on stackoverflow : commandButton/commandLink/ajax action/listener method not invoked or input value not updated

ASP.NET MVC two user control

I have two user controls on the page and one of the user control has this text aread.
which is used to add a note and but when they click add note button the page reloads.
I do not want the page to reload ,i was looking for an example which this is done without
postback.
Thanks
i tired doing this using JSON , but it throws the following error
The HTTP verb POST used to access path '/Documents/TestNote/Documents/AddNote' is not allowed.
<script type="text/javascript">
$(document).ready(function() {
$("#btnAddNote").click(function() {
alert("knock knock");
var gnote = getNotes();
//var notes = $("#txtNote").val();
if (gnote == null) {
alert("Note is null");
return;
}
$.post("Documents/AddNote", gnote, function(data) {
var msg = data.Msg;
$("#resultMsg").html(msg);
});
});
});
function getNotes() {
alert("I am in getNotes function");
var notes = $("#txtNote").val();
if (notes == "")
alert("notes is empty");
return (notes == "") ? null : { Note: notes };
}
</script>
</asp:Content>
Something like this would give you the ability to send data to an action do some logic and return a Json result then you can update your View accordingly.
Javascript Function
function AddNote(){
var d = new Date(); // IE hack to prevent caching
$.getJSON('/MyController/MyAction', { data: "hello world", Date: d.getTime() }, function(data) {
alert(data);
// call back update your view here
});
}
MyController Action
public virtual JsonResult MyAction(string data)
{
// do stuff with your data here, which right now my data equals hello world for this example
return Json("ReturnSomeObject");
}
What you want is AJAX update. There will always be a postback (unless you are satisfied with simple Javascript page update that does not save on the server), but it won't be the flashing screen effect any more.

Resources