从json商店获取记录extjs

问题描述:

我有一个json商店加载,我需要从它抓取一条记录。 我用过:getAt(index)find(),getById(),但没有结果。 这是我的代码:从json商店获取记录extjs

var appSettingReader = new Ext.data.JsonReader({  
       root: 'results', 
       },[ 
       {name: 'id', type: 'int', mapping: 'id'}, 
       {name: 'projetId', type: 'int', mapping: 'projetId'}, 
       {name: 'resLevels', type: 'int', mapping: 'resLevels'}, 
       {name: 'maxResToLock', type: 'int', mapping: 'maxResToLock'}, 
       {name: 'maxTimeToLock', type: 'int', mapping: 'maxTimeToLock'}, 
       {name: 'infosToPrint', type: 'string', mapping: 'infosToPrint'} 
       ]) 

var appSettingStore = new Ext.data.Store({ 
       proxy: new Ext.data.HttpProxy({ 
         url: 'inc/getSettings.php', 
         method: 'POST' 
        }), 
       baseParams:{task: "app"}, 
       reader : appSettingReader, 
       sortInfo:{field: 'id', direction: "DESC"} 
       }) 

appSettingStore.load(); 

此代码返回undefined:

console.log(appSettingStore.getAt(0)); 

console.log(appSettingStore.find("id","1")); 

这是JSON字符串从服务器返回:

{success:true,"results":[{"id":"1","projetId":"1","resLevels":"1","maxResToLock":"40","maxTimeToLock":"10","infosToPrint":"1_2_3_5","hotlineMail":"[email protected]"}]} 

我还测试了这款代码:

var records = new Array()  
var test = appSettingStore.each(function(rec){ 
      records.push(rec) 
     }) 
console.log(records) 

,我得到一个空阵列!

PS:本店未绑定任何组件; 我只想读取和写入它。

+0

我注意到你会遇到一个问题。在你的JsonReader的字段对象中声明一个名为“idProjet”的字段,但是你的服务器返回“projetId”。确保这些匹配以获得期望的结果。 – McStretch 2010-10-31 01:33:26

+1

这是一个错误,问题没有解决,我得到了商店罚款我已经测试了一个网格它的工作!我仍然无法使用find()或任何过滤器函数 – cranberies 2010-10-31 02:42:37

+0

出于好奇,问题是什么? – timdev 2010-10-31 02:47:27

您需要在商店上放置一个回调,该回调将在加载后触发。然后您可以根据需要使用这些数据。

store.load({ 
    callback : function(r, options, success) { 
     console.log(r.data) 
    } 
}) 

看来服务器正在返回无效的JSON。为什么你的服务器端脚本的输出开始“(”?

如果这实际上是没有问题的,也许你应该考虑接受一些问题的答案,人们会更容易帮助。

编辑:好吧,所以你很确定你从服务器获得有效的JSON。尝试添加一个'成功'属性到你的服务器的输出。

如果这不起作用,你会想尝试在商店的.load()中添加一个回调选项,并查看传入回调函数的内容,这可以帮助您找出问题出在哪里。

+0

不,它不是问题,谢谢亚姆等待更多的答案 – cranberies 2010-10-30 23:19:46

+0

存储没有问题它加载罚款,我可以绑定它与网格我不知道问题在哪里,我不能使用存储方法来查找记录 – cranberies 2010-10-31 02:47:54