How Could i Sort the Recodrs Based upon the User Id in h:datatable in JSF

How Could i Sort the Recodrs Based upon the User Id in h:datatable in JSF
LVL 9
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sathish David Kumar NArchitectCommented:
Try this
public void sort(ActionEvent actionEvent) {
        sortColumn = (ColumnType) ColumnType.getEnumManager().getInstance(
            actionEvent.getComponent().getId());
        logger.debug("Attempting to sort by " + sortColumn);

        Comparator c = null;

        // Sort method will depend upon the column type
        switch (sortColumn.getValue()) {
        case ApplicationConstants.USER_ID_COLUMN:
            c = new BeanPropertyComparator(ApplicationConstants.ID);
            Collections.sort((List) this.getSearchResults(), c);
            break;
        case ApplicationConstants.LAST_NAME_COLUMN:
            c = new BeanPropertyComparator(ApplicationConstants.LAST_NAME);
            Collections.sort((List) this.getSearchResults(), c);
            break;

        case ApplicationConstants.FIRST_NAME_COLUMN:
            c = new BeanPropertyComparator(ApplicationConstants.FIRST_NAME);
            Collections.sort((List) this.getSearchResults(), c);
            break;
        default:
            break;
        }

        /
          Update the object stored in the session so the sort order will be
          retained between requests.
         /
        facesContext.getApplication().createValueBinding(
            "#{sessionScope.searchResults}").setValue(facesContext,
            this.getSearchResults());
    }


The search results are initialized from a previous search page and its backing bean. The results are passed as a managed property to the bean containing the sort method.

Some snippets of my edit page that calls the sorting method is below:



<h:dataTable id="userTable" styleClass="table-background"
				rowClasses="table-odd-row,table-even-row" cellpadding="5"
				value="#{pc_Search_user_results.searchResults}" var="searchResult"
				binding="#{pc_Search_user_results.userTable}">

				<h:column>
					<f:facet name="header">
						<h:commandLink styleClass="table-header" id="userId"
							actionListener="#{pc_Search_user_results.sort}">
							<h:outputText value="User ID" />
						</h:commandLink>
					</f:facet>
					<h:outputText value="#{searchResult.id}" />
				</h:column>

<h:column>
					<h:commandLink id="editLink" action="#{pc_Search_user_results.editUser}">
						<h:outputText value="Edit" />
					</h:commandLink>
				</h:column>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Manikandan ThiagarajanSenior consultantAuthor Commented:
without clicking the button i want to sort the records
0
Sathish David Kumar NArchitectCommented:
then you sort in query itself????
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Manikandan ThiagarajanSenior consultantAuthor Commented:
i didnt understand your program

when i click particular command link in header based on that it would sort that

could you please give explanation on one column and code
0
prakash_parvathCommented:
you should use richfaces :

and have something similar to this one below
<rich:dataTable id="table" var="record"
            value="#{tableBean.values}" rows="10" width="100%">
                   <rich:columnGroup>
            <rich:column sortBy="record.value1">
                 <f:facet name="header">
                     <h:outputText value="Column1" />
                 </f:facet>
             <h:outputText value="record.value1"/>
            </rich:column>
            <rich:column sortBy="record.value2">
                  <f:facet name="header">
                      <h:outputText value="Column2" />
             <h:outputText value="record.value2"
             </f:facet>
</rich:dataTable>

Open in new window

0
prakash_parvathCommented:
and refer this url for complete set of components:

http://livedemo.exadel.com/richfaces-demo/index.jsp
0
Manikandan ThiagarajanSenior consultantAuthor Commented:
could not able to reward points
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.