Could you please give me the sorting in hdatatable

Could you please give me the sorting in hdatatable

could you please give me the JSF Code with Explanation for one column

please give that
LVL 9
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

mbonaciCommented:
You should sort the collection container that holds the data your h:datatable is binded to.
Here's an example:

http://balusc.blogspot.com/2006/06/using-datatables.html#SortingDatatable
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
sandeshjCommented:
0
Manikandan ThiagarajanSenior consultantAuthor Commented:
could you please give me the simple exampleto use h:datatable and Explain

i didnt understand from the link
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

mbonaciCommented:
What's wrong with the example from balusc's post?
That couldn't be simpler.

Your data table contains the list of objects.
How can the program know how to compare your objects?
You have to define the rule in which you say when one instance of your class is greater then the other.

In order to sort objects you have to:
 - create YourClassNameComparator class
 - that class implements Comparator
 - which forces you to implement compareTo method
 - which compares two instances of YourClass

Then, when you finish that, it's trivial, you just have to call static method sort of the Collections class:

    static void sort(List list, Comparator c)

In the code you have to:

YourClassNameComparator yc = new YourClassNameComparator();
Collections.sort(listThatHoldsYourObjects, yc)
0
Manikandan ThiagarajanSenior consultantAuthor Commented:
could you please correct my program in the JSF file




<%@ page contentType="text/html"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<f:view>
	<html>
	<head>
	<title>Site Administration Dashboard</title>
	</head>
	<body>
	<script type="text/javascript"> 
 		
        
        function radioButton(radio,name,value,keyId) {
                
         document.getElementById("myform:name").value = name; 
         document.getElementById("myform:value").value = value;
         document.getElementById("myform:keyId").value =keyId;

         var id = radio.name.substring(radio.name.lastIndexOf(':'));
         var el = radio.form.elements;
         for (var i = 0; i < el.length; i++) {
         	if (el[i].name.substring(el[i].name.lastIndexOf(':')) == id) {
         		el[i].checked = false;
         	}
         }
         radio.checked = true;
       }
        
	</script>

	<h:form id="myform">
		<h:selectOneMenu id="environment"
			value="#{propertyLoad.propertyLoadVO.environment}"
			onchange="this.form.submit()"
			valueChangeListener="#{propertyLoad.getProperty}">
			<f:selectItem id="DevServer" itemLabel="DevServer"
				itemValue="DevServer" />
			<f:selectItem id="ProdServer" itemLabel="ProdServer"
				itemValue="ProdServer" />
			<f:selectItem id="UAT" itemLabel="UAT" itemValue="UAT" />
		</h:selectOneMenu>


		<h:commandButton value="Reload" onclick="getValues()" />
		<table>
			<tr>
				<td>PropertyName:</td>

				<td><h:inputText id="name"
					value="#{propertyLoad.propertyLoadVO.name}">
				</h:inputText></td>

			</tr>
			<tr>
				<td>PropertyValue:</td>

				<td><h:inputText id="value"
					value="#{propertyLoad.propertyLoadVO.value}">
				</h:inputText></td>

			</tr>

			<tr>
				<td><h:inputText id="keyId"
					value="#{propertyLoad.propertyLoadVO.keyId}" style="display:none">
				</h:inputText></td>

			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><h:commandButton value="Add" action="#{propertyLoad.add}" /></td>

				<td><h:commandButton value="Save" action="#{propertyLoad.save}" /></td>

			</tr>
		</table>

		<h:dataTable id="dt1" value="#{propertyLoad.propertyShowDataTable}"
			var="item" bgcolor="#F1F1F1" border="10" cellpadding="5"
			cellspacing="3" width="50%">
			<f:facet name="header">
				<h:outputText value="CWS-Dashboard administration" />
			</f:facet>

			<h:column>
				<f:facet name="header">
					<h:outputText styleClass="outputText" value="Select" id="text1"></h:outputText>
				</f:facet>

				<h:selectOneRadio id="radio" layout="pageDirection" onclick="radioButton(this,'#{item.name}','#{item.value}','#{item.keyId}');">
					<f:selectItem id="selectRadio" itemLabel="#{item.name}"
						itemValue="#{item.value}" />
				</h:selectOneRadio>
			</h:column>

			<h:column>
				<f:facet name="header">
				<h:commandLink styleClass="table-header" id="userId"
                                                        actionListener="#{propertyLoad.sort}">
                                                        <f:attribute name="sortField" value="getId" />
                                                        <h:outputText value="Name" />
                                                </h:commandLink>
                  
                                                
					
				</f:facet>
				<h:outputText value="#{item.name}" />
			</h:column>

			<h:column>
				<f:facet name="header">
					<h:outputText value="Value" />
				</f:facet>
				<h:outputText value="#{item.value}" />
			</h:column>

			<h:column>
				<f:facet name="header">
					<h:outputText value="KeyID" style="display:none" />
				</f:facet>
				<h:outputText value="#{item.keyId}" style="display:none" />
			</h:column>

			<f:facet name="footer">
				<h:outputText value="The End" />
			</f:facet>
		</h:dataTable>
		<br>
 <h:inputHidden value="#{propertyLoad.sortField}" />
    <h:inputHidden value="#{propertyLoad.sortAscending}" />

	</h:form>
	</body>
	</html>
</f:view>

Open in new window

0
Manikandan ThiagarajanSenior consultantAuthor Commented:
that is my java file

sorting is not working

when i click command link in name that sorting is not working
ackage com.citi.cpb.cws.dashboard.vo;

import java.util.ArrayList;
import javax.faces.component.html.HtmlDataTable;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import com.citi.cpb.cws.dashboard.service.ISiteAdminDashboardService;
import com.citi.cpb.cws.dashboard.service.impl.SiteAdminDashboardServiceImpl;
import com.citi.cpb.cws.workwithclient.vo.DTOComparator;

import javax.faces.event.AbortProcessingException;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class PropertyLoad {
	 private HtmlDataTable dataTable;

	private PropertyLoadVO propertyLoadVO = new PropertyLoadVO();

	private ISiteAdminDashboardService getService;

	private List<PropertyLoadVO> propertyLoadVOlist = new ArrayList<PropertyLoadVO>();

	private List<PropertyLoadVO> propertyShowDataTable = new ArrayList<PropertyLoadVO>();

	private Iterator iterator;

	private SiteAdminDashboardRequest siteAdminRequest = new SiteAdminDashboardRequest();

	private SiteAdminDashboardResponse siteAdminResponse = new SiteAdminDashboardResponse();

	private SiteAdminDashboardResponse serviceresponse;

	private ISiteAdminDashboardService siteAdminService;

	public PropertyLoadVO getPropertyLoadVO() {
		return propertyLoadVO;
	}

	public void setPropertyLoadVO(PropertyLoadVO propertyLoadVO) {
		this.propertyLoadVO = propertyLoadVO;
	}
    public String next()
    {
    	return "success";
    }
    public void pageFirst() {
        dataTable.setFirst(0);
        
    }

    public void pagePrevious() {
        dataTable.setFirst(dataTable.getFirst() - dataTable.getRows());
    }

    public void pageNext() {
    	System.out.println(dataTable.getRows());
        dataTable.setFirst(dataTable.getFirst() + dataTable.getRows());
    }

    public void pageLast() {
        int count = dataTable.getRowCount();
        int rows = dataTable.getRows();
        dataTable.setFirst(count - ((count % rows != 0) ? count % rows : rows));
    }

	public String add() {

		siteAdminRequest.setPropertyLoadVO(getPropertyLoadVO());
		siteAdminService = (SiteAdminDashboardServiceImpl) getConnection();
		siteAdminService.insertProperty(siteAdminRequest);
		siteAdminResponse = siteAdminService.getProperty(siteAdminRequest,
				siteAdminResponse);
		propertyLoadVOlist = null;
		propertyShowDataTable = null;
		propertyShowDataTable = new ArrayList<PropertyLoadVO>();
		propertyLoadVOlist = siteAdminResponse.getPropertyLoadVOList();
		iterator = propertyLoadVOlist.iterator();
		while (iterator.hasNext()) {
			propertyShowDataTable.add((PropertyLoadVO) iterator.next());
		}

		return "success";
	}

	public String save() {

		SiteAdminDashboardRequest siteAdminRequest = new SiteAdminDashboardRequest();
		SiteAdminDashboardResponse siteAdminResponse = new SiteAdminDashboardResponse();
		
		siteAdminService = (SiteAdminDashboardServiceImpl) getConnection();
		
		siteAdminRequest.setPropertyLoadVO(getPropertyLoadVO());
		siteAdminService.modifyProperty(siteAdminRequest);
		siteAdminResponse = siteAdminService.getProperty(siteAdminRequest,
				siteAdminResponse);
		propertyLoadVOlist = null;
		propertyShowDataTable = null;
		propertyShowDataTable = new ArrayList<PropertyLoadVO>();
		propertyLoadVOlist = siteAdminResponse.getPropertyLoadVOList();
		iterator = propertyLoadVOlist.iterator();
		while (iterator.hasNext()) {
			propertyShowDataTable.add((PropertyLoadVO) iterator.next());
		}
		return "success";
	}

	public void getLoadShowDataTableProperty() {
		SiteAdminDashboardRequest request = new SiteAdminDashboardRequest();
		SiteAdminDashboardResponse response = new SiteAdminDashboardResponse();
		request.setPropertyLoadVO(propertyLoadVO);
		getService = new SiteAdminDashboardServiceImpl();
		serviceresponse = getService.getProperty(request, response);
		propertyLoadVOlist = null;
		propertyLoadVOlist = serviceresponse.getPropertyLoadVOList();
		iterator = propertyLoadVOlist.iterator();
		while (iterator.hasNext()) {
			propertyShowDataTable.add((PropertyLoadVO) iterator.next());
		}

	}
	 private String sortField = null;
	    private boolean sortAscending = true;


	    public void sort(ActionEvent actionEvent) {
	    	
	    	

	    	System.out.println("Enter in to Sort Property Load");
	    	
	    	String sortFieldAttribute = getAttribute(actionEvent,"sortField");
	    	System.out.println(sortFieldAttribute);
	    	

	        // Get and set sort field and sort order.
	        if (sortField != null && sortField.equals(sortFieldAttribute)) {
	            sortAscending = !sortAscending;
	        } else {
	            sortField = sortFieldAttribute;
	            sortAscending = true;
	        }

	        // Sort results.
	        System.out.println("enter into sort");
	        System.out.println(sortField);
	        if (sortField != null) {
	            Collections.sort(propertyShowDataTable,new DTOComparator(sortField,sortAscending));
	        }
	    }
	    private static String getAttribute(ActionEvent event, String name) {
	        return (String) event.getComponent().getAttributes().get(name);
	    }
	    public String getSortField() {
	        return sortField;
	    }

	    public boolean getSortAscending() {
	        return sortAscending;
	    }

	    // Setters -----------------------------------------------------------------------------------

	    public void setSortField(String sortField) {
	        this.sortField = sortField;
	    }

	    public void setSortAscending(boolean sortAscending) {
	        this.sortAscending = sortAscending;
	    }



	public List<PropertyLoadVO> getProperty(ValueChangeEvent vce)
			throws AbortProcessingException {
		String env = (String) vce.getNewValue();
		SiteAdminDashboardRequest siteAdminRequest = new SiteAdminDashboardRequest();
		SiteAdminDashboardResponse siteAdminResponse = new SiteAdminDashboardResponse();
		PropertyLoadVO reqPropVO = new PropertyLoadVO();
		reqPropVO.setEnvironment(env);
		siteAdminRequest.setPropertyLoadVO(reqPropVO);		
		siteAdminService = (SiteAdminDashboardServiceImpl) getConnection();		
		siteAdminResponse = siteAdminService.getProperty(siteAdminRequest,
				siteAdminResponse);
		propertyLoadVOlist = siteAdminResponse.getPropertyLoadVOList();
		iterator = propertyLoadVOlist.iterator();
		while (iterator.hasNext()) {

			propertyShowDataTable.add((PropertyLoadVO) iterator.next());
		}

		return propertyShowDataTable;
	}

	public List<PropertyLoadVO> getPropertyShowDataTable() {
		return propertyShowDataTable;
	}

	public void setProperty(List<PropertyLoadVO> propertyShowDataTable) {
		this.propertyShowDataTable = propertyShowDataTable;
	}

	public SiteAdminDashboardServiceImpl getConnection() {

		Resource resource = new ClassPathResource("/ibatis/spring-ibatis.xml");
		BeanFactory beanFactory = new XmlBeanFactory(resource);
		siteAdminService = (SiteAdminDashboardServiceImpl) beanFactory
				.getBean("siteAdminDashboardService");
		return (SiteAdminDashboardServiceImpl) siteAdminService;
	}

	public HtmlDataTable getDataTable() {
		return dataTable;
	}

	public void setDataTable(HtmlDataTable dataTable) {
		this.dataTable = dataTable;
	}

Open in new window

0
mbonaciCommented:
What's wrong with it?
Where's your bean code?
0
mbonaciCommented:
OK, I see, can you try and set the bean scope in faces-config.xml to session and then try it?
0
Manikandan ThiagarajanSenior consultantAuthor Commented:
when i set session it would not sort
0
darkappleCommented:
why don't you once check out rich:dataTable. It has all the sorting features and also filtering records.
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.