Jannis Alexakis
Jannis Alexakis

Reputation: 1319

How to navigate using <p:ajax listener>

I am trying to setup a in a way that another page gets loaded when a user selects a row. The line return Pages.EDIT_ITEM is executed but nothing happens, no exceptions or errors, nothing at all. I can even access Pages.EDIT_ITEM in the browser by typing the URL, but somehow, letting the backing bean open the page doesn´t work.

My datatable:

<p:dataTable id="categoriesTable" value="#{category.items}" var="item" selectionMode="single" rowKey="#{item.id}" >
   <p:commandButton id="toggler" type="button" value="Columns" style="float:right"  />
   <p:ajax event="rowSelect" listener="#{listCategoriesController.onRowDblClick}"  />      
   <p:columnToggler datasource="categoriesTable" trigger="toggler" />
   <p:column headerText="#{msg['addItem.internal_id']}" sortBy="#{item.internalId}" filterBy="#{item.internalId}" filterMatchMode="contains" >                                        
       <h:outputText value="#{item.internalId}" />
   </p:column>
</p:dataTable>

My backing bean:

    package de.rcwgmbh.inventory.controller;

import de.rcwgmbh.inventory.data.CategoryProducer;
import de.rcwgmbh.inventory.data.ItemProducer;
import de.rcwgmbh.inventory.model.Category;
import de.rcwgmbh.inventory.model.Item;
import de.rcwgmbh.inventory.services.ItemService;
import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.TabChangeEvent;
import org.primefaces.event.UnselectEvent;

/**
 *
 * @author jalexakis
 */
@ViewScoped
@Named
public class ListCategoriesController implements Serializable {

    private static final long serialVersionUID = -2347239478324L;  

    @Inject
    private EntityManager entityManager;
    @Inject
    private CategoryProducer categoryProducer;
    @Inject
    private ItemService itemService;
    @Inject
    private ItemProducer itemProducer;



     public List<Item> doListItems(Category category) {
        final List<Item> items = itemService.getItemsList(category);
        category.setItems(items);
        categoryProducer.setSelectedCategory(category);
        return items;
    }

    public String doAddCategory() {
        categoryProducer.prepareAddCategory();
        return Pages.LIST_CATEGORIES;
    }

    public String doEditCategory(Category category) {
        categoryProducer.prepareEditCategory(category);
        return Pages.LIST_CATEGORIES;
    }

    public String doEditItem(){
        itemProducer.prepareEditItem(itemProducer.getSelectedItem());
        return Pages.EDIT_ITEM;
    }


    public void onTabChange(TabChangeEvent event) {
        String categoryName = event.getTab().getTitle();
        Category category = entityManager.find(Category.class, categoryName);
        categoryProducer.setSelectedCategory(category);
        ResourceBundle bundle = ResourceBundle.getBundle("messages", FacesContext.getCurrentInstance().getViewRoot().getLocale());
        String value = bundle.getString("listCategories.category_selected");
        FacesMessage msg = new FacesMessage(value, event.getTab().getTitle());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }       


    public void onRowDblClick(SelectEvent event){
        itemProducer.setSelectedItem((Item)event.getObject());
        doEditItem();
    }

}

Upvotes: 1

Views: 2356

Answers (2)

Gimby
Gimby

Reputation: 5274

Here is an alternative way to do it which goes through the JSF navigation handling API, allowing you to apply explicit navigation or through navigation rules as you see fit. This code was found in the Primefaces forums and quoted here for convenience.

    ConfigurableNavigationHandler configurableNavigationHandler =
    (ConfigurableNavigationHandler) FacesContext.getCurrentInstance()
        .getApplication().getNavigationHandler();

    configurableNavigationHandler.performNavigation("otherpage?faces-redirect=true);

Upvotes: 0

Zhedar
Zhedar

Reputation: 3510

You don't do a redirect in this code.
Ajax listener calls are not the same as actions of CommandButtons, if you return a String just nothing will happen.
If you want to redirect to another page, you can do that like this in your ajax call: FacesContext.getCurrentInstance().getExternalContext().redirect("otherpage.xhtml");.

Upvotes: 5

Related Questions