extjs嵌套json存储rails

发布于 2024-11-02 15:13:04 字数 2046 浏览 1 评论 0原文

我正在使用 Ext JS & Rails 作为后端..我有一个具有父子关系的 myysql 数据库,即一对多关系...我想要一个“单个”JSON 存储,可用于加载带有父级和父级的“GRID”数据。儿童记录.. 还有什么方法可以将表单和网格绑定到同一商店但具有不同的 jsonreader?即表单的数据读取器将读取带有 root: 'customers' 的字符串,网格将读取带有 root:'products' 的字符串? JSON 如下所示:

{
        "customers": [{
            "amt": 8000,
            "custnm": "rashmi",
            "id": 2,
            "purdt": "2011-04-27",
            "products": [{
                "amt": 40,
                "customer_id": 2,
                "id": 3,
                "prodnm": "oil",
                "qty": 1,
                "rate": 40
            }]
        }, {
            "amt": 300000,
            "custnm": "bhumika",
            "id": 3,
            "purdt": "2011-04-14",
            "products": [{
                "amt": 40,
                "customer_id": 3,
                "id": 1,
                "prodnm": "soap",
                "qty": 20000,
                "rate": 20
            }, {
                "amt": 150,
                "customer_id": 3,
                "id": 2,
                "prodnm": "shampoo",
                "qty": 3000,
                "rate": 50
            }, {
                "amt": null,
                "customer_id": 3,
                "id": 14,
                "prodnm": "aa",
                "qty": 2,
                "rate": null
            }]
        }, {
            "amt": 15000,
            "custnm": "Shruti",
            "id": 13,
            "purdt": "2011-04-08",
            "products": []
        }, {
            "amt": 200000,
            "custnm": "Jayesh",
            "id": 14,
            "purdt": "2011-03-31",
            "products": []
        }, {
            "amt": 220000,
            "custnm": "SHRUTI",
            "id": 15,
            "purdt": "2011-04-06",
            "products": []
        }, {
            "amt": 10000,
            "custnm": "RASHMI",
            "id": 24,
            "purdt": "2011-04-06",
            "products": []
        }],
        "results": 6
    }

i am working with Ext JS & Rails as backend..i am having myysql database that has parent-child relationship i.e. 1-to-many relationship...I want a "single" JSON store that can be used to load "GRID" data with parent & child records..
Also is there any way to bind both - the form as well as the grid to the same store but having different jsonreaders? i.e. form's datareader would read the string with root: 'customers' and grid will read the string with root :'products'?
The JSON looks like this :

{
        "customers": [{
            "amt": 8000,
            "custnm": "rashmi",
            "id": 2,
            "purdt": "2011-04-27",
            "products": [{
                "amt": 40,
                "customer_id": 2,
                "id": 3,
                "prodnm": "oil",
                "qty": 1,
                "rate": 40
            }]
        }, {
            "amt": 300000,
            "custnm": "bhumika",
            "id": 3,
            "purdt": "2011-04-14",
            "products": [{
                "amt": 40,
                "customer_id": 3,
                "id": 1,
                "prodnm": "soap",
                "qty": 20000,
                "rate": 20
            }, {
                "amt": 150,
                "customer_id": 3,
                "id": 2,
                "prodnm": "shampoo",
                "qty": 3000,
                "rate": 50
            }, {
                "amt": null,
                "customer_id": 3,
                "id": 14,
                "prodnm": "aa",
                "qty": 2,
                "rate": null
            }]
        }, {
            "amt": 15000,
            "custnm": "Shruti",
            "id": 13,
            "purdt": "2011-04-08",
            "products": []
        }, {
            "amt": 200000,
            "custnm": "Jayesh",
            "id": 14,
            "purdt": "2011-03-31",
            "products": []
        }, {
            "amt": 220000,
            "custnm": "SHRUTI",
            "id": 15,
            "purdt": "2011-04-06",
            "products": []
        }, {
            "amt": 10000,
            "custnm": "RASHMI",
            "id": 24,
            "purdt": "2011-04-06",
            "products": []
        }],
        "results": 6
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

残花月 2024-11-09 15:13:04

新的 Ext JS 4 具有模型关联,可以解决该问题(http:// /docs.sencha.com/ext-js/4-0/guide/data)

// each User hasMany Orders
Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: ['id', 'name', 'email'],
    proxy : {
        type: 'rest',
        url : '/users',
        reader: 'json'
    },

    hasMany: 'Orders'
});

// each Order belongsTo a User, and hasMany OrderItems
Ext.define('Order', {
    extend: 'Ext.data.Model',
    fields: ['id', 'user_id', 'status'],
    belongsTo: 'User',
    hasMany: 'OrderItems'
});

// each OrderItem belongsTo an Order
Ext.define('OrderItem', {
    extend: 'Ext.data.Model',
    fields: ['id', 'order_id', 'name', 'description', 'price', 'quantity'],
    belongsTo: 'Order'
});

调用 user.orders() 返回一个使用 Orders 模型配置的 Store,因为 User 模型定义了 hasMany 的关联: 'Orders '。

User.load(123, {
    success: function(user) {
        //we can iterate over the orders easily using the Associations API
        user.orders().each(function(order) {
            console.log(order.get('status'));

            //we can even iterate over each Order's OrderItems:
            order.orderItems().each(function(orderItem) {
                console.log(orderItem.get('title'));
            });
        });
    }
});

The new Ext JS 4 has Model associations which will solve the problem (http://docs.sencha.com/ext-js/4-0/guide/data)

// each User hasMany Orders
Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: ['id', 'name', 'email'],
    proxy : {
        type: 'rest',
        url : '/users',
        reader: 'json'
    },

    hasMany: 'Orders'
});

// each Order belongsTo a User, and hasMany OrderItems
Ext.define('Order', {
    extend: 'Ext.data.Model',
    fields: ['id', 'user_id', 'status'],
    belongsTo: 'User',
    hasMany: 'OrderItems'
});

// each OrderItem belongsTo an Order
Ext.define('OrderItem', {
    extend: 'Ext.data.Model',
    fields: ['id', 'order_id', 'name', 'description', 'price', 'quantity'],
    belongsTo: 'Order'
});

Calling user.orders() returns a Store configured with the Orders model, because the User model defined an association of hasMany: 'Orders'.

User.load(123, {
    success: function(user) {
        //we can iterate over the orders easily using the Associations API
        user.orders().each(function(order) {
            console.log(order.get('status'));

            //we can even iterate over each Order's OrderItems:
            order.orderItems().each(function(orderItem) {
                console.log(orderItem.get('title'));
            });
        });
    }
});
一影成城 2024-11-09 15:13:04

您可以使用覆盖默认渲染方法。为列定义渲染器方法。

renderer: function(value, metaData, record, rowIndex, colIndex, store) {
      // you can use record.get method to access the data you require
      return the_retrived_data;
 }

您需要使用 mapping 属性将 json 存储的字段映射到嵌套的 json。例如,如果您需要访问产品名称,您可以:

{name: 'productname', mapping: 'products.prodnm'} 

您还可以使用数组表示法而不是点运算符。例如:“产品['prodnm']”

You can use override the default rendering method. Define a renderer method for the column.

renderer: function(value, metaData, record, rowIndex, colIndex, store) {
      // you can use record.get method to access the data you require
      return the_retrived_data;
 }

You need to use mapping property to map the fields of your json store to the nested json. For example, if you need to access product name you can have:

{name: 'productname', mapping: 'products.prodnm'} 

You can also use the array notation instead of the dot operator. ex: "products['prodnm']".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文