Ola pessoas lindas…é o seguinte, eu sou muito novata nisso e to aprendendo buscando informações, mas as vezes é tenso, então to recorrendo a vocês, desde já agradeço…
Eu estou trazendo informações pelo DataTable Lazy mas o meu FilterBy não funciona eu sei que o erro esta nessa linha:
String fieldValue = String.valueOf(cmv.getClass().getField(filterProperty).get(cmv));
Ele filtra mas por não conseguir passar o Field ele sempre retorna vazio.
Vou postar o meu codigo com comentarios onde estou tendo problemas, se alguem tiver sugestão de melhoria no modo de programação, alguma forma de fazer isso mais simples, fiquem a vontade.
public class CMVLazyList extends LazyDataModel<CMV> {
private List<CMV> cmvCompleto;
// private CMVController cMVController;
public CMVLazyList(List<CMV> cmvVindoDoBean) {
this.cmvCompleto = cmvVindoDoBean;
}
@Override
public CMV getRowData(String rowKey) {
for (CMV cmv : cmvCompleto) {
if (cmv.getCodp().equals(rowKey)) {
return cmv;
}
}
return null;
}
@Override
public Object getRowKey(CMV cmv) {
return cmv.getCodp();
}
@Override
public List<CMV> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
List<CMV> data = new ArrayList<CMV>();
//filtro - Aqui ele traz meus dados completos, so que ele não consegui pegar o fieldValue
for (CMV cmv : cmvCompleto) {
boolean match = true;
for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
try {
String filterProperty = it.next();
String filterValue = filters.get(filterProperty);
String fieldValue = String.valueOf(cmv.getClass().getField(filterProperty).get(cmv));
if (filterValue == null || fieldValue.startsWith(filterValue)) {
match = true;
} else {
match = false;
break; }
} catch (Exception ex) {
System.out.println("Problemas ao Filtrar dados os parametros de conexão! Erro: " + ex.getMessage());
ex.printStackTrace();
match = false;
}
}
if (match) {
data.add(cmv);
}
}
//sort by - Ele não reconheçe esse LazySorte - Alguem sabe se é uma classe que eu deveria criar
if (sortField != null) {
//Collections.sort(data, new LazySorter(sortField, sortOrder));
}
//rowCount
int dataSize = data.size();
this.setRowCount(dataSize);
//paginação - Este Funciona OK
if (dataSize > pageSize) {
try {
return data.subList(first, first + pageSize);
} catch (IndexOutOfBoundsException e) {
return data.subList(first, first + (dataSize % pageSize));
}
} else { return data;
} } }
Meu Bean
@ManagedBean(name = "lazycmvBean")
@ViewScoped
public class LazyCMVBean implements Serializable {
private LazyDataModel<CMV> lazyModel;
private CMV selectedCMV;
private List<CMV> cmvs;
private Date dtInicial;
private Date dtFinal;
public LazyCMVBean() {
}
public void buscaDadosCMV() {
PopularCMVCompleto();
lazyModel = new CMVLazyList(getCmvs());
}
public CMV getSelectedCMV() {
return selectedCMV;
}
public void setSelectedCMV(CMV selectedCMV) {
this.selectedCMV = selectedCMV;
}
public LazyDataModel<CMV> getLazyModel() {
return lazyModel;
}
private void PopularCMVCompleto() {
try {
CMVController cMVController = new CMVController();
this.cmvs = cMVController.listarCMVCompleto(dtInicial, dtFinal);
} catch (Exception ex) {
ex.getMessage();
ex.printStackTrace();
}
}
//Getters e Setters ...
Minha Pagina JSF
<h:form id="formU" style="font-size: 14px;">
<p:growl id="messages" showDetail="true"/>
<p:panel id="panel" header="Periodo">
Data Inicial:<p:calendar value="#{lazycmvBean.dtInicial}" id="cal" showButtonPanel="true" pattern="dd/MM/yyyy"/>
Data Final:<p:calendar value="#{lazycmvBean.dtFinal}" id="cal1" showButtonPanel="true" pattern="dd/MM/yyyy"/>
<p:commandButton value="Consultar" action="#{lazycmvBean.buscaDadosCMV()}" update="@this,:formU" ></p:commandButton>
</p:panel>
<p:dataTable rendered="#{lazycmvBean.cmvs != null}" var="cmv" value="#{lazycmvBean.lazyModel}" paginator="true" rows="20"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15,20" selectionMode="single" id="cmvTable" lazy="true" emptyMessage="Dados não encontrados">
<p:column headerText="Apelido" sortBy="#{cmv.apelido}" filterBy="#{cmv.apelido}">
<h:outputText value="#{cmv.apelido}" />
</p:column>
<p:column headerText="Vlr Custo" sortBy="#{cmv.vlCusto}" filterBy="#{cmv.vlCusto}">
<h:outputText value="#{cmv.vlCusto}" >
<f:convertNumber pattern="#,##0.00"></f:convertNumber>
</h:outputText>
</p:column>
</p:dataTable>
</h:form>
Muito Obrigado Novamente!