Board index JavaServer Faces General Optimus help needed : LazyDataModel and sorting issue

Optimus help needed : LazyDataModel and sorting issue

Components, Ajax Framework, Utilities and More.


Posts: 8
I am sorting just the first coloumn with lazydatamodel. When i click sort on first coloumn the table become empty. I want first coloumn to sort

Complete code

package org.primefaces.examples.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.el.ValueExpression;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;

import org.primefaces.component.column.Column;
import org.primefaces.component.datatable.DataTable;
import org.primefaces.examples.domain.Car;
import org.primefaces.model.LazyDataModel;

public class TableBean3 implements Serializable {

private LazyDataModel<Car> lazyModel;

private Car selectedCar;
private final static String[] colors;

private final static String[] manufacturers;

// Populate dynamic datatable.
//private static List<List<String>> dynamicList; // Simulates fake DB.
private static String[] dynamicHeaders; // Optional.
private HtmlPanelGroup dynamicDataTableGroup; // Placeholder.

public void setDynamicDataTableGroup(HtmlPanelGroup dynamicDataTableGroup) {
this.dynamicDataTableGroup = dynamicDataTableGroup;
}


DataTable dynamicDataTable;

public DataTable getDynamicDataTable() {
if (dynamicDataTable == null) {

System.out.println("Inside getDynamicDataTable");
loadDynamicList(); // Preload dynamic list.
populateDynamicDataTable(); // Populate dynamic datatable.
}

return dynamicDataTable;
}

public void setDynamicDataTable(DataTable dynamicDataTable) {
this.dynamicDataTable = dynamicDataTable;
}

static {
colors = new String[10];
colors[0] = "Black";
colors[1] = "White";
colors[2] = "Green";
colors[3] = "Red";
colors[4] = "Blue";
colors[5] = "Orange";
colors[6] = "Silver";
colors[7] = "Yellow";
colors[8] = "Brown";
colors[9] = "Maroon";

manufacturers = new String[10];
manufacturers[0] = "Mercedes";
manufacturers[1] = "BMW";
manufacturers[2] = "Volvo";
manufacturers[3] = "Audi";
manufacturers[4] = "Renault";
manufacturers[5] = "Opel";
manufacturers[6] = "Volkswagen";
manufacturers[7] = "Chrysler";
manufacturers[8] = "Ferrari";
manufacturers[9] = "Ford";
}


public TableBean3() {

lazyModel = new LazyDataModel<Car>() {

/**
* Dummy implementation of loading a certain segment of data.
* In a real application, this method should load data from a datasource
*/
@Override
public List<Car> load(int first, int pageSize, String sortField, boolean sortOrder, Map<String,String> filters) {
System.out.println("*************First "+first);
System.out.println("**********PageSize "+pageSize);

System.out.println("Loading the lazy car data between {0} and {1}"+ new Object[]{first, (first+pageSize)});

List<Car> lazyCars = new ArrayList<Car>();

populateLazyRandomCars(lazyCars, pageSize,first);
lazyModel.setPageSize(pageSize);
return lazyCars;
}
};

/**
* In a real application, this number should be resolved by a projection query
*/
lazyModel.setRowCount(100000000);


loadDynamicList();
populateDynamicDataTable();
lazyModel.setPageSize(10);
}

@PostConstruct
public void init() {
System.out.println("Calling Init");


}

public void setLazyModel(LazyDataModel<Car> lazyModel) {
this.lazyModel = lazyModel;
}

public Car getSelectedCar() {
return selectedCar;
}

public void setSelectedCar(Car selectedCar) {
this.selectedCar = selectedCar;
}



private void populateLazyRandomCars(List<Car> list, int size,int first) {
for(int i = 0 ; i < size ; i++) {
int offset = i + first;

list.add(new Car("Model_"+offset, getRandomYear(), getRandomManufacturer(), getRandomColor()));
}
}

private String getRandomColor() {
return colors[(int) (Math.random() * 10)];
}

private String getRandomManufacturer() {
return manufacturers[(int) (Math.random() * 10)];
}

private int getRandomYear() {
return (int) (Math.random() * 50 + 1960);
}


/**
* Populate dynamic datatable: load the data list.
*/
private void loadDynamicList() {

// Set headers (optional).
dynamicHeaders = new String[] { "model", "year", "manufacturer","color" };

// Set rows. This is a stub example, just do your dynamic thing.
//dynamicList = new ArrayList<List<String>>();
//dynamicList.add(Arrays.asList(new String[] { "Model", "Year", "Manufacturer","Color" }));


//log(dynamicList);
}


/**
* Populate dynamic datatable: populate the dynamic datatable.
*/
/**
*
*/
private void populateDynamicDataTable() {

System.out.println("Creating dynamic datatable");
// Create <p:dataTable value="#{myRequestBean.dynamicList}" var="dynamicItem">.
dynamicDataTable = new DataTable();

dynamicDataTable.setValueExpression("value",
createValueExpression("#{tableBean3.lazyModel}",this.lazyModel.getClass()));

dynamicDataTable.setValueExpression("paginator",
createValueExpression("true",Boolean.class));

dynamicDataTable.setValueExpression("rows",
createValueExpression("10",int.class));

dynamicDataTable.setValueExpression("lazy",
createValueExpression("true",Boolean.class));

//dynamicDataTable.setValueExpression("scrollable",
//createValueExpression("true",Boolean.class));

dynamicDataTable.setValueExpression("dynamic",
createValueExpression("true",Boolean.class));

dynamicDataTable.setValueExpression("paginatorTemplate",
createValueExpression("{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}",String.class));


dynamicDataTable.setValueExpression("rowsPerPageTemplate",
createValueExpression("5,10,15",String.class));

dynamicDataTable.setValueExpression("selection",
createValueExpression("#{tableBean3.selectedCar}",Car.class));

dynamicDataTable.setValueExpression("selectionMode",
createValueExpression("single",String.class));

//onRowSelectComplete
//dynamicDataTable.setValueExpression("onRowSelectComplete",
// createValueExpression("display",String.class));

dynamicDataTable.setValueExpression("onRowSelectUpdate",
createValueExpression("display",String.class));



dynamicDataTable.setVar("car");



// Iterate over columns.
for (int i = 0; i <dynamicHeaders.length; i++) {

// Create <p:column>.
Column column = new Column();
dynamicDataTable.getChildren().add(column);

column.setValueExpression("style",createValueExpression("width:200px", String.class));

if (i == 0) {

System.out.println("************* First coloumn Header car." + dynamicHeaders[i]);
column.setValueExpression("sortBy",createValueExpression("#{car.model}", String.class));
}

// Create <h:outputText value="dynamicHeaders[i]"> for <f:facet name="header"> of column.
HtmlOutputText header = new HtmlOutputText();
header.setValue(dynamicHeaders[i]);
column.setHeader(header);

// Create <h:outputText value="#{dynamicItem[" + i + "]}"> for the body of column.
HtmlOutputText output = new HtmlOutputText();
output.setValueExpression("value",
createValueExpression("#{car."+dynamicHeaders[i]+"}", String.class));
column.getChildren().add(output);
}

// Add the datatable to <h:panelGroup binding="#{myBean.dynamicDataTableGroup}">.
dynamicDataTableGroup = new HtmlPanelGroup();
dynamicDataTableGroup.getChildren().add(dynamicDataTable);

//dynamicDataTable.processEvent(null);
System.out.println("Complted creating dynamic datatable TableBean 3");

}

/**
* Populate dynamic datatable: get the placeholder.
* @return The placeholder.
*/
public HtmlPanelGroup getDynamicDataTableGroup() {
if (dynamicDataTableGroup == null) {
// This will be called once in the first RESTORE VIEW phase.
//System.out.println("Inside getDynamicDataTableGroup");
//loadDynamicList(); // Preload dynamic list.
//populateDynamicDataTable(); // Populate dynamic datatable.
}
return dynamicDataTableGroup;
}

/**
* Simplistic logging system. Logs the name of the calling method and the given object.
* @param object The object to be logged.
*/
private static void log(Object object) {
System.out.println(new Exception().getStackTrace()[1].getMethodName() + ": " + object);
}

/**
* Create ValueExpression object based on the given value expression string and value type.
* This is to be used in UIComponent#setValueExpression().
* @param valueExpression The value expression string, e.g. "#{myBean.someValue}".
* @param valueType The actual value object type, e.g. String.class.
* @return The ValueExpression object to be used in UIComponent#setValueExpression().
*/
private ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createValueExpression(
facesContext.getELContext(), valueExpression, valueType);
}


public LazyDataModel<Car> getLazyModel() {
return lazyModel;
}

}


Posts: 25
Location: Stockholm

Make sure the bean is in view or session scope.
Primefaces 3.0.M3 with JSF 2.1.2 on Tomcat 7


Posts: 8
I have changed scope to view
<managed-bean>
<managed-bean-name>tableBean3</managed-bean-name>
<managed-bean-class>org.primefaces.examples.view.TableBean3</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
</managed-bean>

and changed the context to server

<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>


Posts: 2
same problem here. any clue?


Return to General