Pessoal, tenho um paginador com JSF utilizando a classe Render, mas percebi que não funciona corretamente. O cenário é o seguinte:
Tenho um Data Table com 10 itens mas preciso exibir de 3 em 3, ele exibe da na ordem de 0,1,2 na primeira tela, 3,4,5 na segunda tela, 6,7,8 na terceira tela e na quarta tela apresenta o erro 7,8,9. Resumindo ele completa os itens 7,8 para ficar formato de 3 em 3 ao invés de deixar apenas 1 item na ultima tabela.
Alguem ja passou por isso?
Segue minha classe:
Obs: Esta classe veio do livro Core JavaServerFaces Segunda Edição Autor: David Geary e Cay Horstman da Sun.
public class PaginadorRenderer extends Renderer {
/** {@inheritDoc} */
public void encodeBegin(
FacesContext contexto, UIComponent componente) throws IOException {
String id = componente.getClientId(contexto);
UIComponent parent = componente;
while (!(parent instanceof UIForm)) {
parent = parent.getParent();
}
String idForm = parent.getClientId(contexto);
ResponseWriter writer = contexto.getResponseWriter();
String styleClass = (String) componente.getAttributes().get(
"styleClass");
String selectedStyleClass = (String) componente.getAttributes().get(
"selectedStyleClass");
String idTabela = (String) componente.getAttributes().get("idTabela");
Integer a = (Integer) componente.getAttributes().get("numPaginas");
int numPaginas = a == null ? 0 : a.intValue();
String onclick = (String) componente.getAttributes().get("onclick");
UIData data = (UIData) componente.findComponent(idTabela);
int first = data.getFirst();
int itemcount = data.getRowCount();
int pagesize = data.getRows();
if (pagesize <= 0) {
pagesize = itemcount;
}
int qtdePaginas = itemcount / pagesize;
if (itemcount % pagesize != 0) {
qtdePaginas = qtdePaginas + 1;
}
if (qtdePaginas == 1) {
return;
}
int paginaAtual = first / pagesize;
if (first >= itemcount - pagesize) {
paginaAtual = qtdePaginas - 1;
}
int startPage = 0;
int endPage = qtdePaginas;
if (numPaginas > 0) {
startPage = (paginaAtual / numPaginas) * numPaginas;
endPage = Math.min(startPage + numPaginas, qtdePaginas);
}
if (paginaAtual > 0) {
adicionarLink(writer, componente, idForm, id, "<", styleClass,
onclick);
}
if (startPage > 0) {
adicionarLink(writer, componente, idForm, id, "<<", styleClass,
onclick);
}
for (int i = startPage; i < endPage; i = i + 1) {
adicionarLink(writer, componente, idForm, id, ""
+ (i + 1), i == paginaAtual ? selectedStyleClass : styleClass,
onclick);
}
if (endPage < qtdePaginas) {
adicionarLink(writer, componente, idForm, id, ">>", styleClass,
onclick);
}
if (first < itemcount - pagesize) {
adicionarLink(writer, componente, idForm, id, ">", styleClass,
onclick);
}
// campo hidden para guardar o resultado
adicionarCampoHidden(writer, componente, id);
}
/** {@inheritDoc} */
public void decode(
FacesContext contexto, UIComponent componente) {
String id = componente.getClientId(contexto);
Map<String, String> parameters = contexto.getExternalContext()
.getRequestParameterMap();
String response = (String) parameters.get(id);
if (response == null || response.equals("")) {
return;
}
String idTabela = (String) componente.getAttributes().get("idTabela");
Integer a = (Integer) componente.getAttributes().get("numPaginas");
int numPaginas = a == null ? 0 : a.intValue();
UIData data = (UIData) componente.findComponent(idTabela);
int first = data.getFirst();
int itemcount = data.getRowCount();
int pagesize = data.getRows();
if (pagesize <= 0) {
pagesize = itemcount;
}
if (response.equals("<")) {
first -= pagesize;
} else if (response.equals(">")) {
first += pagesize;
} else if (response.equals("<<")) {
first -= pagesize * numPaginas;
} else if (response.equals(">>")) {
first += pagesize * numPaginas;
} else {
int page = Integer.parseInt(response);
first = (page - 1) * pagesize;
}
if (first + pagesize > itemcount) {
first = itemcount - pagesize;
}
if (first < 0) {
first = 0;
}
data.setFirst(first);
}
/**
* Adiciona um link no componente.
*
* @param writer
* a entidade que vai escrever o link
* @param componente
* o componente a ser adicionado o link
* @param idForm
* o id do formulário
* @param id
* o id do link
* @param valor
* o valor do link
* @param styleClass
* o estilo do link
* @param onclickAdicional
* codigo javascript definido pelo utilizador do componente
* @throws IOException
* caso não consiga escrever no writer
*/
private void adicionarLink(ResponseWriter writer, UIComponent componente,
String idForm, String id, String valor, String styleClass,
String onclickAdicional) throws IOException {
writer.writeText(" ", null);
writer.startElement("a", componente);
writer.writeAttribute("href", "#", null);
writer.writeAttribute("onclick", onclickCodigo(idForm, id, valor,
onclickAdicional), null);
if (styleClass != null) {
writer.writeAttribute("class", styleClass, "styleClass");
}
writer.writeText(valor, null);
writer.endElement("a");
}
/**
* Escreve o código do atributo onclick da tag anchor do paginador.
*
* @param idForm
* o id do formulário
* @param id
* o id do link
* @param valor
* o valor do link
* @param onclickAdicional
* código javascript definido pelo utilizador do componente
* @return o código gerado do atributo onclick da tag anchor do paginador
*/
private String onclickCodigo(String idForm, String id, String valor,
String onclickAdicional) {
StringBuilder builder = new StringBuilder();
builder.append(onclickAdicional);
builder.append(";");
builder.append("document.forms[");
builder.append("'");
builder.append(idForm);
builder.append("'");
builder.append("]['");
builder.append(id);
builder.append("'].value='");
builder.append(valor);
builder.append("';");
builder.append(" document.forms[");
builder.append("'");
builder.append(idForm);
builder.append("'");
builder.append("].submit()");
builder.append("; return false;");
return builder.toString();
}
/**
* Adiciona um campo hidden no paginador.
*
* @param writer
* a entidade que vai escrever o campo hidden
* @param componente
* o componente
* @param id
* o id do campo hidden
* @throws IOException
* caso não consiga escrever no writer
*/
private void adicionarCampoHidden(ResponseWriter writer,
UIComponent componente, String id) throws IOException {
writer.startElement("input", componente);
writer.writeAttribute("type", "hidden", null);
writer.writeAttribute("name", id, null);
writer.endElement("input");
}
}