extjs 嵌套 json

Posted

技术标签:

【中文标题】extjs 嵌套 json【英文标题】:extjs nested json 【发布时间】:2013-02-28 19:32:20 【问题描述】:

从各个地方获取关于嵌套 json 是否在 extjs 中可用的相互冲突的信息。注意使用 Sencha Architect 可能会使事情复杂化...

我有一个通过 store/ajax 代理返回的客户端 json 对象。我无法访问第一级以外的任何内容。

JSON 看起来像这样:

    
    "client": 
        "0": 
            "id": "12345",
            "type_id": "3",
            "association_id": "0",
            "is_active": "1",
            "manager_id": "2",
            "business_name": null,
            "has_payroll": "0",
            "business_proprietor_contact_id": null,
            "date_created": "1358893243",
            "date_modified": "1358893243"
        ,
        "contacts": [
            
                "id": "1",
                "client_id": "12345",
                "contact_id": "1",
                "is_primary": "0",
                "display_priority": "0",
                "relationship": "Client",
                "friendly_name": "Homer",
                "contact": [
                    
                        "id": "1",
                        "surname": "Mr.",
                        "first_name": "Homer",
                        "middle_name": "J",
                        "last_name": "Simpson",
                        "client_id": "12345",
                        "date_created": "1356038714",
                        "date_modified": null
                    ,
                    
                        "addresses": [
                            
                                "id": "1",
                                "contact_id": "1",
                                "address_id": "16",
                                "is_mailing_address": "0",
                                "is_primary_address": "0",
                                "display_priority": "0",
                                "address": 
                                    "0": 
                                        "id": "16",
                                        "address": "5 Dirt Rd",
                                        "address2": "",
                                        "city": "TAXMASTER",
                                        "state": "NY",
                                        "zip_code": "454545",
                                        "country": "USA",
                                        "other": null,
                                        "phone": null,
                                        "fax": null,
                                        "display_priority": null,
                                        "is_mailing_address": "1"
                                    ,
                                    "data": [
                                        
                                            "id": "10",
                                            "label": "Phone",
                                            "value": "BUT WHO WAS",
                                            "date_created": "1356104685",
                                            "date_modified": "1357142782",
                                            "user_id": null,
                                            "display_priority": null,
                                            "client_id": null,
                                            "contact_id": null,
                                            "account_id": null,
                                            "business_id": null,
                                            "address_id": "16",
                                            "is_deleted": "0"
                                        ,
                                        
                                            "id": "14",
                                            "label": "Fax",
                                            "value": "simile",
                                            "date_created": "1356557379",
                                            "date_modified": "1357142782",
                                            "user_id": null,
                                            "display_priority": null,
                                            "client_id": null,
                                            "contact_id": null,
                                            "account_id": null,
                                            "business_id": null,
                                            "address_id": "16",
                                            "is_deleted": "0"
                                        ,
                                        
                                            "id": "17",
                                            "label": "Also Known as:",
                                            "value": "DUDE",
                                            "date_created": "1357141357",
                                            "date_modified": "1357142782",
                                            "user_id": null,
                                            "display_priority": null,
                                            "client_id": null,
                                            "contact_id": null,
                                            "account_id": null,
                                            "business_id": null,
                                            "address_id": "16",
                                            "is_deleted": "0"
                                        
                                    ]
                                
                            ,
                            
                                "id": "6",
                                "contact_id": "1",
                                "address_id": "26",
                                "is_mailing_address": "0",
                                "is_primary_address": "0",
                                "display_priority": "0",
                                "address": 
                                    "0": 
                                        "id": "26",
                                        "address": "1 Lift Road",
                                        "address2": "",
                                        "city": "Tony Beach",
                                        "state": "KS",
                                        "zip_code": "15151",
                                        "country": "USA",
                                        "other": null,
                                        "phone": null,
                                        "fax": null,
                                        "display_priority": null,
                                        "is_mailing_address": "0"
                                    ,
                                    "data": [
                                        
                                            "id": "18",
                                            "label": "Phone:",
                                            "value": "cool phone number",
                                            "date_created": "1357141527",
                                            "date_modified": "1357141527",
                                            "user_id": null,
                                            "display_priority": null,
                                            "client_id": null,
                                            "contact_id": null,
                                            "account_id": null,
                                            "business_id": null,
                                            "address_id": "26",
                                            "is_deleted": "0"
                                        ,
                                        
                                            "id": "19",
                                            "label": "Note:",
                                            "value": "cool note",
                                            "date_created": "1357141527",
                                            "date_modified": "1357141527",
                                            "user_id": null,
                                            "display_priority": null,
                                            "client_id": null,
                                            "contact_id": null,
                                            "account_id": null,
                                            "business_id": null,
                                            "address_id": "26",
                                            "is_deleted": "0"
                                        
                                    ]
                                
                            ,
                        ],
                        "data": [
                            
                                "id": "1",
                                "label": "asdf",
                                "value": "dfdfefefefef",
                                "date_created": "1356038714",
                                "date_modified": "1356040822",
                                "user_id": "2",
                                "display_priority": "0",
                                "client_id": null,
                                "contact_id": "1",
                                "account_id": null,
                                "business_id": null,
                                "address_id": null,
                                "is_deleted": "0"
                            ,
                            
                                "id": "2",
                                "label": "asfd",
                                "value": "152151",
                                "date_created": "1356038714",
                                "date_modified": "1356040822",
                                "user_id": null,
                                "display_priority": "2",
                                "client_id": null,
                                "contact_id": "1",
                                "account_id": null,
                                "business_id": null,
                                "address_id": null,
                                "is_deleted": "0"
                            ,
                        ]
                    
                ]
            ,
            
                "id": "7",
                "client_id": "12345",
                "contact_id": "11",
                "is_primary": "0",
                "display_priority": "0",
                "relationship": "None",
                "friendly_name": "Mary Jane",
                "contact": 
                    "0": 
                        "id": "11",
                        "surname": "",
                        "first_name": "Mary Jane",
                        "middle_name": "",
                        "last_name": "Simpson",
                        "client_id": null,
                        "date_created": "1357145795",
                        "date_modified": null
                    ,
                    "7": 
                        "addresses": [
                            
                                "id": "11",
                                "contact_id": "11",
                                "address_id": "33",
                                "is_mailing_address": "0",
                                "is_primary_address": "0",
                                "display_priority": "0",
                                "address": 
                                    "0": 
                                        "id": "33",
                                        "address": "33 Street",
                                        "address2": "",
                                        "city": "NY",
                                        "state": "NY",
                                        "zip_code": "06830",
                                        "country": "USA",
                                        "other": null,
                                        "phone": null,
                                        "fax": null,
                                        "display_priority": null,
                                        "is_mailing_address": "0"
                                    ,
                                    "data": [
                                        
                                            "id": "31",
                                            "label": "Fax:",
                                            "value": "asdfasdf",
                                            "date_created": "1357145905",
                                            "date_modified": "1357145939",
                                            "user_id": null,
                                            "display_priority": null,
                                            "client_id": null,
                                            "contact_id": null,
                                            "account_id": null,
                                            "business_id": null,
                                            "address_id": "33",
                                            "is_deleted": "0"
                                        ,
                                        
                                            "id": "32",
                                            "label": "Fax:",
                                            "value": "sadfsadf",
                                            "date_created": "1357145905",
                                            "date_modified": "1357145905",
                                            "user_id": null,
                                            "display_priority": null,
                                            "client_id": null,
                                            "contact_id": null,
                                            "account_id": null,
                                            "business_id": null,
                                            "address_id": "33",
                                            "is_deleted": "0"
                                        
                                    ]
                                
                            
                        ],
                        "data": [
                            
                                "id": "29",
                                "label": "Phone:",
                                "value": "asdfsadf",
                                "date_created": "1357145833",
                                "date_modified": "1357145833",
                                "user_id": "2",
                                "display_priority": null,
                                "client_id": null,
                                "contact_id": "11",
                                "account_id": null,
                                "business_id": null,
                                "address_id": null,
                                "is_deleted": "0"
                            ,
                            
                                "id": "30",
                                "label": "Email:",
                                "value": "dsfgdsfg",
                                "date_created": "1357145855",
                                "date_modified": "1357145855",
                                "user_id": "2",
                                "display_priority": null,
                                "client_id": null,
                                "contact_id": "11",
                                "account_id": null,
                                "business_id": null,
                                "address_id": null,
                                "is_deleted": "0"
                            
                        ]
                    
                
            
        ],
        "notes": [
            
                "id": "5",
                "client_id": "12345",
                "label": "This is a HomerHomer Note",
                "value": "This is from Homer's Dashboard Notes.",
                "contact_id": null,
                "address_id": null,
                "account_id": null,
                "account_data_id": null,
                "account_credential_id": null,
                "payment_id": null,
                "contact_log_id": null,
                "user_id": "2",
                "bill_id": null,
                "is_deleted": "0",
                "date_created": "1357148256",
                "date_modified": "1357148256"
            
        ]
    

根据https://***.com/a/8318589/183254 嵌套 JSON 不可用。根据https://***.com/a/5604594/183254,它可用的,但必须采用某种格式。

我根据 API 文档格式做了一个小测试:


    results: 2000,
    rows: [
         id: 1, firstname: 'Bill', occupation: 'Gardener', cool: neat: 'wow' ,
         id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' ,
    ]

有了这个,我可以从 tpl 调用所有内容。特别是,cool.neat 打印出wow。但是,将 cool 更改为 cool:[neat:'wow] 会中断。所以它似乎嵌套的 json 部分工作 - 可能只是部分因为我做错了什么。

我的想法是,向 api 发出单个请求并收集所有需要的信息、解析它并将其发送到组件而不是为所需的每组数据创建代理是有意义的。我可能错了……

除了需要的指导/打脸,我还有 2 个问题:

1) 尝试做我正在做的事情是否可行/可行?即尝试使用单个请求来收集多个组件的数据?

2) 有没有办法从 tpl 调试数据?

这是来自 Sencha Architect 的 extjs 4.1

谢谢。

【问题讨论】:

【参考方案1】:

我也有同样的问题。我可以绕过它的唯一方法(我尝试了映射和其他几种解决方案)基本上是拥有一个主商店和子商店。从 ajax 获取信息并将其带入主存储。有一个“加载”侦听器,当它加载时,检查是否需要将信息传递给其他存储。

将所有子存储的 autoload 属性设置为 false 并且从不加载它们。

即:

load: function(t,records,successful,eOpts) 
    var substores [store: nameOfStore, root: rootPropInTheJson,
                   store...etc...etc..];
    for (var i = 0; i < substores.length; i++) 
        if (pathtoroot* != undefined) 
            substores[i].store.add(pathtojsonroot)
        
    

至少,这是我认为你的意思。如果这是你的意思,那么是的,我觉得它很实用,我不知道第二个问题的答案。

【讨论】:

我假设您设置了其他变量? pathtoroot* ?错字?无论如何,我不断收到Uncaught TypeError: Object mySubStore has no method 'add'。另外,您从主商店的构造函数中的负载侦听器调用它吗?谢谢! 随机变量只是填充物。您将 var 的名称与您的商店一起放在那里。如果您做得正确,则在您的商店中对变量使用 add 只会向其中添加记录。因此,您基本上最终会确定是否需要添加记录,然后在需要时将其推出。您能否发布导致类型错误的代码以及商店声明。是的,它在主存储负载监听器中 而 * 是因为我打算在底部添加一个注释,提到根目录的路径最终类似于:t.data.items[i].raw[substores[i] .root][j]。另一件需要注意的事情是,您可以不寻找根,而是在根内寻找一个值。也许是身份证? t.data.items[0].raw[substores[i].root][j].id ` var substores = [store: 'contactStore', root: 'contacts' ]; for (var i = 0; i store 不是字符串。您可以通过 2 种方式做到这一点: 如果您创建了这样的商店:var firstStore = Ext.create('Ext.data.Store', ,那么您将使用 store: firstStore 如果您想使用 storeId,那么您可以使用 store: Ext.getStore('firstStore') 我看到的另一个问题是您是做 .add('contact')。通过 pathtojsonroot,我的意思是 t.data.items[0].raw['root']

以上是关于extjs 嵌套 json的主要内容,如果未能解决你的问题,请参考以下文章

ExtJS 在网格中加载嵌套的 JSON 数据

如何在 Extjs 4 中使用嵌套的 JSON 填充表单

ExtJS 4.1 - JSON 中关联的嵌套 hasOne

ExtJS 4.1 - 检索嵌套 JSON 的 hasOne 信息

如何在 extjs4 中使用 tpl 显示嵌套的 json

如何在extjs的列级获取嵌套的json数据