问题:在做一个项目时,后台通过json传入前台数据,但是后台打印的是三条数据,前台只显示一条数据。
json格式如下:
{"sqksm":[{"carbonLayerNum":1,"endArea":0,"endTime":"","id":0,"shiHouEndArea":0,"shiHouStartArea":0,"startArea":0,"start Time":"","treeId":32,"treeName":"黑松","tree_lm_ksm_rate":0},{"carbonLayerNum":2,"endArea":0,"endTime":"","id":0,"shiHouEn dArea":0,"shiHouStartArea":0,"startArea":0,"startTime":"","treeId":33,"treeName":"紫杉","tree_lm_ksm_rate":0},{"carbonLaye rNum":2,"endArea":0,"endTime":"","id":0,"shiHouEndArea":0,"shiHouStartArea":0,"startArea":0,"startTime":"","treeId":34," treeName":"铁杉","tree_lm_ksm_rate":0}],"total":3}
前台代码如下:
var jixian_kusismu_store = new Ext.data.JsonStore({ url:'listXiangMuShiQianKusiMu.do',/**请求用户数据库*/ totalProperty : 'total', root:'sqksm', fields : [/**用到的json域*/ { name : 'id' },{ name:'treeId' },{ name:'treeName' },{ name:'carbonLayerNum' },{ name:'startTime' },{ name:'endTime' },{ name: 'tree_lm_ksm_rate' },{ name:'startArea' },{ name:'endArea' } ] }); jixian_kusismu_store.load({ params : { start : 0, limit : 20 } });
细心的人可发现:传回的数据的json数据的id都是0,导致extjs无法找到主键,这是是extjs的一个bug。 在grid的getStore的load的回调函数中,用getCount到到的是一条数据。也就是服务端返回了多条数据,但是extjs只解释了一条数据,同样,extjs也不能解析符合主键。
解决方法:回传数据id为主键即可。
{"sqksm":[{"carbonLayerNum":1,"endArea":0,"endTime":"","id":12,"shiHouEndArea":0,"shiHouStartArea":0,"startArea":0,"star tTime":"","treeId":32,"treeName":"黑松","tree_lm_ksm_rate":0},{"carbonLayerNum":2,"endArea":0,"endTime":"","id":13,"shiHou EndArea":0,"shiHouStartArea":0,"startArea":0,"startTime":"","treeId":33,"treeName":"紫杉","tree_lm_ksm_rate":0},{"carbonLa yerNum":2,"endArea":0,"endTime":"","id":14,"shiHouEndArea":0,"shiHouStartArea":0,"startArea":0,"startTime":"","treeId":3 4,"treeName":"铁杉","tree_lm_ksm_rate":0}],"total":3}