@Public
@Get @Path("/modelos/modelos.json")
public void loadGrid(Integer page, Integer rows, String sidx, String sord){
@SuppressWarnings("unchecked")
Collection<Modelo> model = dao.fetchGrid(sidx, sord,page,rows);
/**
* Aqui fiz um teste para verificar se consigo recuperar o Fabricante
*/
for (Modelo p : model){
System.out.print(p.getFabricante().getFabricante());
}
Collection<Modelo> modelAll = dao.fetchAll();
JQgrid<Modelo> JQgrid = new JQgrid<Modelo>();
JQgrid.setPage(page);
JQgrid.setRecords(modelAll.size());
JQgrid.setTotal((int)Math.ceil((double)modelAll.size() / (double)rows));
JQgrid.setRows(model);
result.use(Results.json()).withoutRoot().from(JQgrid).include("rows").serialize();
/**
* O resultado do Json nao vem com fabricante
* {"page": 1,"total": 1,"records": 2,"rows": [{"id": 1,"modelo": "DSC900l"},{"id": 2,"modelo": "DLS000"}]}
*/
}
Duvida em Json com Vraptor 3
6 Respostas
tenta:
esult.use(Results.json()).withoutRoot().from(JQgrid).recursive().serialize();
Você pode usar o recursive como o lucas falou ou adicionar no includes.
result.use(Results.json()).withoutRoot().from(JQgrid).include("rows","rows.fabricante").serialize();
Olá Lucas usando o recursive ele me retorna:
{“page”: 1,“total”: 1,“records”: 3,“rows”: [{“id”: 3,“fabricante”: {“id”: 2,“fabricante”: “Teste”},“modelo”: “testando”},{“id”: 1,“fabricante”: {“id”: 1,“fabricante”: “D-Link”},“modelo”: “DSC900l”},{“id”: 2,“fabricante”: {“id”: 1,“fabricante”: “D-Link”},“modelo”: “DLS000”}]}
FernandoAt usando rows.fabricante:
ele da erro como se nao existisse a rows.fabricante .
usando o recursive eu consigo pegar na grid o id do fabricante porem nao pego o fabricante segue codigo!
<script type="text/javascript">
$(function() {
$("#tbl-modelo")
.jqGrid(
{
url : '<c:url value="/modelos/modelos.json"/>',
editurl : '<c:url value="/modelos/actionGrid"/>',
datatype : "json",
autowidth : true,
colNames : [ 'Código', 'Id Fabricante','Fabricante', 'Modelo' ],
colModel : [
{
name : 'id',
index : 'id',
width : 55,
sorttype : "long",
align : "center"
},
{
name : 'fabricante.id',
index : 'fabricante.id',
sorttype : "string",
width : 400,
editable : false
},
//Na teoria aquid seria fabricante.fabricante pois quero o nome do fabricante dentro do objeto fabricante porem ele nao pega nada acima farbicante.id me retorna o is correto!
{
name : 'fabricante.fabricante',
index : 'fabricante.fabricante',
width : 300,
editable : true,
formatter : 'select',
edittype : "select",
editoptions:{
dataInit :
function (elem) {
alert(elem);
alert("ok");
}
}
/**editoptions : {
dataUrl : '<c:url value="/fabricantes/select"/>',
dataEvents : [ {
type : 'change',
fn : function(e) {
// $('#categorie_parent').val(this.value);
var id = (this.value);
}
} ]
}**/
},{
name : 'modelo',
index : 'modelo',
sorttype : "string",
width : 400,
editable : true
}, ],
rowNum : 40,
pager : '#pg-modelo',
height : 500,
viewrecords : true,
sortname : 'fabricante',
sortorder : "desc",
caption : "Lista de Modelos",
jsonReader : {
root : "rows", //array containing actual data
page : "page", //current page
total : "total", //total pages for the query
records : "records", //total number of records
repeatitems : false,
id : "id"
}
});
$("#tbl-modelo").jqGrid('navGrid', '#pg-modelo', {
edit : true,
add : true,
del : true,
search : false
});
});
</script>
o formato que retornou com o recursive não era o que vc queria?
Sim Lucas o Problema é agora pegar o que esta dentro veja:
$(function() {
$("#tbl-modelo")
.jqGrid(
{
url : '<c:url value="/modelos/modelos.json"/>',
editurl : '<c:url value="/modelos/actionGrid"/>',
datatype : "json",
autowidth : true,
colNames : [ 'Código', 'Id Fabricante','Fabricante', 'Modelo' ],
colModel : [
{
name : 'id',
index : 'id',
width : 55,
sorttype : "long",
align : "center"
},
{
//Aqui ele pega normal dentro do fabricante o ID
name : 'fabricante.id',
index : 'fabricante.id',
sorttype : "string",
width : 400,
editable : false
},
//Aqui ele ja nao me retorna o fabricante o que estranho demais e tb nao retorna acessando o objeto que seria fabricante.fabricante
{
name : 'fabricante',
index : 'fabricante',
width : 300,
editable : true,
formatter : 'select',
edittype : "select",
editoptions:{
dataInit :
function (elem) {
alert(elem);
alert("ok");
}
}
/**editoptions : {
dataUrl : '<c:url value="/fabricantes/select"/>',
dataEvents : [ {
type : 'change',
fn : function(e) {
// $('#categorie_parent').val(this.value);
var id = (this.value);
}
} ]
}**/
},{
name : 'modelo',
index : 'modelo',
sorttype : "string",
width : 400,
editable : true
}, ],
rowNum : 40,
pager : '#pg-modelo',
height : 500,
viewrecords : true,
sortname : 'fabricante',
sortorder : "desc",
caption : "Lista de Modelos",
jsonReader : {
root : "rows", //array containing actual data
page : "page", //current page
total : "total", //total pages for the query
records : "records", //total number of records
repeatitems : false,
id : "id"
}
});
$("#tbl-modelo").jqGrid('navGrid', '#pg-modelo', {
edit : true,
add : true,
del : true,
search : false
});
});
o que na vdd creio que o correto seria no json
{“page”: 1,“total”: 1,“records”: 3,“rows”: [{“id”: 3,“fabricante”:“Teste”,“modelo”: “testando”},{“id”: 1,“fabricante”: “D-Link”,“modelo”: “DSC900l”},{“id”: 2,“fabricante”: “D-Link”,“modelo”: “DLS000”}]}
estranho!
cria um DTO com as propriedades que vc precisa gerar, é mais fácil do que ficar customizando a saída do json.