Javascript 从 json store extjs 获取记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4060929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:19:32  来源:igfitidea点击:

Get records from json store extjs

javascriptjsonextjsstore

提问by cranberies

I have a json store loaded, I need to grab one record from it. I used : getAt(index), find(), getById(), but no results . This is my code :

我加载了一个 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(); 

This code return undefined :

此代码返回 undefined :

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

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

This is the json string returned from server :

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

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

I've also tested this code :

我也测试了这段代码:

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

and I get an empty array !

我得到一个空数组!

PS : This store is not bound to any component; I just want to read and write to it.

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

回答by Mark

You need to place a callback on the store, that will be fired after it loads. You can then use the data as required.

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

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

回答by timdev

It appears the server is returning invalid JSON. Why does your server-side script's output start with "("?

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

If that's not actually the problem, maybe you should consider accepting some more answers to your questions. People will be more likely to help.

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

EDIT: Okay, so you're pretty sure you're getting valid json back from the server. Try adding a 'success' property to your server's output.

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

If that doesn't work, you'll want to dig in a little more. Try adding a callback option to your store's .load(), and look at the stuff that gets passed into the callback. That should help you figure out where things are going wrong.

如果这不起作用,你会想要多挖一点。尝试将回调选项添加到您商店的 .load() 中,并查看传递到回调中的内容。这应该可以帮助您找出问题出在哪里。