1 Reply Latest reply on Jul 9, 2012 4:11 PM by Vikash Tiwari

    How to attach actionListner to rich: column header

    Vikash Tiwari Newbie

      Hi, i want to call an action when user clicks on header to sort data table.

       

      <f:view id="userView">

                <a4j:form id="userForm">

                          <rich:dataTable id="userTable" cellpadding="0" cellspacing="0"

                                    width="700" border="0" rows="5" var="user" value="#{office.userList}">

                                    <f:facet name="header">

                                              <h:outputText value="Users" />

                                    </f:facet>

                                    <rich:column id="idcol" sortBy="#{user.id}">

                                              <f:facet name="header">

                                                        <h:outputText id="idHeader" value="ID">

                                                        </h:outputText>

                                              </f:facet>

                                              <h:outputText value="#{user.id}"></h:outputText>

                                    </rich:column>

                                    <rich:column sortBy="#{user.name}">

                                              <f:facet name="header">

                                                        <h:outputText value="Name" />

                                              </f:facet>

                                              <h:outputText value="#{user.name}">

                                              </h:outputText>

                                    </rich:column>

                                    <f:facet name="footer">

                                              <rich:datascroller id="tableScroller" scrollerListener="#{tableBackingBean.myListener}">

                                              </rich:datascroller>

                                    </f:facet>

                          </rich:dataTable>

                </a4j:form>

      </f:view>

       

      If i add a4j:support

      <h:outputText id="idHeader" value="ID">

      <a4j:support event="onclick" actionListener="#{tableBackingBean.sortEvent}"/>

        </h:outputText>

       

      It doesnt work.

      "

        • 1. Re: How to attach actionListner to rich: column header
          Vikash Tiwari Newbie

          I removed sortby attribute and implemented custom sorting.

           

          <f:facet name="header">

                                                            <a4j:commandLink id="idHeader" actionListener="#{officeTableBean.sort}" reRender="userTable">

                                                                      <h:outputText value="ID" />

                                                                      <h:panelGroup layout="block" styleClass="#{officeTableBean.columnOrderingMap['id']}"/>

                                                                      <f:attribute name="sortField" value="id" />

                                                            </a4j:commandLink>

          </f:facet>

           

          I am using reflection to get data model of data table. is there a better way?

           

          /**

                     * Method to get data model of HtmlDataTable using reflection.

                     *

                     * @param dataTable

                     * @return DataModel

                     */

                    @SuppressWarnings("unchecked")

                    private <T> List<T> retrieveRecordList(HtmlDataTable dataTable){

                              List<T> recordList = Collections.emptyList();

                              try{

                                        Method method = dataTable.getClass().getSuperclass().getSuperclass().getDeclaredMethod("getDataModel");

                                        method.setAccessible(true);

                                        DataModel dataModel = (DataModel) method.invoke(dataTable);

                                        recordList = (List<T>) dataModel.getWrappedData();

                              } catch (SecurityException e) {

                                        e.printStackTrace();

                              } catch (NoSuchMethodException e) {

                                        e.printStackTrace();

                              } catch (IllegalArgumentException e) {

                                        e.printStackTrace();

                              } catch (IllegalAccessException e) {

                                        e.printStackTrace();

                              } catch (InvocationTargetException e) {

                                        e.printStackTrace();

                              }

                              return recordList;

                    }