开发者

How can I navigate to a different page when user clicks a row?

I want to be able to navigate to a different page when user clicks are row.

Problems

  • No error is thrown I can see the selectionListener getting executed with expected row value but nothing happens.
  • Is this the right way of doing it i.e. using my action listener for this purpose.

Backing bean is this.
Backing bean

package com.howto;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.DataModel;

import org.richfaces.component.UIExtendedDataTable;

@ManagedBean(name = "backingBean")
@SessionScoped
public class BackingBean {
    private List<String> names;
    private DataModel<String> name;
    private String rowValue;
   // private List<String> selection;
    private Collection<String> selection;

    @PostConstruct
    public void init() {
        names = new ArrayList<String>();
        names.add("MyName");
        names.add("YourName");
        names.add("OurName");
        names.add("MamaDidNotGiveOne");
    }

    public void update() {
        System.out.println("Somebody clicked "
                + name);
    }

    public List<String> getNames() {
        return names;
    }

    public void setNames(List<String> names) {
        this.names = names;
    }

    public DataModel<String> getName() {
        return name;
    }

    public void setName(DataModel<String> name) {
        this.name = name;
    }

    public String selectionListener(AjaxBehaviorEvent event) {
        UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
        Object originalKey = dataTable.getRowKey();
      //  selectionIt开发者_如何学Cems.clear();
        for (Object selectionKey : selection) {
            dataTable.setRowKey(selectionKey);
            if (dataTable.isRowAvailable()) {
                //selectionItems.add((InventoryItem) dataTable.getRowData());
                rowValue =  (String) dataTable.getRowData();
            }
        }        
        dataTable.setRowKey(originalKey);
        return "success";
    }

    public String gotopage() {
        return "success";
    }

    public Collection<String> getSelection() {
        return selection;
    }

    public void setSelection(Collection<String> selection) {
        this.selection = selection;
    }

    public String getRowValue() {
        return rowValue;
    }

    public void setRowValue(String rowValue) {
        this.rowValue = rowValue;
    }

}

Method selectionListenerdoes get hit every time user clicks a row item no error is thrown but nothing happens.

XHTML

        <h:form>
            <rich:extendedDataTable
                value="#{backingBean.names}" var="rowItem"
                selection="#{backingBean.selection}" id="table"
                frozenColumns="2" style="height:300px; width:500px;" selectionMode="single">
                <a4j:ajax event="selectionchange" listener="#{backingBean.selectionListener}" />                    
                <rich:column>
                    <f:facet name="header">
                        <h:outputText value="vendor" />
                    </f:facet>
                    <h:outputText value="#{rowItem}" />
                </rich:column>
            </rich:extendedDataTable>
        </h:form>

Navigation Rule

<navigation-rule>
        <from-view-id>*</from-view-id>
         <navigation-case>
            <from-action>#{backingBean.selectionListener}</from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>/pages/dummyfolder/results.xhtml</to-view-id>
        </navigation-case>
</navigation-rule>

Using Richfaces 4 could not find a tag with version.


You can't navigate by returning String from an action listener method. You can only do this in real action methods. You need to set the method return type back to void and take the navigation in your own hands with help of NavigationHandler#handleNavigation().

public void selectionListener(AjaxBehaviorEvent event) {
    // ...

    FacesContext context = FacesContext.getCurrentInstance();
    context.getApplication().getNavigationHandler().handleNavigation(context, "#{backingBean.selectionListener}", "success");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜