ruby on rails - extjs嵌套json商店導軌




ruby-on-rails grid (2)

我正在與Ext JS&Rails作為後端..我有myysql數據庫有父子關係,即一對多的關係...我想要一個“單一的”JSON商店,可以用來加載“網格”數據與父母和孩子的記錄..也有什麼辦法綁定兩個 - 表單以及網格到同一商店,但有不同的JSONreaders? 即表單的數據讀取器會讀取字符串:'customers' ,grid會讀取根:'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
    }

你可以使用默認的渲染方法。 為該列定義一個渲染器方法。

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商店的字段mapping到嵌套的json。 例如,如果您需要訪問產品名稱,您可以擁有:

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

您也可以使用數組符號而不是點運算符。 例如: "products['prodnm']"


新的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'));
            });
        });
    }
});




jsonstore