Utilizar select dinâmico via Url com JqGrid + Struts, alguem ja teve sucesso?

0 respostas
L

Olá a todos, estou com uma aplicação utilizando as seguintes ferramentas:
apache tomcat 6.0.33, Struts 2.0.11.2, Spring , Jsonplugin 0.31 e JqGrid > 3.X.X.

A Aplicação está funcionando com um campo do JqGrid no modo estático, mas queria implementar select dinâmico, pois quando o usuário inserir um novo dado no BD, terei que mudar o código, inserindo na unha.

O código da Action está assim:

package br.gov.sp.mp.pontoeletronico.model.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.config.Namespace;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import br.gov.sp.mp.pontoeletronico.model.entity.TipoDeOcorrencia;
import br.gov.sp.mp.pontoeletronico.model.service.TipoDeOcorrenciaService;

import com.googlecode.jsonplugin.JSONResult;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/")
@Results({
		@Result(name = "visualizarTeste", value = "/view/jsp/teste.jsp"),		
		@Result(name = "input", type = JSONResult.class, params = {
				"contentType", "application/json", "noCache", "true", "root",
				"jqGridLinhas" }, value = "/view/jsp/teste.jsp"), 
		@Result(name = "selectDescricao", type = JSONResult.class, params = {
				"contentType", "application/json", "noCache", "true", "root",
				"teste" }, value = "/listarSelect.json")
})
public class TesteAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware {

	private static final long serialVersionUID = 1L;
	
	private TipoDeOcorrenciaService tipoDeOcorrenciaService;
	
	private Map<String, String> jqOcorrencias;
	private Map<String, String> selectOcorrencias;
	private List<Map<String, String>> jqGridLinhas;
	private List<Map<String, String>> selectRows;
	private List<TipoDeOcorrencia> ocorrencias;
	private String Id;
	private String sigla;
	private String descricao;
	private String oper;
	private String status;
	private List<String> variavel;
	private String teste;
	
	private Map session;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	@Autowired
	public TesteAction(TipoDeOcorrenciaService tipoDeOcorrenciaService) {
		this.tipoDeOcorrenciaService = tipoDeOcorrenciaService;
	}

	public String execute() {		
		return "visualizarTeste";
	}	

	public String listarTeste(){
	
		ocorrencias = tipoDeOcorrenciaService.listarTodas();
		System.out.println(ocorrencias==null);
		jqGridLinhas = new LinkedList<Map<String,String>>();
		selectOcorrencias = new HashMap<String, String>();
		variavel = new ArrayList<String>();
		int cont = 0;
		teste = "";
		
		for(TipoDeOcorrencia ocorrencia : ocorrencias){
			selectOcorrencias = new HashMap<String, String>();
			
			selectOcorrencias.put("id", String.valueOf(ocorrencia.getId()));
			selectOcorrencias.put("sigla", ocorrencia.getSigla());
			selectOcorrencias.put("descricao", ocorrencia.getDescricao());
			selectOcorrencias.put("status", ""+ocorrencia.getStatus());
			
			jqGridLinhas.add(selectOcorrencias);
			variavel.add(ocorrencias.get(cont).getDescricao().toString());
			cont++;
		}	
		
		for(cont=0;cont<ocorrencias.size();cont++){
			teste.concat(String.valueOf(cont));
			teste.concat(":");
			teste.concat(ocorrencias.get(cont).getDescricao());
			if(!(cont==(ocorrencias.size()-1))){				
				teste.concat(";");				
			}
		}
		
		System.out.println(teste);
		
		return "input";
	}
	
	public String listarSelect() {
				
		ocorrencias = tipoDeOcorrenciaService.listarTodas();
		System.out.println(ocorrencias==null);
		int cont = 0;
		this.teste = "";			
		
		for(cont=0;cont<ocorrencias.size();cont++){
			teste.concat(String.valueOf(cont));
			teste.concat(":");
			teste.concat(ocorrencias.get(cont).getDescricao());
			if(!(cont==(ocorrencias.size()-1))){				
				teste.concat(";");				
			}
		}		
		
		return "selectDescricao";
	}
// + Getters and Setters...

O código da JSP que carrega a Grid:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<table id="list5"></table>
<div id="plist5"></div>
<style type="text/css">
.style2 {color: #3778A8}
</style>
<script type="text/javascript">
function checkSigla(siglaGrid) {
	var novaSigla = siglaGrid;
	if(novaSigla.length > 0 && novaSigla.length < 4){
		for(i=0;i<novaSigla.length;i++){
			if(novaSigla.charAt(i)==" "){
				return [false,"A sigla não deve conter caracteres de espaço!",""];
			}
		}
		
	}else {
		return [false,"A sigla não pode ser nula e deve conter até três letras!",""];			 	
	}
	return [true,"",""];
	
}
</script>
<script type="text/javascript">
$(function() {
	$("#list5").jqGrid({
		jsonReader : {
		      root:"jqGridLinhas",
		      repeatitems: false,
		      id: "id"
		},
   		url:'teste!listarTeste.action',
		datatype: "json",
		caption: "Ocorrências",
   		colNames:['Sigla','Descrição','Status'],
   		colModel:[   		          
   			{name:'sigla',index:'sigla', width:60, editable:true,editrules:{custom:true,custom_func:checkSigla}},
   			{name:'descricao',index:'descricao', width:110, editable:true},
   			{name:'status',index:'status', width:80, editable:true, formatter:"select", edittype:"select",editrules:{required:true}},
   		],
   		rowNum:5,
   		rowList:[05,10],
   		pager:'#plist5',
   		hidegrid:false,
   		sortname:'id',
       	viewrecords:true,
        sortorder:"asc",
        loadComplete: function() {
			$('#list5').setColProp('status', { editoptions: { value:getData()}
			});
			},    
		editurl:"teste!listarSelect.action",
		
		width:350,
		height:"auto"
		
});
    function getData(){
        $.getJSON("teste!listarSelect.action", null, function(data) {
            if (data != null) {
                 return data;
            }            
        });
    }
		$("#list5").jqGrid('navGrid',
				'#plist5',
				{search:false, refresh:false},
				{closeAfterEdit:true},//opções para edit
				{closeAfterDel:true},//opções para del
				{closeAfterAdd:false}//opções para add
		); 
	}
);
</script>

A Grid está carregando e já tentei de tudo, até mesmo os plugins Jquery e Jquery-Grid para o Struts, mas sem sucesso...

Alguém tem alguma idéia???

Desde já agradeço a atenção dispensada...

Criado 11 de dezembro de 2011
Respostas 0
Participantes 1