【问题标题】:Get records from json store extjs从 json 存储 extjs 获取记录
【发布时间】:2011-05-02 22:25:46
【问题描述】:

我加载了一个 json 存储,我需要从中获取一条记录。 我用过:getAt(index)find()getById(),但没有结果。 这是我的代码:

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(); 

此代码返回未定义:

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":"admin@app.com"}]}

我也测试过这段代码:

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

我得到一个空数组!

PS : 这个 store 没有绑定任何组件; 我只想读写它。

【问题讨论】:

  • 我注意到你会遇到一个问题。在您的 JsonReader 的字段对象中,您声明了一个名为“idProjet”的字段,但您的服务器返回“projetId”。确保这些匹配以获得所需的结果。
  • 这是一个错误,问题没有解决,我把商店很好地加载了,我用网格测试过它可以工作!我仍然无法使用 find() 或任何过滤功能
  • 出了什么问题,出于好奇?
  • 问题是商店加载正常,但我不能使用类似的方法:find()、getById()、getAt()?

标签: javascript json extjs store


【解决方案1】:

似乎服务器正在返回无效的 JSON。为什么您的服务器端脚本的输出以“(”开头?

如果这实际上不是问题,也许您应该考虑接受对您的问题的更多答案。人们更有可能提供帮助。

编辑:好的,所以你很确定你从服务器返回了有效的 json。尝试将“成功”属性添加到服务器的输出中。

如果这不起作用,您将需要进一步挖掘。尝试在您的商店的 .load() 中添加回调选项,并查看传递给回调的内容。这应该可以帮助您找出问题所在。

【讨论】:

  • 不,不是那个问题,谢谢,我正在等待更多答案
  • 加载正常的存储没有问题,我可以将它与不知道问题在哪里的网格绑定,我不能使用存储方法来查找记录
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2013-02-13
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多