0

I create a dataTable component, and I want to add in header a button for hide columns, but the button doesn't fire the event.

This is the encode Method for header. I'm in the function rendering header and call this one when a header has to write the button for hide himself.

private void encodeHideColumnOption(FacesContext context,
                   UIComponent table, ResponseWriter writer) throws IOException {

    HtmlCommandButton hide = (HtmlCommandButton) context
            .getApplication()
            .createComponent(HtmlCommandButton.COMPONENT_TYPE);
    hide.setId("hb_" + model.getColumnIndex());
    hide.setValue("X");
    AjaxBehavior ajax = new AjaxBehavior();
    ajax.setRender(Arrays.asList(":" + table.getParent().getClientId(context),
                                 ":messagePanel"));
    ajax.setExecute(Arrays.asList(":form"));
    hide.addClientBehavior(hide.getDefaultEventName(), ajax);
    hide.addActionListener(new ActionListenerHideOption());
    table.getChildren().add(hide);
    hide.encodeAll(context);
}

This is the ActionListener Class

@SessionScoped
public class ActionListenerHideOption implements ActionListener, Serializable {

    public ActionListenerHideOption(){
    }

    @Override
    public void processAction(ActionEvent event)
                              throws AbortProcessingException {
        System.out.println("Action Listener Fired :D");
    }

}

But when I click in the button nothing happen, I verify with this question : h:commandLink / h:commandButton is not being invoked but doesnt work. Can you help me?

---- Edited ----

I'm trying different methods, but I really don't understand why I use this way and works

 <h:commandButton id="hb_x" value="&empty;">
      <f:actionListener type="package.ActionListenerHideOption" />                    
      <f:ajax render=":form:dataTable" onevent="tableReset" />
 </h:commandButton>

But the other way doesn't... Can anyOne help me?

Community
  • 1
  • 1

1 Answers1

0

This is the solution. In render of the dataTable component (my dataTable component) I have to add this function to decode the component (dataTable)

@Override
public void decode(FacesContext context, UIComponent component) {
    for (UIComponent child : component.getChildren()) {
            for (UIComponent grandChild : child.getChildren()) {
                if (grandChild instanceof HtmlCommandButton) {
                    grandChild.decode(context);
                }
            }
    }
}

This allow to fire the decode process in all the HtmlCommandButton inside my dataTable... I think it will works with HtmlCommandLink but is not tested. Could be better code, if anyOne can do a better code in performance and other things, is welcome :)

---- Edited ----

I do a better solution, it decodes all on my dataTable.

@Override
public void decode(FacesContext context, UIComponent component) {
    decodeChildren(context, component);
}

public void decodeChildren(FacesContext context, UIComponent component) {
    for (UIComponent child : component.getChildren()) {
        if (!child.getChildren().isEmpty()) {
            decodeChildren(context, child);
        }else{
            child.decode(context);
        }
    }
}

And that's it. =)